diff --git a/CHANGELOG.md b/CHANGELOG.md index c5f070a..533f146 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ The format is based on [Keep a Changelog]. [Keep a Changelog]: http://keepachangelog.com/en/1.0.0/ +# 0.18.1 (2025-10-09) + +A small patch release to impl `DecodeAsFields` for `scale_value::Value<()>`, allowing the `Value` type to be used in more contexts upstream. + +Also elaborate on the `value!` macro docs a little. + ## 0.18.0 (2024-11-15) This release makes scale-value entirely no_std which is now using core::error::Error instead of std::error::Error as it was using before behind diff --git a/Cargo.toml b/Cargo.toml index 29a2e6b..7479cb4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "scale-value" -version = "0.18.0" +version = "0.18.1" authors = ["Parity Technologies "] edition = "2021" diff --git a/src/macros.rs b/src/macros.rs index 92a1d4b..3145e4b 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -13,30 +13,64 @@ // See the License for the specific language governing permissions and // limitations under the License. -/// Construct a `scale_value::Value` +/// Construct a `scale_value::Value` using syntax similar to the `serde_jaon::json!` macro. /// +/// Construct values representing structs (SCALE "composite" types with named fields): /// -/// Supports unnamed and named composites and variants: /// ``` /// use scale_value::value; /// -/// let val = value!({ -/// name: "localhost", -/// address: V4(127, 0, 0, 1), -/// payload: { -/// bytes: (255, 3, 4, 9), -/// method: ("Post", 3000), -/// }, +/// let struct_value = value!({ +/// name: "foo", +/// value: 123, +/// sub_fields: { +/// foo: true, +/// bar: false +/// } /// }); +/// ``` +/// +/// Construct values representing tuples (SCALE composite types with unnamed fields): +/// +/// ``` +/// use scale_value::value; +/// +/// let tuple_value = value!{ +/// ("foo", 123, (true, false)) +/// }; +/// ``` +/// +/// Construct enum variants by prefixing the variant name to either of the above constructions: /// /// ``` +/// use scale_value::value; +/// +/// let variant_with_unnamed_fields = value!{ +/// VariantName("foo", 123) +/// }; +/// +/// let variant_with_named_fields = value!{ +/// VariantName { +/// name: "foo", +/// value: 123, +/// sub_fields: { +/// foo: true, +/// bar: false, +/// other_variant: AnotherVariant(true, 1,2,3) +/// } +/// } +/// }; +/// ``` +/// /// Values can be nested in each other: +/// /// ``` /// use scale_value::value; /// /// let data_value = value!((1, v1(1, 2), 3)); /// let val = value!(POST { data: data_value }); /// ``` +/// /// Trailing commas are optional. #[macro_export(local_inner_macros)] macro_rules! value { diff --git a/src/scale_impls/decode.rs b/src/scale_impls/decode.rs index a0fdeb1..fa3d533 100644 --- a/src/scale_impls/decode.rs +++ b/src/scale_impls/decode.rs @@ -87,7 +87,6 @@ macro_rules! to_unnamed_composite { }}; } -// We can't implement this on `Value` because we have no TypeId to assign to the value. impl scale_decode::DecodeAsFields for Composite<()> { fn decode_as_fields<'resolver, R: TypeResolver>( input: &mut &[u8], @@ -111,6 +110,17 @@ impl scale_decode::DecodeAsFields for Composite<()> { } } +impl scale_decode::DecodeAsFields for Value<()> { + fn decode_as_fields<'resolver, R: TypeResolver>( + input: &mut &[u8], + fields: &mut dyn FieldIter<'resolver, R::TypeId>, + types: &'resolver R, + ) -> Result { + let composite = Composite::<()>::decode_as_fields(input, fields, types)?; + Ok(Value { value: ValueDef::Composite(composite), context: () }) + } +} + impl scale_decode::IntoVisitor for Value<()> { // Note: the DefaultMapper just removes all type ids here. type AnyVisitor =