From 2973cad57b6e074b215daab909181378bfb97449 Mon Sep 17 00:00:00 2001 From: eater <=@eater.me> Date: Tue, 8 Aug 2023 08:33:20 +0200 Subject: [PATCH] let containers call DecodeScalar::decode when called, instead of relying on default implementation --- src/containers.rs | 63 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/src/containers.rs b/src/containers.rs index a09b023..c77146d 100644 --- a/src/containers.rs +++ b/src/containers.rs @@ -11,7 +11,7 @@ use crate::traits::{ErrorSpan, DecodeSpan, Span}; impl> Decode for Box { fn decode_node(node: &SpannedNode, ctx: &mut Context) - -> Result> + -> Result> { Decode::decode_node(node, ctx).map(Box::new) } @@ -19,7 +19,7 @@ impl> Decode for Box { impl> DecodeChildren for Box { fn decode_children(nodes: &[SpannedNode], ctx: &mut Context) - -> Result> + -> Result> { DecodeChildren::decode_children(nodes, ctx).map(Box::new) } @@ -27,14 +27,14 @@ impl> DecodeChildren for Box { impl> DecodePartial for Box { fn insert_child(&mut self, node: &SpannedNode, ctx: &mut Context) - -> Result> + -> Result> { (**self).insert_child(node, ctx) } fn insert_property(&mut self, name: &Spanned, S>, value: &Value, ctx: &mut Context) - -> Result> + -> Result> { (**self).insert_property(name, value, ctx) } @@ -46,15 +46,19 @@ impl> DecodeScalar for Box { T::type_check(type_name, ctx) } fn raw_decode(value: &Spanned, ctx: &mut Context) - -> Result> + -> Result> { DecodeScalar::raw_decode(value, ctx).map(Box::new) } + + fn decode(value: &Value, ctx: &mut Context) -> Result> { + T::decode(value, ctx).map(Box::new) + } } impl> Decode for Arc { fn decode_node(node: &SpannedNode, ctx: &mut Context) - -> Result> + -> Result> { Decode::decode_node(node, ctx).map(Arc::new) } @@ -62,7 +66,7 @@ impl> Decode for Arc { impl> DecodeChildren for Arc { fn decode_children(nodes: &[SpannedNode], ctx: &mut Context) - -> Result> + -> Result> { DecodeChildren::decode_children(nodes, ctx).map(Arc::new) } @@ -70,7 +74,7 @@ impl> DecodeChildren for Arc { impl> DecodePartial for Arc { fn insert_child(&mut self, node: &SpannedNode, ctx: &mut Context) - -> Result> + -> Result> { Arc::get_mut(self).expect("no Arc clone yet") .insert_child(node, ctx) @@ -78,7 +82,7 @@ impl> DecodePartial for Arc { fn insert_property(&mut self, name: &Spanned, S>, value: &Value, ctx: &mut Context) - -> Result> + -> Result> { Arc::get_mut(self).expect("no Arc clone yet") .insert_property(name, value, ctx) @@ -92,15 +96,19 @@ impl> DecodeScalar for Arc { T::type_check(type_name, ctx) } fn raw_decode(value: &Spanned, ctx: &mut Context) - -> Result> + -> Result> { DecodeScalar::raw_decode(value, ctx).map(Arc::new) } + + fn decode(value: &Value, ctx: &mut Context) -> Result> { + T::decode(value, ctx).map(Arc::new) + } } impl> Decode for Rc { fn decode_node(node: &SpannedNode, ctx: &mut Context) - -> Result> + -> Result> { Decode::decode_node(node, ctx).map(Rc::new) } @@ -108,7 +116,7 @@ impl> Decode for Rc { impl> DecodeChildren for Rc { fn decode_children(nodes: &[SpannedNode], ctx: &mut Context) - -> Result> + -> Result> { DecodeChildren::decode_children(nodes, ctx).map(Rc::new) } @@ -116,7 +124,7 @@ impl> DecodeChildren for Rc { impl> DecodePartial for Rc { fn insert_child(&mut self, node: &SpannedNode, ctx: &mut Context) - -> Result> + -> Result> { Rc::get_mut(self).expect("no Rc clone yet") .insert_child(node, ctx) @@ -124,7 +132,7 @@ impl> DecodePartial for Rc { fn insert_property(&mut self, name: &Spanned, S>, value: &Value, ctx: &mut Context) - -> Result> + -> Result> { Rc::get_mut(self).expect("no Rc clone yet") .insert_property(name, value, ctx) @@ -138,15 +146,19 @@ impl> DecodeScalar for Rc { T::type_check(type_name, ctx) } fn raw_decode(value: &Spanned, ctx: &mut Context) - -> Result> + -> Result> { DecodeScalar::raw_decode(value, ctx).map(Rc::new) } + + fn decode(value: &Value, ctx: &mut Context) -> Result> { + T::decode(value, ctx).map(Rc::new) + } } impl> DecodeChildren for Vec { fn decode_children(nodes: &[SpannedNode], ctx: &mut Context) - -> Result> + -> Result> { let mut result = Vec::with_capacity(nodes.len()); for node in nodes { @@ -165,13 +177,20 @@ impl> DecodeScalar for Option { T::type_check(type_name, ctx) } fn raw_decode(value: &Spanned, ctx: &mut Context) - -> Result> + -> Result> { match &**value { Literal::Null => Ok(None), _ => DecodeScalar::raw_decode(value, ctx).map(Some), } } + + fn decode(value: &Value, ctx: &mut Context) -> Result> { + match &*value.literal { + Literal::Null => Ok(None), + _ => T::decode(value, ctx).map(Some) + } + } } impl, S, Q> DecodeScalar for Spanned @@ -184,7 +203,7 @@ impl, S, Q> DecodeScalar for Spanned T::type_check(type_name, ctx) } fn raw_decode(value: &Spanned, ctx: &mut Context) - -> Result> + -> Result> { let decoded = T::raw_decode(value, ctx)?; Ok(Spanned { @@ -192,4 +211,12 @@ impl, S, Q> DecodeScalar for Spanned value: decoded, }) } + + fn decode(value: &Value, ctx: &mut Context) -> Result> { + let decoded = T::decode(value, ctx)?; + Ok(Spanned { + span: DecodeSpan::decode_span(&value.literal.span, ctx), + value: decoded, + }) + } }