vdf_reader/
event.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use crate::error::WrongEventTypeError;
use crate::VdfError;
use logos::Span;
use std::borrow::Cow;

/// Kinds of item.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Item<'a> {
    /// A statement, the ones starting with #.
    Statement { content: Cow<'a, str>, span: Span },

    /// A value.
    Item { content: Cow<'a, str>, span: Span },
}

impl<'a> Item<'a> {
    pub fn span(&self) -> Span {
        match self {
            Item::Statement { span, .. } => span.clone(),
            Item::Item { span, .. } => span.clone(),
        }
    }

    pub fn into_content(self) -> Cow<'a, str> {
        match self {
            Item::Statement { content, .. } => content,
            Item::Item { content, .. } => content,
        }
    }

    pub fn as_str(&self) -> &str {
        match self {
            Item::Statement { content, .. } => content.as_ref(),
            Item::Item { content, .. } => content.as_ref(),
        }
    }

    pub fn into_owned(self) -> Item<'static> {
        match self {
            Item::Statement { content, span } => Item::Statement {
                content: content.into_owned().into(),
                span,
            },
            Item::Item { content, span } => Item::Item {
                content: content.into_owned().into(),
                span,
            },
        }
    }
}

/// Reader event.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Event<'a> {
    /// A group with the given name is starting.
    GroupStart(GroupStartEvent<'a>),

    /// A group has ended.
    GroupEnd(GroupEndEvent),

    /// An entry.
    Entry(EntryEvent<'a>),
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct GroupStartEvent<'a> {
    pub name: Cow<'a, str>,
    pub span: Span,
}

impl GroupStartEvent<'_> {
    pub fn into_owned(self) -> GroupStartEvent<'static> {
        GroupStartEvent {
            name: self.name.into_owned().into(),
            span: self.span,
        }
    }
}

impl<'a> TryFrom<Event<'a>> for GroupStartEvent<'a> {
    type Error = VdfError;

    fn try_from(event: Event<'a>) -> Result<Self, Self::Error> {
        match event {
            Event::GroupStart(event) => Ok(event),
            Event::GroupEnd(_) => {
                Err(WrongEventTypeError::new(event, "group start", "group end").into())
            }
            Event::Entry(_) => Err(WrongEventTypeError::new(event, "group start", "entry").into()),
        }
    }
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct GroupEndEvent {
    pub span: Span,
}

impl<'a> TryFrom<Event<'a>> for GroupEndEvent {
    type Error = VdfError;

    fn try_from(event: Event<'a>) -> Result<Self, Self::Error> {
        match event {
            Event::GroupEnd(event) => Ok(event),
            Event::GroupStart(_) => {
                Err(WrongEventTypeError::new(event, "group end", "group start").into())
            }
            Event::Entry(_) => Err(WrongEventTypeError::new(event, "group start", "entry").into()),
        }
    }
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct EntryEvent<'a> {
    pub key: Item<'a>,
    pub value: Item<'a>,
    pub span: Span,
}

impl EntryEvent<'_> {
    pub fn into_owned(self) -> EntryEvent<'static> {
        EntryEvent {
            key: self.key.into_owned(),
            value: self.value.into_owned(),
            span: self.span,
        }
    }
}

impl<'a> TryFrom<Event<'a>> for EntryEvent<'a> {
    type Error = VdfError;

    fn try_from(event: Event<'a>) -> Result<Self, Self::Error> {
        match event {
            Event::Entry(event) => Ok(event),
            Event::GroupEnd(_) => Err(WrongEventTypeError::new(event, "entry", "group end").into()),
            Event::GroupStart(_) => {
                Err(WrongEventTypeError::new(event, "entry", "group start").into())
            }
        }
    }
}

impl Event<'_> {
    #[allow(dead_code)]
    pub fn span(&self) -> Span {
        match self {
            Event::GroupStart(GroupStartEvent { span, .. }) => span.clone(),
            Event::GroupEnd(GroupEndEvent { span, .. }) => span.clone(),
            Event::Entry(EntryEvent { span, .. }) => span.clone(),
        }
    }
    pub fn into_owned(self) -> Event<'static> {
        match self {
            Event::GroupStart(event) => Event::GroupStart(event.into_owned()),
            Event::GroupEnd(event) => Event::GroupEnd(event),
            Event::Entry(event) => Event::Entry(event.into_owned()),
        }
    }
}