diff options
author | stuebinm | 2021-04-10 22:59:07 +0200 |
---|---|---|
committer | stuebinm | 2021-04-10 22:59:07 +0200 |
commit | 2958333c83dce911463734e391fe3ccc76cfc9d5 (patch) | |
tree | f8b6a6370222f6f1053061f684c27ead563b7c74 /dhall/src/lib.rs | |
parent | c307b914eca192f8e8648345e365f9c0207bc18c (diff) |
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::<SimpleType>()?
```
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
Diffstat (limited to 'dhall/src/lib.rs')
-rw-r--r-- | dhall/src/lib.rs | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index 4238b6e..f3a11a1 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -99,6 +99,11 @@ impl Parsed { pub fn to_expr(&self) -> Expr { self.0.clone() } + + pub fn substitute_name(self, label: syntax::Label, value: Expr) -> Parsed { + let Parsed (expr, import_location) = self; + Parsed (expr.substitute_name(label, value), import_location) + } } impl<'cx> Resolved<'cx> { |