Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ getrandom = { version = "0.2", features = ["js"] }
simplicity-sys = { version = "0.6.1", path = "./simplicity-sys", features = [
"test-utils",
] }
serde_test = "1"
Copy link
Collaborator

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_json since serde_test is weird and broken.


[workspace]
members = ["simpcli", "simplicity-sys", "fuzz"]
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@
pub extern crate bitcoin;
#[cfg(feature = "elements")]
pub extern crate elements;
#[cfg(feature = "serde")]
pub extern crate serde;

/// Re-export of base64 crate
#[cfg(feature = "base64")]
Expand Down Expand Up @@ -96,6 +94,8 @@ mod merkle;
pub mod node;
#[cfg(feature = "elements")]
pub mod policy;
#[cfg(feature = "serde")]
pub mod serde;
pub mod types;
mod value;

Expand Down
3 changes: 3 additions & 0 deletions src/serde/mod.rs
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 serde crate than there actually are.

Copy link
Author

@trevarj trevarj Feb 18, 2026

Choose a reason for hiding this comment

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

That is true. Any reason for re-exporting serde?

Alternative module ideas:

  1. rust_simplicity::serializers::stringify
  2. rust_simplicity::value::serde::stringify (the serializer is generic so this really doesn't make sense)

Other ideas? What's your preference?

58 changes: 58 additions & 0 deletions src/serde/stringify.rs
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,
],
);
}
}
}
Loading