pub trait ConfigShape: Sized {
// Required methods
fn schema() -> Schema;
fn to_value(&self) -> Value;
fn from_value(value: &Value) -> Result<Self, ShapeError>;
}Expand description
A type that can describe its own configuration shape and move through it.
Implemented by #[derive(ConfigShape)] for structs, and by hand for the
primitives and containers below. The three methods have to agree:
to_value must produce something schema accepts, and from_value must
invert to_value. A type where they disagree is a silently lossy option,
which is the failure the SDK’s own round-trip test exists to catch.
Provided impls: bool, i64, String, Vec<T> and Option<T> (the last
meaning “not required” at field position — see its impl). The derive
supports structs with named fields (→ Schema::Record) and all-unit enums
(→ Schema::Enum); tuple structs, data-carrying enums and unions are
compile errors. The derive is re-exported at the crate root as
lattice_plugin_sdk::ConfigShape, beside this trait’s path in shape.
§Examples
use lattice_plugin_sdk::ConfigShape;
use lattice_plugin_sdk::shape::{ConfigShape as _, Schema, Value};
/// How a capture is filed.
#[derive(Debug, PartialEq, ConfigShape)]
enum Disposition { Append, FileUnder }
assert_eq!(
Disposition::schema(),
Schema::Enum(vec!["append".into(), "file-under".into()]),
);
assert_eq!(
Disposition::from_value(&Value::Str("file-under".into())),
Ok(Disposition::FileUnder),
);
assert!(Disposition::from_value(&Value::Str("sideways".into())).is_err());Required Methods§
Sourcefn to_value(&self) -> Value
fn to_value(&self) -> Value
This value as a tree the host can validate against
schema. Must always be accepted by it.
Sourcefn from_value(value: &Value) -> Result<Self, ShapeError>
fn from_value(value: &Value) -> Result<Self, ShapeError>
Read a value back; the inverse of to_value.
§Errors
A ShapeError carrying the path to the offending node when a kind is
wrong, a required field is missing, or an enum string names no variant.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl ConfigShape for String
impl ConfigShape for String
Source§impl ConfigShape for bool
impl ConfigShape for bool
Source§impl ConfigShape for i64
impl ConfigShape for i64
Source§impl<T: ConfigShape> ConfigShape for Option<T>
Option<T> is how a field says it is not required.
impl<T: ConfigShape> ConfigShape for Option<T>
Option<T> is how a field says it is not required.
The schema of an Option<T> is T’s — optionality is a property of the
FIELD, not of the value, which is why Field::required carries it and
this impl does not wrap the schema in anything. A bare Option<T> used as a
whole option’s type therefore describes itself as T; None is then
indistinguishable from absent, which is the correct reading at a leaf and
the reason the derive only consults this at field position.