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

Support partial serialization & deserialization #7

Merged
merged 1 commit into from
Oct 31, 2022

Conversation

cfrantz
Copy link
Collaborator

@cfrantz cfrantz commented Oct 28, 2022

This feature allows writing structs which contain Document nodes.

  • When deserializing, everything under the Document node is simply attached to the struct.
  • When serializing, the Document node is passed through unmodified.

Signed-off-by: Chris Frantz [email protected]

}"#;
let p = serde_annotate::from_str::<Partial>(doc)?;
assert_eq!(p.n, 10);
if let Document::Mapping(m) = p.doc {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style preference: I prefer to replace large if statements with guard clauses wherever possible.

That is possible with this if statement. One way to do so is to use the new let else feature:

let Document::mapping(m) = p.doc else {
    panic!("Expecting Document::Mapping");
};
let (k, v) = m[0].as_kv()?;
/* ... */

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oh, that's brand new (rust 1.6.5). Fixed.

src/partial.rs Outdated
unsafe {
// If `serializer` is the correct type, then we can clone the
// Document node and return it.
let r: Result<Document, Error> = Ok(self.clone());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should probably move this line out of the unsafe block, because unless I am missing something it doesn't require unsafe.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Only the transmute needs unsafe. Fixed.

This feature allows writing structs which contain `Document` nodes.

- When deserializing, everything under the `Document` node is simply
  attached to the struct.
- When serializing, the `Document` node is passed through unmodified.

Signed-off-by: Chris Frantz <[email protected]>
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.

2 participants