Skip to content
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "scale-value"
version = "0.18.0"
version = "0.18.1"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"

Expand Down
52 changes: 43 additions & 9 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion src/scale_impls/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ macro_rules! to_unnamed_composite {
}};
}

// We can't implement this on `Value<TypeId>` 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],
Expand All @@ -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<Self, scale_decode::Error> {
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<R: scale_decode::TypeResolver> =
Expand Down
Loading