From 2958333c83dce911463734e391fe3ccc76cfc9d5 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Sat, 10 Apr 2021 22:59:07 +0200 Subject: add type substitutions to serde_dhall this adds subsititutions, which work similar to the `Dhall.substitutions` mechanism of the Haskell dhall package, and which can be used e.g. like this: ```rust serde_dhall.from_str(...) .substitute_names(hashmap!["Newtype" => static_type]) .parse::()? ``` The idea behind this is to make it easy to add programmer-defined types which may be used in configs for programs, without forcing the program's users to re-import the same type definitions each time (or the programmers to keep the dhall-based definitions in sync with their rust types, since these are now generated automatically). Caveats so far: - makes some of the code ugly (dhall internals are now used in serde_dhall/src/lib.rs, for example) - haven't tested error messages so far - some unecessary copying of strings and static type values --- dhall/src/syntax/ast/expr.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'dhall/src/syntax/ast') diff --git a/dhall/src/syntax/ast/expr.rs b/dhall/src/syntax/ast/expr.rs index eba2735..bc3ffa4 100644 --- a/dhall/src/syntax/ast/expr.rs +++ b/dhall/src/syntax/ast/expr.rs @@ -182,6 +182,19 @@ impl Expr { let data = binary::encode(self)?; Ok(crate::utils::sha256_hash(&data)) } + + /// this wraps the expression into an additional let-binding + pub fn substitute_name(self, label: Label, value: Expr) -> Expr { + Expr::new( + ExprKind::Let( + label, + None, + value, + self + ), + Span::Artificial + ) + } } // Empty enum to indicate that no error can occur -- cgit v1.2.3