This crate provides machine-readable descriptions for types.
For a general overview, please check out the guide.
The idea is to make types discoverable for users by explaining them in a way
that a user can understand without knowing implementation details (a u16
is
an "integer with 16 bit")
One could make configuration types explained with this crate and show the explanation (in a GUI, web interface, some special config-editor) to the user.
```rust use typedescription::AsTypeDescription; use typedescription::TypeDescription; use typedescription::TypeKind; use typedescription::Sign;
/// A configuration
struct Config { /// The bind address addr: std::net::SocketAddr,
/// The Port
port: u16,
}
let desc = Config::astypedescription();
asserteq!(desc.name(), "Config"); asserteq!(desc.doc(), Some("A configuration")); assert!(std::matches!(desc.kind(), TypeKind::Struct(_)));
match desc.kind() { TypeKind::Struct(v) => { let firstfield = &v[0]; asserteq!(firstfield.name(), "addr"); asserteq!(firstfield.doc(), Some("The bind address")); asserteq!(firstfield.kind().name(), "String"); asserteq!(firstfield.kind().doc(), Some("A socket address")); asserteq!(*firstfield.kind().kind(), typedescription::TypeKind::String);
let second_field = &v[1];
assert_eq!(second_field.name(), "port");
assert_eq!(second_field.doc(), Some("The Port"));
assert_eq!(second_field.kind().name(), "Integer");
assert_eq!(second_field.kind().doc(), Some("An unsigned integer with 16 bits"));
assert_eq!(*second_field.kind().kind(), type_description::TypeKind::Integer { size: 16, sign: Sign::Unsigned });
}
_ => unreachable!()
} ```
serde
model and be compatible with all
serde::{Deserialize, Serialize}
typesAsTypeDescription
TypeDescription
(serde::Deserialize
should be preferred for that)MPL-2.0