Enum serde_dhall::SimpleType
source · pub enum SimpleType {
Bool,
Natural,
Integer,
Double,
Text,
Optional(Box<SimpleType>),
List(Box<SimpleType>),
Record(HashMap<String, SimpleType>),
Union(HashMap<String, Option<SimpleType>>),
}
Expand description
The type of a value that can be decoded by serde_dhall
, e.g. { x: Bool, y: List Natural }
.
A SimpleType
is used when deserializing values to ensure they are of the expected type.
Rather than letting serde
handle potential type mismatches, this uses the type-checking
capabilities of Dhall to catch errors early and cleanly indicate in the user’s code where the
mismatch happened.
You would typically not manipulate SimpleType
s by hand but rather let Rust infer it for your
datatype by deriving the StaticType
trait, and using
Deserializer::static_type_annotation
. If you need to supply a SimpleType
manually, you
can either deserialize it like any other Dhall value, or construct it manually.
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 |
Examples
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);
use std::collections::HashMap;
use serde_dhall::SimpleType;
let ty: SimpleType =
serde_dhall::from_str("{ x: Natural, y: Natural }").parse()?;
let mut map = HashMap::new();
map.insert("x".to_string(), SimpleType::Natural);
map.insert("y".to_string(), SimpleType::Natural);
assert_eq!(ty, SimpleType::Record(map));
Variants§
Bool
Corresponds to the Dhall type Bool
Natural
Corresponds to the Dhall type Natural
Integer
Corresponds to the Dhall type Integer
Double
Corresponds to the Dhall type Double
Text
Corresponds to the Dhall type Text
Optional(Box<SimpleType>)
Corresponds to the Dhall type Optional T
List(Box<SimpleType>)
Corresponds to the Dhall type List T
Record(HashMap<String, SimpleType>)
Corresponds to the Dhall type { x : T, y : U }
Union(HashMap<String, Option<SimpleType>>)
Corresponds to the Dhall type < x : T | y : U >
Trait Implementations§
source§impl Clone for SimpleType
impl Clone for SimpleType
source§fn clone(&self) -> SimpleType
fn clone(&self) -> SimpleType
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for SimpleType
impl Debug for SimpleType
source§impl Display for SimpleType
impl Display for SimpleType
source§impl PartialEq<SimpleType> for SimpleType
impl PartialEq<SimpleType> for SimpleType
source§fn eq(&self, other: &SimpleType) -> bool
fn eq(&self, other: &SimpleType) -> bool
self
and other
values to be equal, and is used
by ==
.impl Eq for SimpleType
impl FromDhall for SimpleType
impl StructuralEq for SimpleType
impl StructuralPartialEq for SimpleType
impl ToDhall for SimpleType
Auto Trait Implementations§
impl RefUnwindSafe for SimpleType
impl Send for SimpleType
impl Sync for SimpleType
impl Unpin for SimpleType
impl UnwindSafe for SimpleType
Blanket Implementations§
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.