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
26 changes: 26 additions & 0 deletions Cargo-recent.lock
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3011d1213f159867b13cfd6ac92d2cd5f1345762c63be3554e84092d85a50bbd"

[[package]]
name = "itoa"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2"

[[package]]
name = "jobserver"
version = "0.1.34"
Expand Down Expand Up @@ -451,6 +457,19 @@ dependencies = [
"syn",
]

[[package]]
name = "serde_json"
version = "1.0.149"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86"
dependencies = [
"itoa",
"memchr",
"serde",
"serde_core",
"zmij",
]

[[package]]
name = "shlex"
version = "1.3.0"
Expand Down Expand Up @@ -503,6 +522,7 @@ dependencies = [
"miniscript",
"santiago",
"serde",
"serde_json",
"simplicity-sys 0.6.1",
]

Expand Down Expand Up @@ -626,3 +646,9 @@ dependencies = [
"quote",
"syn",
]

[[package]]
name = "zmij"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"
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_json = "1.0.149"

[workspace]
members = ["simpcli", "simplicity-sys", "fuzz"]
Expand Down
2 changes: 1 addition & 1 deletion src/bit_encoding/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ mod test {
let mut w = BitWriter::from(&mut sink);
encode_natural(n, &mut w).expect("encoding to vector");
w.flush_all().expect("flushing");
let m = BitIter::from(sink.into_iter())
let m: usize = BitIter::from(sink.into_iter())
Copy link
Author

@trevarj trevarj Feb 20, 2026

Choose a reason for hiding this comment

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

Once I imported serde_json this type couldn't be inferred because of conflicting implementations

.read_natural(None)
.expect("decoding from vector");
assert_eq!(n, m);
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ mod merkle;
pub mod node;
#[cfg(feature = "elements")]
pub mod policy;
#[cfg(feature = "serde")]
pub mod serializers;
pub mod types;
mod value;

Expand Down
1 change: 1 addition & 0 deletions src/serializers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod stringify;
58 changes: 58 additions & 0 deletions src/serializers/stringify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/// This module provides a generic serializer that serializes any type T:
/// Display as a string and can be used with `#[serde(with =
/// "simplicity::serializers::stringify")` over a struct field that implements
/// `std::fmt::Display`.
///
///```rust
/// use simplicity::Value;
///
/// #[derive(serde::Serialize)]
/// struct Foo {
/// #[serde(with = "simplicity::serializers::stringify")]
/// v: Value,
/// }
///
/// assert_eq!(
/// serde_json::to_value(&Foo { v: Value::u1(0) }).unwrap(),
/// serde_json::json!( { "v": "0b0" } )
/// );
///```
use std::fmt::Display;

use serde::Serializer;

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;

#[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_eq!(
serde_json::to_value(&Foo { v }).unwrap(),
serde_json::json!({ "v": want }),
);
}
}
}