Expand description
Valuable provides object-safe value inspection. Use cases include passing structured data to trait objects and object-safe serialization.
§Getting started
First, derive Valuable
on your types.
use valuable::Valuable;
#[derive(Valuable)]
struct HelloWorld {
message: Message,
}
#[derive(Valuable)]
enum Message {
HelloWorld,
Custom(String),
}
Then, implement a visitor to inspect the data.
use valuable::{NamedValues, Value, Valuable, Visit};
struct Print;
impl Visit for Print {
fn visit_value(&mut self, value: Value<'_>) {
match value {
Value::Structable(v) => {
println!("struct {}", v.definition().name());
v.visit(self);
}
Value::Enumerable(v) => {
println!("enum {}::{}", v.definition().name(), v.variant().name());
v.visit(self);
}
Value::Listable(v) => {
println!("list");
v.visit(self);
}
Value::Mappable(v) => {
println!("map");
v.visit(self);
}
_ => {
println!("value {:?}", value);
}
}
}
fn visit_named_fields(&mut self, named_fields: &NamedValues<'_>) {
for (field, value) in named_fields.iter() {
println!("named field {}", field.name());
value.visit(self);
}
}
fn visit_unnamed_fields(&mut self, values: &[Value<'_>]) {
for value in values {
value.visit(self);
}
}
fn visit_entry(&mut self, key: Value<'_>, value: Value<'_>) {
println!("key / value");
key.visit(self);
value.visit(self);
}
}
Then, use the visitor to visit the value.
let hello_world = HelloWorld { message: Message::HelloWorld };
hello_world.visit(&mut Print);
§Related Crates
valuable-serde
provides a bridge betweenvaluable
and theserde
serialization ecosystem. Usingvaluable_serde::Serializable
allows any type that implementsValuable
to be serialized by anyserde::ser::Serializer
.
Structs§
- Named
Field - A named field
- Named
Values - Set of values from a
Structable
orEnumerable
with named fields. - Variant
Def - An enum variant definition.
Enums§
- EnumDef
- An enum’s variants, variant fields, and other enum-level information.
- Fields
- Data stored within a
Structable
or anEnumerable
. - Slice
- A slice containing primitive values.
- Struct
Def - A struct’s name, fields, and other struct-level information.
- Tuple
Def - The number of fields and other tuple-level information.
- Value
- Any Rust value
- Variant
- An enum variant
Traits§
- Enumerable
- An enum-like
Valuable
sub-type. - Listable
- A list-like
Valuable
sub-type. - Mappable
- A map-like
Valuable
sub-type. - Structable
- A struct-like
Valuable
sub-type. - Tuplable
- A tuple-like
Valuable
sub-type. - Valuable
- A type that can be converted to a
Value
. - Visit
- Traverse a value’s fields and variants.
Functions§
Derive Macros§
- Valuable
derive
- Derive a
Valuable
implementation for a struct or enum.