Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement valuable-serde #23

Merged
merged 28 commits into from
May 21, 2021
Merged

Implement valuable-serde #23

merged 28 commits into from
May 21, 2021

Conversation

taiki-e
Copy link
Member

@taiki-e taiki-e commented May 14, 2021

Closes #17

Examples

use valuable::Valuable;
use valuable_serde::Serializable;

#[derive(Valuable)]
struct Point {
    x: i32,
    y: i32,
}

let point = Point { x: 1, y: 2 };

let value = Serializable::new(&point);

assert_eq!(
    serde_json::to_string(&value).unwrap(),
    r#"{"x":1,"y":2}"#,
);

valuable-serde/src/lib.rs Outdated Show resolved Hide resolved
valuable-serde/src/lib.rs Outdated Show resolved Hide resolved
Comment on lines 27 to 28
// TODO
// Unit,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, Valuable treats the unit variant/struct as an empty tuple variant/struct. Therefore, I'm not sure if we can provide a behavior that is fully consistent with serde on unit variant/struct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems related to #21, maybe provide some thoughts there?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem I encountered here is mainly related to the unit struct, not to tuples. But in any case, we need to think about how to represent tuples...

// TODO:
// - valuable treats unit struct as an empty tuple struct
// - serde treats unit struct as unit struct
#[test]
fn test_unit_struct() {
#[derive(Debug, PartialEq, Valuable, Serialize)]
struct S;
assert_ser_tokens(
&Serializable::new(S),
&[
Token::TupleStruct { name: "S", len: 0 },
Token::TupleStructEnd,
],
);
assert_ser_tokens(&S, &[Token::UnitStruct { name: "S" }]);
}

Comment on lines +69 to +78
// TODO:
// - valuable treats Option<T> as T or unit
// - serde treats Option<T> as Some(T) or None
#[test]
fn test_option() {
assert_ser_tokens(&Serializable::new(None::<u8>), &[Token::Unit]);
assert_ser_tokens(&None::<u8>, &[Token::None]);
assert_ser_tokens(&Serializable::new(Some(1)), &[Token::I32(1)]);
assert_ser_tokens(&Some(1), &[Token::Some, Token::I32(1)]);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • valuable treats Option<T> as T or unit
  • serde treats Option<T> as Some(T) or None

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, we can consider breaking out Some and None in Value?

@taiki-e taiki-e marked this pull request as ready for review May 18, 2021 12:25
@taiki-e
Copy link
Member Author

taiki-e commented May 18, 2021

I think this is now ready for review.

Currently, I'm aware of two differences in behavior from serde. See #23 (comment) and #23 (comment) for more.

@taiki-e taiki-e requested review from carllerche and hawkw May 18, 2021 13:04
@taiki-e taiki-e changed the title Initial implement valuable-serde Implement valuable-serde May 20, 2021
Copy link
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Could you file follow up issues w/ the remaining unknowns?

@taiki-e
Copy link
Member Author

taiki-e commented May 21, 2021

filed #42 to track remaining todos.

@taiki-e taiki-e merged commit 4238787 into master May 21, 2021
@taiki-e taiki-e deleted the taiki-e/serde branch May 21, 2021 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement valuable-serde crate
2 participants