-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta.rs
138 lines (120 loc) · 4.6 KB
/
meta.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use crate::{
model::{
Meta, MetaArray, MetaAttribute, MetaObject, MetaObjectField, MetaString, MetaStringPart,
MetaValue, ModelError, ParameterMeta,
},
parsers::tree_sitter::{
node::{BlockDelim, BlockEnds, TSNode},
syntax::{fields, keywords, rules, symbols},
},
};
use error_stack::{bail, Report, Result};
use std::convert::TryFrom;
impl<'a> TryFrom<TSNode<'a>> for MetaStringPart {
type Error = Report<ModelError>;
fn try_from(node: TSNode<'a>) -> Result<Self, ModelError> {
let part = match node.kind() {
rules::CONTENT => Self::Content(node.try_into()?),
rules::ESCAPE_SEQUENCE => Self::Escape(node.try_into()?),
_ => bail!(ModelError::parser(format!(
"Invalid meta string part {:?}",
node
))),
};
Ok(part)
}
}
impl<'a> TryFrom<TSNode<'a>> for MetaString {
type Error = Report<ModelError>;
fn try_from(node: TSNode<'a>) -> Result<Self, ModelError> {
let mut children = node.into_block(BlockEnds::Quotes, BlockDelim::None);
let parts = match children.get_next_field(fields::PARTS)? {
Some(parts) => parts.into_children().collect_anchors()?,
None => Vec::new(),
};
Ok(Self { parts })
}
}
impl<'a> TryFrom<TSNode<'a>> for MetaArray {
type Error = Report<ModelError>;
fn try_from(node: TSNode<'a>) -> Result<Self, ModelError> {
let mut children = node.into_children();
let elements = children
.next_field(fields::ELEMENTS)?
.into_block(BlockEnds::Brackets, BlockDelim::Comma)
.collect_anchors()?;
Ok(Self { elements })
}
}
impl<'a> TryFrom<TSNode<'a>> for MetaObjectField {
type Error = Report<ModelError>;
fn try_from(node: TSNode<'a>) -> Result<Self, ModelError> {
let mut children = node.into_children();
let name = children.next_field(fields::NAME).try_into()?;
children.skip_terminal(symbols::COLON)?;
let value = children.next_field(fields::VALUE).try_into()?;
Ok(Self { name, value })
}
}
impl<'a> TryFrom<TSNode<'a>> for MetaObject {
type Error = Report<ModelError>;
fn try_from(node: TSNode<'a>) -> Result<Self, ModelError> {
let mut children = node.into_children();
let fields = children
.next_field(fields::FIELDS)?
.into_block(BlockEnds::Braces, BlockDelim::Comma)
.collect_anchors()?;
Ok(Self { fields })
}
}
impl<'a> TryFrom<TSNode<'a>> for MetaValue {
type Error = Report<ModelError>;
fn try_from(node: TSNode<'a>) -> Result<Self, ModelError> {
let value = match node.kind() {
rules::NULL => Self::Null,
rules::TRUE => Self::Boolean(true),
rules::FALSE => Self::Boolean(false),
rules::DEC_INT | rules::OCT_INT | rules::HEX_INT => Self::Int(node.try_into()?),
rules::FLOAT => Self::Float(node.try_into()?),
rules::SIMPLE_STRING => Self::String(node.try_into()?),
rules::META_ARRAY => Self::Array(node.try_into()?),
rules::META_OBJECT => Self::Object(node.try_into()?),
_ => bail!(ModelError::parser(format!("Invalid meta value {:?}", node))),
};
Ok(value)
}
}
impl<'a> TryFrom<TSNode<'a>> for MetaAttribute {
type Error = Report<ModelError>;
fn try_from(node: TSNode<'a>) -> Result<Self, ModelError> {
let mut children = node.into_children();
let name = children.next_field(fields::NAME).try_into()?;
children.skip_terminal(symbols::COLON)?;
let value = children.next_field(fields::VALUE).try_into()?;
Ok(Self { name, value })
}
}
impl<'a> TryFrom<TSNode<'a>> for Meta {
type Error = Report<ModelError>;
fn try_from(node: TSNode<'a>) -> Result<Self, ModelError> {
let mut children = node.into_children();
children.skip_terminal(keywords::META)?;
let attributes = children
.next_field(fields::ATTRIBUTES)?
.into_block(BlockEnds::Braces, BlockDelim::None)
.collect_anchors()?;
Ok(Self { attributes })
}
}
impl<'a> TryFrom<TSNode<'a>> for ParameterMeta {
type Error = Report<ModelError>;
fn try_from(node: TSNode<'a>) -> Result<Self, ModelError> {
let mut children = node.into_children();
children.skip_terminal(keywords::PARAMETER_META)?;
let attributes = children
.next_field(fields::ATTRIBUTES)?
.into_block(BlockEnds::Braces, BlockDelim::None)
.collect_anchors()?;
Ok(Self { attributes })
}
}