Skip to main content

block_params

Macro block_params 

Source
macro_rules! block_params {
    (@default required) => { ... };
    (@default absent) => { ... };
    (@default absent $text:literal) => { ... };
    (@default $v:expr => $text:literal) => { ... };
    (@default $v:expr) => { ... };
    (@or $d:literal) => { ... };
    (@or $d:literal $v:literal) => { ... };
    (@bound) => { ... };
    (@bound $b:literal) => { ... };
    (@choices) => { ... };
    (@choices [$($c:literal),+ $(,)?]) => { ... };
    (@choices ($c:expr)) => { ... };
    (@info $v:ident, $field:ident, $ty:ty, [flatten], $($rest:tt)*) => { ... };
    (
        @info $v:ident, $field:ident, $ty:ty, [$($def:tt)+], [$($unit:literal)?], [$($choices:tt)?],
        [$($gt:literal)?], [$($ge:literal)?], [$($le:literal)?], [$($lt:literal)?], $doc:expr
    ) => { ... };
    (@take $kind:ident, $m:ident, $params:ident, $field:ident, $ty:ty, [flatten]) => { ... };
    (@take $kind:ident, $m:ident, $params:ident, $field:ident, $ty:ty, [$($def:tt)+]) => { ... };
    (
        $(#[$sattr:meta])*
        $vis:vis struct $name:ident {
            $(
                $(#[$fattr:meta])*
                $field:ident : $ty:ty [ $($def:tt)+ ]
                $($unit:literal)?
                $(one_of $choices:tt)?
                $(> $gt:literal)?
                $(>= $ge:literal)?
                $(<= $le:literal)?
                $(< $lt:literal)?
                , $doc:expr ;
            )*
        }
    ) => { ... };
}
Expand description

Declare the parameters of a block: the structure, its Params parser and its documentation (see the module documentation).

block_params! {
    [pub] struct NAME {
        FIELD: TYPE [DEFAULT] ["UNIT"] [one_of [VALUE, …] | one_of (CONST)] [> B] [>= B] [<= B] [< B], "MEANING";
        FIELD: GROUP [flatten], "MEANING";
        …
    }
}

DEFAULT is required, absent, absent "TEXT", a value or VALUE => "TEXT"; the type of a field implements ParamType (its JSON Schema) and Deserialize. The accepted values are a list of literals or, in parentheses, a constant array of strings kept with the type they name (one_of (DetectorKind::NAMES)). A [flatten] field is a group of parameters declared once with block_params! and shared by several blocks: its parameters are those of the block, at the place of the field, and a name that neither the block nor the group declares is still rejected.

use comnum_spec::blocks::Params;
comnum_spec::block_params! {
    struct Decoder {
        iterations: usize [50] >= 1, "decoding iterations at most";
    }
}
comnum_spec::block_params! {
    struct Code {
        n: usize [required], "length";
        decoder: Decoder [flatten], "the decoder";
    }
}
let c = Code::parse("code", serde_json::json!({"n": 8, "iterations": 5})).unwrap();
assert_eq!((c.n, c.decoder.iterations), (8, 5));
assert_eq!(Code::params().iter().map(|p| p.name).collect::<Vec<_>>(), ["n", "iterations"]);
assert!(Code::parse("code", serde_json::json!({"n": 8, "rule": "min_sum"})).is_err());