1use std::collections::BTreeMap;
16use std::convert::Infallible;
17use std::fmt;
18use std::str::FromStr;
19
20use crate::span::Spanned;
21
22pub type SpannedChildren<S> = Spanned<Vec<SpannedNode<S>>, S>;
24pub type SpannedName<S> = Spanned<Box<str>, S>;
26pub type SpannedNode<S> = Spanned<Node<S>, S>;
28
29#[derive(Debug, Clone)]
31#[cfg_attr(feature="minicbor", derive(minicbor::Encode, minicbor::Decode))]
32pub struct Node<S> {
33 #[cfg_attr(feature="minicbor", n(0))]
35 pub type_name: Option<Spanned<TypeName, S>>,
36 #[cfg_attr(feature="minicbor", n(1))]
38 pub node_name: SpannedName<S>,
39 #[cfg_attr(feature="minicbor", n(2))]
41 pub arguments: Vec<Value<S>>,
42 #[cfg_attr(feature="minicbor", n(3))]
44 pub properties: BTreeMap<SpannedName<S>, Value<S>>,
45 #[cfg_attr(feature="minicbor", n(4))]
47 pub children: Option<SpannedChildren<S>>,
48}
49
50#[derive(Debug, Clone)]
52#[cfg_attr(feature="minicbor", derive(minicbor::Encode, minicbor::Decode))]
53pub struct Document<S> {
54 #[cfg_attr(feature="minicbor", n(0))]
56 pub nodes: Vec<SpannedNode<S>>,
57}
58
59#[derive(Debug, Clone, PartialEq, Eq)]
60#[cfg_attr(feature="minicbor", derive(minicbor::Encode, minicbor::Decode))]
61#[cfg_attr(feature="minicbor", cbor(index_only))]
62pub(crate) enum Radix {
63 #[cfg_attr(feature="minicbor", n(2))]
64 Bin,
65 #[cfg_attr(feature="minicbor", n(16))]
66 Hex,
67 #[cfg_attr(feature="minicbor", n(8))]
68 Oct,
69 #[cfg_attr(feature="minicbor", n(10))]
70 Dec,
71}
72
73#[derive(Debug, Clone, PartialEq, Eq)]
75#[cfg_attr(feature="minicbor", derive(minicbor::Encode, minicbor::Decode))]
76pub struct Integer(
77 #[cfg_attr(feature="minicbor", n(0))]
78 pub(crate) Radix,
79 #[cfg_attr(feature="minicbor", n(1))]
80 pub(crate) Box<str>,
81);
82
83#[derive(Debug, Clone, PartialEq, Eq)]
85#[cfg_attr(feature="minicbor", derive(minicbor::Encode, minicbor::Decode))]
86#[cfg_attr(feature="minicbor", cbor(transparent))]
87pub struct Decimal(
88 #[cfg_attr(feature="minicbor", n(0))]
89 pub(crate) Box<str>,
90);
91
92#[derive(Debug, Clone)]
94#[cfg_attr(feature="minicbor", derive(minicbor::Encode, minicbor::Decode))]
95pub struct Value<S> {
96 #[cfg_attr(feature="minicbor", n(0))]
98 pub type_name: Option<Spanned<TypeName, S>>,
99 #[cfg_attr(feature="minicbor", n(1))]
101 pub literal: Spanned<Literal, S>,
102}
103
104#[derive(Debug, Clone, PartialEq, Eq)]
106pub struct TypeName(TypeNameInner);
107
108#[derive(Debug, Clone, PartialEq, Eq)]
109enum TypeNameInner {
110 Builtin(BuiltinType),
111 Custom(Box<str>),
112}
113
114#[non_exhaustive]
116#[derive(Debug, Clone, PartialEq, Eq)]
117pub enum BuiltinType {
118 U8,
120 I8,
122 U16,
124 I16,
126 U32,
128 I32,
130 U64,
132 I64,
134 Usize,
136 Isize,
138 F32,
140 F64,
142 Base64,
144}
145
146#[derive(Debug, Clone, PartialEq, Eq)]
148#[cfg_attr(feature="minicbor", derive(minicbor::Encode, minicbor::Decode))]
149pub enum Literal {
150 #[cfg_attr(feature="minicbor", n(0))]
152 Null,
153 #[cfg_attr(feature="minicbor", n(1))]
155 Bool(
156 #[cfg_attr(feature="minicbor", n(0))]
157 bool
158 ),
159 #[cfg_attr(feature="minicbor", n(2))]
161 Int(
162 #[cfg_attr(feature="minicbor", n(0))]
163 Integer
164 ),
165 #[cfg_attr(feature="minicbor", n(3))]
167 Decimal(
168 #[cfg_attr(feature="minicbor", n(0))]
169 Decimal
170 ),
171 #[cfg_attr(feature="minicbor", n(4))]
173 String(
174 #[cfg_attr(feature="minicbor", n(0))]
175 Box<str>
176 ),
177}
178
179impl<S> Node<S> {
180 pub fn children(&self)
182 -> impl Iterator<Item=&Spanned<Node<S>, S>> +
183 ExactSizeIterator
184 {
185 self.children.as_ref().map(|c| c.iter()).unwrap_or_else(|| [].iter())
186 }
187}
188
189impl BuiltinType {
190 pub const fn as_str(&self) -> &'static str {
193 use BuiltinType::*;
194 match self {
195 U8 => "u8",
196 I8 => "i8",
197 U16 => "u16",
198 I16 => "i16",
199 U32 => "u32",
200 I32 => "i32",
201 U64 => "u64",
202 I64 => "i64",
203 Usize => "usize",
204 Isize => "isize",
205 F32 => "f32",
206 F64 => "f64",
207 Base64 => "base64",
208 }
209 }
210 pub const fn as_type(self) -> TypeName {
212 TypeName(TypeNameInner::Builtin(self))
213 }
214}
215
216impl TypeName {
217 pub(crate) fn from_string(val: Box<str>) -> TypeName {
218 use TypeNameInner::*;
219
220 match BuiltinType::from_str(&val[..]) {
221 Ok(b) => TypeName(Builtin(b)),
222 _ => TypeName(Custom(val)),
223 }
224 }
225 pub fn as_str(&self) -> &str {
227 match &self.0 {
228 TypeNameInner::Builtin(t) => t.as_str(),
229 TypeNameInner::Custom(t) => t.as_ref(),
230 }
231 }
232 pub const fn as_builtin(&self) -> Option<&BuiltinType> {
239 match &self.0 {
240 TypeNameInner::Builtin(t) => Some(t),
241 TypeNameInner::Custom(_) => None,
242 }
243 }
244}
245
246impl FromStr for BuiltinType {
247 type Err = ();
248 fn from_str(s: &str) -> Result<BuiltinType, ()> {
249 use BuiltinType::*;
250 match s {
251 "u8" => Ok(U8),
252 "i8" => Ok(I8),
253 "u16" => Ok(U16),
254 "i16" => Ok(I16),
255 "u32" => Ok(U32),
256 "i32" => Ok(I32),
257 "u64" => Ok(U64),
258 "i64" => Ok(I64),
259 "f32" => Ok(F32),
260 "f64" => Ok(F64),
261 "base64" => Ok(Base64),
262 _ => Err(())
263 }
264 }
265}
266
267impl FromStr for TypeName {
268 type Err = Infallible;
269 fn from_str(s: &str) -> Result<TypeName, Infallible> {
270 use TypeNameInner::*;
271 match BuiltinType::from_str(s) {
272 Ok(b) => Ok(TypeName(Builtin(b))),
273 Err(()) => Ok(TypeName(Custom(s.into()))),
274 }
275 }
276}
277
278impl std::ops::Deref for TypeName {
279 type Target = str;
280 fn deref(&self) -> &str {
281 self.as_str()
282 }
283}
284
285impl fmt::Display for TypeName {
286 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
287 self.as_str().fmt(f)
288 }
289}
290
291impl Into<TypeName> for BuiltinType {
292 fn into(self) -> TypeName {
293 self.as_type()
294 }
295}
296
297#[cfg(feature="minicbor")]
298mod cbor {
299 use super::TypeName;
300 use minicbor::{Decoder, Encoder};
301 use minicbor::encode::Encode;
302 use minicbor::decode::Decode;
303
304 impl<'d, C> Decode<'d, C> for TypeName {
305 fn decode(d: &mut Decoder<'d>, _ctx: &mut C)
306 -> Result<Self, minicbor::decode::Error>
307 {
308 d.str().and_then(|s| s.parse().map_err(|e| match e {}))
309 }
310 }
311 impl<C> Encode<C> for TypeName {
312 fn encode<W>(&self, e: &mut Encoder<W>, ctx: &mut C)
313 -> Result<(), minicbor::encode::Error<W::Error>>
314 where W: minicbor::encode::write::Write
315 {
316 self.as_str().encode(e, ctx)
317 }
318 }
319}