[][src]Trait serde_dhall::StaticType

pub trait StaticType {
    fn static_type() -> SimpleType;
}

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:

DhallRust
Boolbool
Naturalu64, u32, ...
Integeri64, i32, ...
Doublef64, f32, ...
TextString
List TVec<T>
Optional TOption<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 THashMap<String, T>, structs
T -> Uunsupported
Prelude.JSON.Typeunsupported
Prelude.Map.Type T Uunsupported

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]);
Loading content...

Implementations on Foreign Types

impl StaticType for bool[src]

impl StaticType for usize[src]

impl StaticType for u64[src]

impl StaticType for u32[src]

impl StaticType for isize[src]

impl StaticType for i64[src]

impl StaticType for i32[src]

impl StaticType for f64[src]

impl StaticType for f32[src]

impl StaticType for String[src]

impl<A, B> StaticType for (A, B) where
    A: StaticType,
    B: StaticType
[src]

impl<T, E> StaticType for Result<T, E> where
    T: StaticType,
    E: StaticType
[src]

impl<T> StaticType for Option<T> where
    T: StaticType
[src]

impl<T> StaticType for Vec<T> where
    T: StaticType
[src]

impl<'a, T> StaticType for &'a T where
    T: StaticType
[src]

Loading content...

Implementors

Loading content...