1use std::sync::Arc;
2use std::rc::Rc;
3
4use crate::ast::{SpannedNode, Literal, Value, TypeName};
5use crate::decode::Context;
6use crate::errors::DecodeError;
7use crate::span::Spanned;
8use crate::traits::{Decode, DecodeChildren, DecodeScalar, DecodePartial};
9use crate::traits::{ErrorSpan, DecodeSpan, Span};
10
11
12impl<S: ErrorSpan, T: Decode<S>> Decode<S> for Box<T> {
13 fn decode_node(node: &SpannedNode<S>, ctx: &mut Context<S>)
14 -> Result<Self, DecodeError<S>>
15 {
16 Decode::decode_node(node, ctx).map(Box::new)
17 }
18}
19
20impl<S: ErrorSpan, T: DecodeChildren<S>> DecodeChildren<S> for Box<T> {
21 fn decode_children(nodes: &[SpannedNode<S>], ctx: &mut Context<S>)
22 -> Result<Self, DecodeError<S>>
23 {
24 DecodeChildren::decode_children(nodes, ctx).map(Box::new)
25 }
26}
27
28impl<S: ErrorSpan, T: DecodePartial<S>> DecodePartial<S> for Box<T> {
29 fn insert_child(&mut self, node: &SpannedNode<S>, ctx: &mut Context<S>)
30 -> Result<bool, DecodeError<S>>
31 {
32 (**self).insert_child(node, ctx)
33 }
34 fn insert_property(&mut self,
35 name: &Spanned<Box<str>, S>, value: &Value<S>,
36 ctx: &mut Context<S>)
37 -> Result<bool, DecodeError<S>>
38 {
39 (**self).insert_property(name, value, ctx)
40 }
41}
42
43impl<S: ErrorSpan, T: DecodeScalar<S>> DecodeScalar<S> for Box<T> {
44 fn type_check(type_name: &Option<Spanned<TypeName, S>>,
45 ctx: &mut Context<S>) {
46 T::type_check(type_name, ctx)
47 }
48 fn raw_decode(value: &Spanned<Literal, S>, ctx: &mut Context<S>)
49 -> Result<Self, DecodeError<S>>
50 {
51 DecodeScalar::raw_decode(value, ctx).map(Box::new)
52 }
53}
54
55impl<S: ErrorSpan, T: Decode<S>> Decode<S> for Arc<T> {
56 fn decode_node(node: &SpannedNode<S>, ctx: &mut Context<S>)
57 -> Result<Self, DecodeError<S>>
58 {
59 Decode::decode_node(node, ctx).map(Arc::new)
60 }
61}
62
63impl<S: ErrorSpan, T: DecodeChildren<S>> DecodeChildren<S> for Arc<T> {
64 fn decode_children(nodes: &[SpannedNode<S>], ctx: &mut Context<S>)
65 -> Result<Self, DecodeError<S>>
66 {
67 DecodeChildren::decode_children(nodes, ctx).map(Arc::new)
68 }
69}
70
71impl<S: ErrorSpan, T: DecodePartial<S>> DecodePartial<S> for Arc<T> {
72 fn insert_child(&mut self, node: &SpannedNode<S>, ctx: &mut Context<S>)
73 -> Result<bool, DecodeError<S>>
74 {
75 Arc::get_mut(self).expect("no Arc clone yet")
76 .insert_child(node, ctx)
77 }
78 fn insert_property(&mut self,
79 name: &Spanned<Box<str>, S>, value: &Value<S>,
80 ctx: &mut Context<S>)
81 -> Result<bool, DecodeError<S>>
82 {
83 Arc::get_mut(self).expect("no Arc clone yet")
84 .insert_property(name, value, ctx)
85 }
86}
87
88impl<S: ErrorSpan, T: DecodeScalar<S>> DecodeScalar<S> for Arc<T> {
89 fn type_check(type_name: &Option<Spanned<TypeName, S>>,
90 ctx: &mut Context<S>)
91 {
92 T::type_check(type_name, ctx)
93 }
94 fn raw_decode(value: &Spanned<Literal, S>, ctx: &mut Context<S>)
95 -> Result<Self, DecodeError<S>>
96 {
97 DecodeScalar::raw_decode(value, ctx).map(Arc::new)
98 }
99}
100
101impl<S: ErrorSpan, T: Decode<S>> Decode<S> for Rc<T> {
102 fn decode_node(node: &SpannedNode<S>, ctx: &mut Context<S>)
103 -> Result<Self, DecodeError<S>>
104 {
105 Decode::decode_node(node, ctx).map(Rc::new)
106 }
107}
108
109impl<S: ErrorSpan, T: DecodeChildren<S>> DecodeChildren<S> for Rc<T> {
110 fn decode_children(nodes: &[SpannedNode<S>], ctx: &mut Context<S>)
111 -> Result<Self, DecodeError<S>>
112 {
113 DecodeChildren::decode_children(nodes, ctx).map(Rc::new)
114 }
115}
116
117impl<S: ErrorSpan, T: DecodePartial<S>> DecodePartial<S> for Rc<T> {
118 fn insert_child(&mut self, node: &SpannedNode<S>, ctx: &mut Context<S>)
119 -> Result<bool, DecodeError<S>>
120 {
121 Rc::get_mut(self).expect("no Rc clone yet")
122 .insert_child(node, ctx)
123 }
124 fn insert_property(&mut self,
125 name: &Spanned<Box<str>, S>, value: &Value<S>,
126 ctx: &mut Context<S>)
127 -> Result<bool, DecodeError<S>>
128 {
129 Rc::get_mut(self).expect("no Rc clone yet")
130 .insert_property(name, value, ctx)
131 }
132}
133
134impl<S: ErrorSpan, T: DecodeScalar<S>> DecodeScalar<S> for Rc<T> {
135 fn type_check(type_name: &Option<Spanned<TypeName, S>>,
136 ctx: &mut Context<S>)
137 {
138 T::type_check(type_name, ctx)
139 }
140 fn raw_decode(value: &Spanned<Literal, S>, ctx: &mut Context<S>)
141 -> Result<Self, DecodeError<S>>
142 {
143 DecodeScalar::raw_decode(value, ctx).map(Rc::new)
144 }
145}
146
147impl<S: ErrorSpan, T: Decode<S>> DecodeChildren<S> for Vec<T> {
148 fn decode_children(nodes: &[SpannedNode<S>], ctx: &mut Context<S>)
149 -> Result<Self, DecodeError<S>>
150 {
151 let mut result = Vec::with_capacity(nodes.len());
152 for node in nodes {
153 match Decode::decode_node(node, ctx) {
154 Ok(node) => result.push(node),
155 Err(e) => ctx.emit_error(e),
156 }
157 }
158 Ok(result)
159 }
160}
161
162impl<S: ErrorSpan, T: DecodeScalar<S>> DecodeScalar<S> for Option<T> {
163 fn type_check(type_name: &Option<Spanned<TypeName, S>>,
164 ctx: &mut Context<S>) {
165 T::type_check(type_name, ctx)
166 }
167 fn raw_decode(value: &Spanned<Literal, S>, ctx: &mut Context<S>)
168 -> Result<Self, DecodeError<S>>
169 {
170 match &**value {
171 Literal::Null => Ok(None),
172 _ => DecodeScalar::raw_decode(value, ctx).map(Some),
173 }
174 }
175}
176
177impl<T: DecodeScalar<S>, S, Q> DecodeScalar<S> for Spanned<T, Q>
178 where S: Span,
179 Q: DecodeSpan<S>
180{
181 fn type_check(type_name: &Option<Spanned<TypeName, S>>,
182 ctx: &mut Context<S>)
183 {
184 T::type_check(type_name, ctx)
185 }
186 fn raw_decode(value: &Spanned<Literal, S>, ctx: &mut Context<S>)
187 -> Result<Self, DecodeError<S>>
188 {
189 let decoded = T::raw_decode(value, ctx)?;
190 Ok(Spanned {
191 span: DecodeSpan::decode_span(&value.span, ctx),
192 value: decoded,
193 })
194 }
195}