1use crate::error::WrongEventTypeError;
2use crate::VdfError;
3use logos::Span;
4use std::borrow::Cow;
5
6#[derive(Clone, PartialEq, Eq, Debug)]
8pub enum Item<'a> {
9 Statement { content: Cow<'a, str>, span: Span },
11
12 Item { content: Cow<'a, str>, span: Span },
14}
15
16impl<'a> Item<'a> {
17 pub fn span(&self) -> Span {
18 match self {
19 Item::Statement { span, .. } => span.clone(),
20 Item::Item { span, .. } => span.clone(),
21 }
22 }
23
24 pub fn into_content(self) -> Cow<'a, str> {
25 match self {
26 Item::Statement { content, .. } => content,
27 Item::Item { content, .. } => content,
28 }
29 }
30
31 pub fn as_str(&self) -> &str {
32 match self {
33 Item::Statement { content, .. } => content.as_ref(),
34 Item::Item { content, .. } => content.as_ref(),
35 }
36 }
37
38 pub fn into_owned(self) -> Item<'static> {
39 match self {
40 Item::Statement { content, span } => Item::Statement {
41 content: content.into_owned().into(),
42 span,
43 },
44 Item::Item { content, span } => Item::Item {
45 content: content.into_owned().into(),
46 span,
47 },
48 }
49 }
50}
51
52#[derive(Clone, PartialEq, Eq, Debug)]
54pub enum Event<'a> {
55 GroupStart(GroupStartEvent<'a>),
57
58 GroupEnd(GroupEndEvent),
60
61 Entry(EntryEvent<'a>),
63
64 ValueContinuation(ValueContinuationEvent<'a>),
66}
67
68#[derive(Clone, Copy, PartialEq, Eq, Debug)]
69pub enum EventType {
70 GroupStart,
71 GroupEnd,
72 Entry,
73 ValueContinuation,
74}
75
76#[derive(Clone, PartialEq, Eq, Debug)]
77pub struct GroupStartEvent<'a> {
78 pub name: Cow<'a, str>,
79 pub span: Span,
80}
81
82impl GroupStartEvent<'_> {
83 pub fn into_owned(self) -> GroupStartEvent<'static> {
84 GroupStartEvent {
85 name: self.name.into_owned().into(),
86 span: self.span,
87 }
88 }
89}
90
91impl<'a> TryFrom<Event<'a>> for GroupStartEvent<'a> {
92 type Error = VdfError;
93
94 fn try_from(event: Event<'a>) -> Result<Self, Self::Error> {
95 match event {
96 Event::GroupStart(event) => Ok(event),
97 Event::GroupEnd(_) => {
98 Err(WrongEventTypeError::new(event, "group start", "group end").into())
99 }
100 Event::Entry(_) => Err(WrongEventTypeError::new(event, "group start", "entry").into()),
101 Event::ValueContinuation(_) => {
102 Err(WrongEventTypeError::new(event, "group start", "value continuation").into())
103 }
104 }
105 }
106}
107
108#[derive(Clone, PartialEq, Eq, Debug)]
109pub struct GroupEndEvent {
110 pub span: Span,
111}
112
113impl<'a> TryFrom<Event<'a>> for GroupEndEvent {
114 type Error = VdfError;
115
116 fn try_from(event: Event<'a>) -> Result<Self, Self::Error> {
117 match event {
118 Event::GroupEnd(event) => Ok(event),
119 Event::GroupStart(_) => {
120 Err(WrongEventTypeError::new(event, "group end", "group start").into())
121 }
122 Event::Entry(_) => Err(WrongEventTypeError::new(event, "group start", "entry").into()),
123 Event::ValueContinuation(_) => {
124 Err(WrongEventTypeError::new(event, "group start", "value continuation").into())
125 }
126 }
127 }
128}
129
130#[derive(Clone, PartialEq, Eq, Debug)]
131pub struct EntryEvent<'a> {
132 pub key: Item<'a>,
133 pub value: Item<'a>,
134 pub span: Span,
135}
136
137impl EntryEvent<'_> {
138 pub fn into_owned(self) -> EntryEvent<'static> {
139 EntryEvent {
140 key: self.key.into_owned(),
141 value: self.value.into_owned(),
142 span: self.span,
143 }
144 }
145}
146
147impl<'a> TryFrom<Event<'a>> for EntryEvent<'a> {
148 type Error = VdfError;
149
150 fn try_from(event: Event<'a>) -> Result<Self, Self::Error> {
151 match event {
152 Event::Entry(event) => Ok(event),
153 Event::GroupEnd(_) => Err(WrongEventTypeError::new(event, "entry", "group end").into()),
154 Event::GroupStart(_) => {
155 Err(WrongEventTypeError::new(event, "entry", "group start").into())
156 }
157 Event::ValueContinuation(_) => {
158 Err(WrongEventTypeError::new(event, "entry", "value continuation").into())
159 }
160 }
161 }
162}
163
164#[derive(Clone, PartialEq, Eq, Debug)]
165pub struct ValueContinuationEvent<'a> {
166 pub value: Item<'a>,
167 pub span: Span,
168}
169
170impl ValueContinuationEvent<'_> {
171 pub fn into_owned(self) -> ValueContinuationEvent<'static> {
172 ValueContinuationEvent {
173 value: self.value.into_owned(),
174 span: self.span,
175 }
176 }
177}
178
179impl Event<'_> {
180 #[allow(dead_code)]
181 pub fn span(&self) -> Span {
182 match self {
183 Event::GroupStart(GroupStartEvent { span, .. }) => span.clone(),
184 Event::GroupEnd(GroupEndEvent { span, .. }) => span.clone(),
185 Event::Entry(EntryEvent { span, .. }) => span.clone(),
186 Event::ValueContinuation(ValueContinuationEvent { span, .. }) => span.clone(),
187 }
188 }
189 pub fn into_owned(self) -> Event<'static> {
190 match self {
191 Event::GroupStart(event) => Event::GroupStart(event.into_owned()),
192 Event::GroupEnd(event) => Event::GroupEnd(event),
193 Event::Entry(event) => Event::Entry(event.into_owned()),
194 Event::ValueContinuation(event) => Event::ValueContinuation(event.into_owned()),
195 }
196 }
197
198 pub fn ty(&self) -> EventType {
199 match self {
200 Event::GroupStart(GroupStartEvent { .. }) => EventType::GroupStart,
201 Event::GroupEnd(GroupEndEvent { .. }) => EventType::GroupEnd,
202 Event::Entry(EntryEvent { .. }) => EventType::Entry,
203 Event::ValueContinuation(ValueContinuationEvent { .. }) => EventType::ValueContinuation,
204 }
205 }
206}