Make it so that any defined language can be used as placeholder to identity-(un)parse `NestedValue`s

This commit is contained in:
Tony Garnock-Jones 2021-09-20 15:38:21 +02:00
parent 9936ddb29d
commit b1ed29657e
2 changed files with 10 additions and 4 deletions

View File

@ -64,6 +64,9 @@ macro_rules! define_language {
}
})*
impl<N: $crate::support::preserves::value::NestedValue> $crate::support::NestedValueCodec
for $lang<N> {}
mod $fname {
use super::*;
lazy_static::lazy_static! {

View File

@ -19,12 +19,15 @@ use std::sync::Arc;
use thiserror::Error;
pub trait NestedValueCodec {} // marker trait
impl NestedValueCodec for () {}
pub trait Parse<L, Value: NestedValue>: Sized {
fn parse(language: L, value: &Value) -> Result<Self, ParseError>;
}
impl<'a, Value: NestedValue> Parse<&'a (), Value> for Value {
fn parse(_language: &'a (), value: &Value) -> Result<Self, ParseError> {
impl<'a, T: NestedValueCodec, Value: NestedValue> Parse<&'a T, Value> for Value {
fn parse(_language: &'a T, value: &Value) -> Result<Self, ParseError> {
Ok(value.clone())
}
}
@ -33,8 +36,8 @@ pub trait Unparse<L, Value: NestedValue> {
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()
}
}