Trait serde_dhall::StaticType
source · pub trait StaticType {
fn static_type() -> SimpleType;
}
Expand description
A Rust type that can be represented as a Dhall type.
A typical example is Option<bool>
, represented by the Dhall expression Optional Bool
.
This trait can be automatically derived, and this is the recommended way of implementing it.
Some Rust types cannot implement this trait, because there isn’t a single Dhall type that
corresponds to them. For example, HashMap<String, u64>
could correspond to multiple different
Dhall types, e.g. { foo: Natural, bar: Natural }
and { baz: Natural }
.
See also the table of type correspondances.
Example
use serde_dhall::{SimpleType, StaticType};
#[derive(StaticType)]
struct Foo {
x: bool,
y: Vec<u64>,
}
let ty: SimpleType =
serde_dhall::from_str("{ x: Bool, y: List Natural }").parse()?;
assert_eq!(Foo::static_type(), ty);
Required Methods§
sourcefn static_type() -> SimpleType
fn static_type() -> SimpleType
Return the Dhall type that represents this type.
Example
use serde::Deserialize;
use serde_dhall::{SimpleType, StaticType};
// Using `derive(StaticType)` here would give it the type `{ _1: List Natural }`.
#[derive(Deserialize)]
#[serde(transparent)]
struct Foo(Vec<u64>);
impl StaticType for Foo {
fn static_type() -> SimpleType {
SimpleType::List(Box::new(SimpleType::Natural))
}
}
let foo = serde_dhall::from_str("[ 1, 2 ]")
.static_type_annotation()
.parse::<Foo>()?;
assert_eq!(foo.0, vec![1, 2]);