Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 45 additions & 18 deletions src/containers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@ use crate::traits::{ErrorSpan, DecodeSpan, Span};

impl<S: ErrorSpan, T: Decode<S>> Decode<S> for Box<T> {
fn decode_node(node: &SpannedNode<S>, ctx: &mut Context<S>)
-> Result<Self, DecodeError<S>>
-> Result<Self, DecodeError<S>>
{
Decode::decode_node(node, ctx).map(Box::new)
}
}

impl<S: ErrorSpan, T: DecodeChildren<S>> DecodeChildren<S> for Box<T> {
fn decode_children(nodes: &[SpannedNode<S>], ctx: &mut Context<S>)
-> Result<Self, DecodeError<S>>
-> Result<Self, DecodeError<S>>
{
DecodeChildren::decode_children(nodes, ctx).map(Box::new)
}
}

impl<S: ErrorSpan, T: DecodePartial<S>> DecodePartial<S> for Box<T> {
fn insert_child(&mut self, node: &SpannedNode<S>, ctx: &mut Context<S>)
-> Result<bool, DecodeError<S>>
-> Result<bool, DecodeError<S>>
{
(**self).insert_child(node, ctx)
}
fn insert_property(&mut self,
name: &Spanned<Box<str>, S>, value: &Value<S>,
ctx: &mut Context<S>)
-> Result<bool, DecodeError<S>>
-> Result<bool, DecodeError<S>>
{
(**self).insert_property(name, value, ctx)
}
Expand All @@ -46,39 +46,43 @@ impl<S: ErrorSpan, T: DecodeScalar<S>> DecodeScalar<S> for Box<T> {
T::type_check(type_name, ctx)
}
fn raw_decode(value: &Spanned<Literal, S>, ctx: &mut Context<S>)
-> Result<Self, DecodeError<S>>
-> Result<Self, DecodeError<S>>
{
DecodeScalar::raw_decode(value, ctx).map(Box::new)
}

fn decode(value: &Value<S>, ctx: &mut Context<S>) -> Result<Self, DecodeError<S>> {
T::decode(value, ctx).map(Box::new)
}
}

impl<S: ErrorSpan, T: Decode<S>> Decode<S> for Arc<T> {
fn decode_node(node: &SpannedNode<S>, ctx: &mut Context<S>)
-> Result<Self, DecodeError<S>>
-> Result<Self, DecodeError<S>>
{
Decode::decode_node(node, ctx).map(Arc::new)
}
}

impl<S: ErrorSpan, T: DecodeChildren<S>> DecodeChildren<S> for Arc<T> {
fn decode_children(nodes: &[SpannedNode<S>], ctx: &mut Context<S>)
-> Result<Self, DecodeError<S>>
-> Result<Self, DecodeError<S>>
{
DecodeChildren::decode_children(nodes, ctx).map(Arc::new)
}
}

impl<S: ErrorSpan, T: DecodePartial<S>> DecodePartial<S> for Arc<T> {
fn insert_child(&mut self, node: &SpannedNode<S>, ctx: &mut Context<S>)
-> Result<bool, DecodeError<S>>
-> Result<bool, DecodeError<S>>
{
Arc::get_mut(self).expect("no Arc clone yet")
.insert_child(node, ctx)
}
fn insert_property(&mut self,
name: &Spanned<Box<str>, S>, value: &Value<S>,
ctx: &mut Context<S>)
-> Result<bool, DecodeError<S>>
-> Result<bool, DecodeError<S>>
{
Arc::get_mut(self).expect("no Arc clone yet")
.insert_property(name, value, ctx)
Expand All @@ -92,39 +96,43 @@ impl<S: ErrorSpan, T: DecodeScalar<S>> DecodeScalar<S> for Arc<T> {
T::type_check(type_name, ctx)
}
fn raw_decode(value: &Spanned<Literal, S>, ctx: &mut Context<S>)
-> Result<Self, DecodeError<S>>
-> Result<Self, DecodeError<S>>
{
DecodeScalar::raw_decode(value, ctx).map(Arc::new)
}

fn decode(value: &Value<S>, ctx: &mut Context<S>) -> Result<Self, DecodeError<S>> {
T::decode(value, ctx).map(Arc::new)
}
}

impl<S: ErrorSpan, T: Decode<S>> Decode<S> for Rc<T> {
fn decode_node(node: &SpannedNode<S>, ctx: &mut Context<S>)
-> Result<Self, DecodeError<S>>
-> Result<Self, DecodeError<S>>
{
Decode::decode_node(node, ctx).map(Rc::new)
}
}

impl<S: ErrorSpan, T: DecodeChildren<S>> DecodeChildren<S> for Rc<T> {
fn decode_children(nodes: &[SpannedNode<S>], ctx: &mut Context<S>)
-> Result<Self, DecodeError<S>>
-> Result<Self, DecodeError<S>>
{
DecodeChildren::decode_children(nodes, ctx).map(Rc::new)
}
}

impl<S: ErrorSpan, T: DecodePartial<S>> DecodePartial<S> for Rc<T> {
fn insert_child(&mut self, node: &SpannedNode<S>, ctx: &mut Context<S>)
-> Result<bool, DecodeError<S>>
-> Result<bool, DecodeError<S>>
{
Rc::get_mut(self).expect("no Rc clone yet")
.insert_child(node, ctx)
}
fn insert_property(&mut self,
name: &Spanned<Box<str>, S>, value: &Value<S>,
ctx: &mut Context<S>)
-> Result<bool, DecodeError<S>>
-> Result<bool, DecodeError<S>>
{
Rc::get_mut(self).expect("no Rc clone yet")
.insert_property(name, value, ctx)
Expand All @@ -138,15 +146,19 @@ impl<S: ErrorSpan, T: DecodeScalar<S>> DecodeScalar<S> for Rc<T> {
T::type_check(type_name, ctx)
}
fn raw_decode(value: &Spanned<Literal, S>, ctx: &mut Context<S>)
-> Result<Self, DecodeError<S>>
-> Result<Self, DecodeError<S>>
{
DecodeScalar::raw_decode(value, ctx).map(Rc::new)
}

fn decode(value: &Value<S>, ctx: &mut Context<S>) -> Result<Self, DecodeError<S>> {
T::decode(value, ctx).map(Rc::new)
}
}

impl<S: ErrorSpan, T: Decode<S>> DecodeChildren<S> for Vec<T> {
fn decode_children(nodes: &[SpannedNode<S>], ctx: &mut Context<S>)
-> Result<Self, DecodeError<S>>
-> Result<Self, DecodeError<S>>
{
let mut result = Vec::with_capacity(nodes.len());
for node in nodes {
Expand All @@ -165,13 +177,20 @@ impl<S: ErrorSpan, T: DecodeScalar<S>> DecodeScalar<S> for Option<T> {
T::type_check(type_name, ctx)
}
fn raw_decode(value: &Spanned<Literal, S>, ctx: &mut Context<S>)
-> Result<Self, DecodeError<S>>
-> Result<Self, DecodeError<S>>
{
match &**value {
Literal::Null => Ok(None),
_ => DecodeScalar::raw_decode(value, ctx).map(Some),
}
}

fn decode(value: &Value<S>, ctx: &mut Context<S>) -> Result<Self, DecodeError<S>> {
match &*value.literal {
Literal::Null => Ok(None),
_ => T::decode(value, ctx).map(Some)
}
}
}

impl<T: DecodeScalar<S>, S, Q> DecodeScalar<S> for Spanned<T, Q>
Expand All @@ -184,12 +203,20 @@ impl<T: DecodeScalar<S>, S, Q> DecodeScalar<S> for Spanned<T, Q>
T::type_check(type_name, ctx)
}
fn raw_decode(value: &Spanned<Literal, S>, ctx: &mut Context<S>)
-> Result<Self, DecodeError<S>>
-> Result<Self, DecodeError<S>>
{
let decoded = T::raw_decode(value, ctx)?;
Ok(Spanned {
span: DecodeSpan::decode_span(&value.span, ctx),
value: decoded,
})
}

fn decode(value: &Value<S>, ctx: &mut Context<S>) -> Result<Self, DecodeError<S>> {
let decoded = T::decode(value, ctx)?;
Ok(Spanned {
span: DecodeSpan::decode_span(&value.literal.span, ctx),
value: decoded,
})
}
}