[−][src]Trait serde_dhall::StaticType
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 }
.
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);
Type correspondence
The following Dhall types correspond to the following Rust types:
Dhall | Rust |
---|---|
Bool | bool |
Natural | u64 , u32 , ... |
Integer | i64 , i32 , ... |
Double | f64 , f32 , ... |
Text | String |
List T | Vec<T> |
Optional T | Option<T> |
{ x: T, y: U } | structs |
{ _1: T, _2: U } | (T, U) , structs |
{ x: T, y: T } | HashMap<String, T> , structs |
< x: T \| y: U > | enums |
Prelude.Map.Type Text T | HashMap<String, T> , structs |
T -> U | unsupported |
Prelude.JSON.Type | unsupported |
Prelude.Map.Type T U | unsupported |
Required methods
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]);
Implementations on Foreign Types
impl StaticType for bool
[src]
fn static_type() -> SimpleType
[src]
impl StaticType for usize
[src]
fn static_type() -> SimpleType
[src]
impl StaticType for u64
[src]
fn static_type() -> SimpleType
[src]
impl StaticType for u32
[src]
fn static_type() -> SimpleType
[src]
impl StaticType for isize
[src]
fn static_type() -> SimpleType
[src]
impl StaticType for i64
[src]
fn static_type() -> SimpleType
[src]
impl StaticType for i32
[src]
fn static_type() -> SimpleType
[src]
impl StaticType for f64
[src]
fn static_type() -> SimpleType
[src]
impl StaticType for f32
[src]
fn static_type() -> SimpleType
[src]
impl StaticType for String
[src]
fn static_type() -> SimpleType
[src]
impl<A, B> StaticType for (A, B) where
A: StaticType,
B: StaticType,
[src]
A: StaticType,
B: StaticType,
fn static_type() -> SimpleType
[src]
impl<T, E> StaticType for Result<T, E> where
T: StaticType,
E: StaticType,
[src]
T: StaticType,
E: StaticType,
fn static_type() -> SimpleType
[src]
impl<T> StaticType for Option<T> where
T: StaticType,
[src]
T: StaticType,
fn static_type() -> SimpleType
[src]
impl<T> StaticType for Vec<T> where
T: StaticType,
[src]
T: StaticType,
fn static_type() -> SimpleType
[src]
impl<'a, T> StaticType for &'a T where
T: StaticType,
[src]
T: StaticType,