From cd31d3eb311024153ccf1f8187126ad351963456 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 16 Oct 2020 02:18:35 +0100 Subject: feat: Add a `Display` impl for `SimpleType` Fixes https://github.com/Nadrieril/dhall-rust/issues/179 --- CHANGELOG.md | 2 ++ serde_dhall/src/value.rs | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b6388f..f976b59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### [Unreleased] +- Add a `Display` impl for `SimpleType` (https://github.com/Nadrieril/dhall-rust/issues/179) + #### [0.7.0] - 2020-09-15 - BREAKING CHANGE: Support Dhall v18.0.0 diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index 0f0b256..b26985b 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -290,6 +290,11 @@ impl SimpleType { ), }) } + + /// Converts back to the corresponding AST expression. + pub(crate) fn to_expr(&self) -> Expr { + self.to_hir().to_expr(Default::default()) + } } impl Sealed for Value {} @@ -336,3 +341,19 @@ impl std::fmt::Display for Value { self.to_expr().fmt(f) } } + +impl std::fmt::Display for SimpleType { + fn fmt( + &self, + f: &mut std::fmt::Formatter, + ) -> std::result::Result<(), std::fmt::Error> { + self.to_expr().fmt(f) + } +} + +#[test] +fn test_display_simpletype() { + use SimpleType::*; + let ty = List(Box::new(Optional(Box::new(Natural)))); + assert_eq!(ty.to_string(), "List (Optional Natural)".to_string()) +} -- cgit v1.2.3