From b1ed29657e9e934d2a08c820d521044ca3c5444a Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Mon, 20 Sep 2021 15:38:21 +0200 Subject: [PATCH] Make it so that any defined language can be used as placeholder to identity-(un)parse `NestedValue`s --- implementations/rust/preserves-schema/src/lib.rs | 3 +++ .../rust/preserves-schema/src/support/mod.rs | 11 +++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/implementations/rust/preserves-schema/src/lib.rs b/implementations/rust/preserves-schema/src/lib.rs index ec34461..39be38c 100644 --- a/implementations/rust/preserves-schema/src/lib.rs +++ b/implementations/rust/preserves-schema/src/lib.rs @@ -64,6 +64,9 @@ macro_rules! define_language { } })* + impl $crate::support::NestedValueCodec + for $lang {} + mod $fname { use super::*; lazy_static::lazy_static! { diff --git a/implementations/rust/preserves-schema/src/support/mod.rs b/implementations/rust/preserves-schema/src/support/mod.rs index e159574..e656671 100644 --- a/implementations/rust/preserves-schema/src/support/mod.rs +++ b/implementations/rust/preserves-schema/src/support/mod.rs @@ -19,12 +19,15 @@ use std::sync::Arc; use thiserror::Error; +pub trait NestedValueCodec {} // marker trait +impl NestedValueCodec for () {} + pub trait Parse: Sized { fn parse(language: L, value: &Value) -> Result; } -impl<'a, Value: NestedValue> Parse<&'a (), Value> for Value { - fn parse(_language: &'a (), value: &Value) -> Result { +impl<'a, T: NestedValueCodec, Value: NestedValue> Parse<&'a T, Value> for Value { + fn parse(_language: &'a T, value: &Value) -> Result { Ok(value.clone()) } } @@ -33,8 +36,8 @@ pub trait Unparse { fn unparse(&self, language: L) -> Value; } -impl<'a, Value: NestedValue> Unparse<&'a (), Value> for Value { - fn unparse(&self, _language: &'a ()) -> Value { +impl<'a, T: NestedValueCodec, Value: NestedValue> Unparse<&'a T, Value> for Value { + fn unparse(&self, _language: &'a T) -> Value { self.clone() } }