Function serde_dhall::serialize [−][src]
pub fn serialize<T>(data: &T) -> Serializer<'_, T, NoAnnot> where
T: ToDhall,
Serialize a value to a string of Dhall text.
This returns a Serializer
object. Call the to_string()
method to get the serialized
value, or use other Serializer
methods to control the serialization process.
In order to process certain values (like unions or empty lists) correctly, it is necessary to
add a type annotation (with static_type_annotation()
or type_annotation()
).
Examples
use serde::Serialize; use serde_dhall::{serialize, StaticType}; #[derive(Serialize)] struct Point { x: u64, y: u64, } let data = Point { x: 0, y: 0 }; let string = serialize(&data).to_string()?; assert_eq!(string, "{ x = 0, y = 0 }");
use serde::Serialize; use serde_dhall::{serialize, StaticType}; #[derive(Serialize, StaticType)] enum MyOption { MyNone, MySome(u64), } let data = MyOption::MySome(0); let string = serialize(&data) .static_type_annotation() .to_string()?; // The resulting Dhall string depends on the type annotation; it could not have been // printed without it. assert_eq!(string, "< MyNone | MySome: Natural >.MySome 0".to_string());