Struct serde_dhall::Serializer
source · [−]pub struct Serializer<'a, T, A> { /* private fields */ }
Expand description
Controls how a Dhall value is written.
This builder exposes the ability to configure how a value is serialized, and to set type annotations.
When using Serializer
, you’ll create it with serialize()
, then chain calls to methods
to set each option, then call to_string()
. This will give you a Result
containing the
input serialized to Dhall.
Note that if you do not provide a type annotation, some values may not be convertible to Dhall, like empty lists or enums.
Examples
Serializing without a type annotation:
use serde_dhall::serialize;
let string = serialize(&1i64).to_string()?;
assert_eq!(string, "+1".to_string());
Serializing with an automatic type annotation:
use serde_dhall::serialize;
let data: Option<u64> = None;
let string = serialize(&data).static_type_annotation().to_string()?;
assert_eq!(string, "None Natural".to_string());
Implementations
sourceimpl<'a, T> Serializer<'a, T, NoAnnot>
impl<'a, T> Serializer<'a, T, NoAnnot>
sourcepub fn type_annotation<'ty>(
self,
ty: &'ty SimpleType
) -> Serializer<'a, T, ManualAnnot<'ty>>
pub fn type_annotation<'ty>(
self,
ty: &'ty SimpleType
) -> Serializer<'a, T, ManualAnnot<'ty>>
Provides a type to the serialization process. The provided value will be checked against that type, and the type will be used when Dhall needs it, like for empty lists or for unions.
In many cases the Dhall type that corresponds to a Rust type can be inferred automatically.
See the StaticType
trait and the static_type_annotation()
method for that.
Example
use serde_dhall::{serialize, from_str, SimpleType, SimpleValue};
let ty = from_str("< A | B: Bool >").parse()?;
let data = SimpleValue::Union("A".to_string(), None);
let string = serialize(&data)
.type_annotation(&ty)
.to_string()?;
assert_eq!(string, "< A | B: Bool >.A".to_string());
// Invalid data fails the type validation; serialization would have succeeded otherwise.
let ty = SimpleType::Integer;
assert!(
serialize(&Some(0u64))
.type_annotation(&ty)
.to_string()
.is_err()
);
sourcepub fn static_type_annotation(self) -> Serializer<'a, T, StaticAnnot>
pub fn static_type_annotation(self) -> Serializer<'a, T, StaticAnnot>
Uses the type of T
in the serialization process. This will be used when Dhall needs it,
like for empty lists or for unions.
T
must implement the StaticType
trait. If it doesn’t, you can use type_annotation()
Example
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());
sourceimpl<'a, T, A> Serializer<'a, T, A> where
A: TypeAnnot,
impl<'a, T, A> Serializer<'a, T, A> where
A: TypeAnnot,
sourcepub fn to_string(&self) -> Result<String> where
T: ToDhall + HasAnnot<A>,
pub fn to_string(&self) -> Result<String> where
T: ToDhall + HasAnnot<A>,
Prints the chosen value with the options provided.
If you enabled static annotations, T
is required to implement StaticType
.
Note that if you do not provide a type annotation, some values may not be convertible to Dhall, like empty lists or enums.
Example
use serde_dhall::serialize;
let string = serialize(&1i64).static_type_annotation().to_string()?;
assert_eq!(string, "+1".to_string());
Trait Implementations
sourceimpl<'a, T: Clone, A: Clone> Clone for Serializer<'a, T, A>
impl<'a, T: Clone, A: Clone> Clone for Serializer<'a, T, A>
sourcefn clone(&self) -> Serializer<'a, T, A>
fn clone(&self) -> Serializer<'a, T, A>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
Auto Trait Implementations
impl<'a, T, A> RefUnwindSafe for Serializer<'a, T, A> where
A: RefUnwindSafe,
T: RefUnwindSafe,
impl<'a, T, A> Send for Serializer<'a, T, A> where
A: Send,
T: Sync,
impl<'a, T, A> Sync for Serializer<'a, T, A> where
A: Sync,
T: Sync,
impl<'a, T, A> Unpin for Serializer<'a, T, A> where
A: Unpin,
impl<'a, T, A> UnwindSafe for Serializer<'a, T, A> where
A: UnwindSafe,
T: RefUnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more