-
Notifications
You must be signed in to change notification settings - Fork 26
serde: add stringify serializer over generic T:Display #340
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // re-export of the serde crate | ||
| pub use ::serde::*; | ||
| pub mod stringify; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In 467111c: I really don't like this. It's a weird trick to pretend that there are more things in the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is true. Any reason for re-exporting Alternative module ideas:
Other ideas? What's your preference? |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| use std::fmt::Display; | ||
|
|
||
| use serde::Serializer; | ||
|
|
||
| /// A generic serializer that serializes any type T: Display as a string and can | ||
| /// be used with `#[serde(with = "simplicity::serde::stringify")` over a struct | ||
| /// field. | ||
| ///```rust | ||
| /// #[derive(serde::Serialize)] | ||
| /// struct Foo { | ||
| /// #[serde(with = "simplicity::serde::stringify")] | ||
| /// v: simplicity::Value, | ||
| /// } | ||
| ///``` | ||
| pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error> | ||
| where | ||
| S: Serializer, | ||
| T: Display, | ||
| { | ||
| serializer.serialize_str(&value.to_string()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::Value; | ||
| use serde::Serialize; | ||
| use serde_test::{assert_ser_tokens, Token}; | ||
|
|
||
| #[test] | ||
| fn can_stringify_value() { | ||
| #[derive(Serialize)] | ||
| struct Foo { | ||
| #[serde(with = "crate::serde::stringify")] | ||
| v: Value, | ||
| } | ||
|
|
||
| // values taken from Value's `value_display` test | ||
| let value_string_pairs = [ | ||
| (Value::u1(0), "0b0"), | ||
| (Value::u1(1), "0b1"), | ||
| (Value::u4(6), "0x6"), | ||
| ]; | ||
| for (v, want) in value_string_pairs { | ||
| assert_ser_tokens( | ||
| &Foo { v }, | ||
| &[ | ||
| Token::Struct { | ||
| name: "Foo", | ||
| len: 1, | ||
| }, | ||
| Token::Str("v"), | ||
| Token::Str(want), | ||
| Token::StructEnd, | ||
| ], | ||
| ); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In 467111c:
We should set this to the actual minimum version required. Also, I think we should just test with
serde_jsonsinceserde_testis weird and broken.