summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstuebinm2021-04-10 23:28:29 +0200
committerstuebinm2021-04-10 23:28:29 +0200
commit9c3d04e84c8f22db4a616cf1a98bd90dc27819cb (patch)
tree16e283aa0e143bdf39b30b106e09da8bf6641bd6
parent2958333c83dce911463734e391fe3ccc76cfc9d5 (diff)
substitutions: add documentation
-rw-r--r--serde_dhall/src/options/de.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/serde_dhall/src/options/de.rs b/serde_dhall/src/options/de.rs
index a070af7..cf95496 100644
--- a/serde_dhall/src/options/de.rs
+++ b/serde_dhall/src/options/de.rs
@@ -228,6 +228,43 @@ impl<'a, A> Deserializer<'a, A> {
// self
// }
+ /// Sets a Collection of names which should be substituted with
+ /// the given types, i.e. effectively adds built-in type variables
+ /// which do not need to be imported within dhall.
+ ///
+ /// This is especially useful when deserialising into many nested
+ /// structs and enums at once, since it allows exposing the rust
+ /// types to dhall without having to redefine them in both languages
+ /// and manually keep both definitions in sync.
+ ///
+ /// # Example
+ /// ```
+ /// use serde::Deserialize;
+ /// use serde_dhall::StaticType;
+ /// use std::collections::HashMap;
+ ///
+ /// #[derive(Deserialize, StaticType, Debug, PartialEq)]
+ /// enum Newtype {
+ /// Foo,
+ /// Bar
+ /// }
+ ///
+ /// let mut substs = HashMap::new();
+ /// substs.insert(
+ /// "Newtype".to_string(),
+ /// Newtype::static_type()
+ /// );
+ ///
+ /// let data = "Newtype.Bar";
+ ///
+ /// let deserialized = serde_dhall::from_str(data)
+ /// .substitute_names(substs)
+ /// .parse::<Newtype>()
+ /// .unwrap();
+ ///
+ /// assert_eq!(deserialized, Newtype::Bar);
+ ///
+ /// ```
pub fn substitute_names(self, substs: HashMap<String, SimpleType>) -> Self {
Deserializer {
substitutions: substs