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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//! Deserialize VDF data to a Rust data structure

use std::collections::VecDeque;

use serde::Deserialize;
use serde::de::{self, Visitor, MapAccess, DeserializeSeed, IntoDeserializer};
use steamy_vdf::parser::{self as vdf_parser, Token};

use crate::error::{Error, Result};
use std::str::FromStr;
use std::borrow::{Borrow, Cow};

/// A structure that deserializes VDF into Rust values
pub struct Deserializer<'de> {
    input: &'de str,
    parsed_input: VecDeque<Token<'de>>,
    top_level: bool,
}

impl<'de> Deserializer<'de> {
    /// Creates a VDF deserializer from a `&str`
    pub fn from_str(input: &'de str) -> Self {
        Self {
            input,
            parsed_input: VecDeque::new(),
            top_level: true,
        }
    }
}

/// Deserialize an instance of type `T` from a string of VDF text
///
/// # Errors
///
/// If `s` is not valid VDF, or `T` uses an unsupported Serde data type,
/// or `T`'s `Deserialize` implementation itself returns an error, an error will be
/// returned.
pub fn from_str<'a, T>(s: &'a str) -> Result<T> where T: Deserialize<'a> {
    let mut deserializer = Deserializer::from_str(s);
    let t = T::deserialize(&mut deserializer)?;
    // before we toss a LateEOF, let's make sure we're not erroring on some whitespace
    let remaining_input = deserializer.input.trim_end();
    if remaining_input.is_empty() {
        Ok(t)
    } else {
        Err(Error::LateEOF)
    }
}

impl<'de> Deserializer<'de> {
    fn parse_more(&mut self) -> Result<()> {
        let parsed = vdf_parser::next(self.input.as_bytes());
        match parsed {
            nom::IResult::Done(remainder, token) => {
                // since it came from `as_bytes` this is safe
                self.input = unsafe { std::str::from_utf8_unchecked(remainder) };
                self.parsed_input.push_back(token);
            }
            nom::IResult::Incomplete(_) => return Err(Error::EarlyEOF),
            nom::IResult::Error(err) => return Err(Error::Tokenize(err.to_string())),
        }
        Ok(())
    }

    fn parse_more_if_needed(&mut self) -> Result<()> {
        if self.parsed_input.is_empty() {
            self.parse_more()?;
        }
        Ok(())
    }

    fn peek_token(&mut self) -> Result<&Token<'de>> {
        self.parse_more_if_needed()?;
        self.parsed_input.get(0).ok_or(Error::EarlyEOF)
    }

    fn next_token(&mut self) -> Result<Token<'de>> {
        self.parse_more_if_needed()?;
        self.parsed_input.pop_front().ok_or(Error::EarlyEOF)
    }

    fn next_token_item(&mut self) -> Result<Cow<'de, str>> {
        match self.next_token()? {
            Token::Item(data) => Ok(data),
            got => Err(Error::Expected("Item", format!("{:?}", got))),
        }
    }

    fn parse_next_token_data<T: FromStr>(&mut self) -> Result<T> where T::Err : std::fmt::Display {
        self.next_token_item()?.parse().map_err(|err: T::Err| Error::StringParse(err.to_string()))
    }
}

impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
    type Error = Error;

    fn deserialize_any<V: Visitor<'de>>(self, _visitor: V) -> Result<V::Value> {
        Err(Error::UnsupportedType("any"))
    }

    fn deserialize_bool<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        match self.next_token()? {
            Token::Item(data) if data == "0" => visitor.visit_bool(false),
            Token::Item(data) if data == "1" => visitor.visit_bool(true),
            got => Err(Error::Expected("bool (\"0\" or \"1\")", format!("{:?}", got))),
        }
    }

    fn deserialize_i8<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_i8(self.parse_next_token_data()?)
    }

    fn deserialize_i16<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_i16(self.parse_next_token_data()?)
    }

    fn deserialize_i32<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_i32(self.parse_next_token_data()?)
    }

    fn deserialize_i64<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_i64(self.parse_next_token_data()?)
    }

    fn deserialize_u8<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_u8(self.parse_next_token_data()?)
    }

    fn deserialize_u16<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_u16(self.parse_next_token_data()?)
    }

    fn deserialize_u32<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_u32(self.parse_next_token_data()?)
    }

    fn deserialize_u64<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_u64(self.parse_next_token_data()?)
    }

    fn deserialize_f32<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_f32(self.parse_next_token_data()?)
    }

    fn deserialize_f64<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_f64(self.parse_next_token_data()?)
    }

    fn deserialize_char<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_char(self.parse_next_token_data()?)
    }

    fn deserialize_str<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_str(self.next_token_item()?.borrow())
    }

    fn deserialize_string<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_string(String::from(self.next_token_item()?))
    }

    fn deserialize_bytes<V: Visitor<'de>>(self, _visitor: V) -> Result<V::Value> {
        Err(Error::UnsupportedType("byte array"))
    }

    fn deserialize_byte_buf<V: Visitor<'de>>(self, _visitor: V) -> Result<V::Value> {
        Err(Error::UnsupportedType("byte array"))
    }

    fn deserialize_option<V: Visitor<'de>>(self, _visitor: V) -> Result<V::Value> {
        Err(Error::UnsupportedType("option"))
    }

    fn deserialize_unit<V: Visitor<'de>>(self, _visitor: V) -> Result<V::Value> {
        Err(Error::UnsupportedType("unit"))
    }

    fn deserialize_unit_struct<V: Visitor<'de>>(self, _name: &'static str, _visitor: V) -> Result<V::Value> {
        Err(Error::UnsupportedType("unit_struct"))
    }

    fn deserialize_newtype_struct<V: Visitor<'de>>(self, name: &'static str, visitor: V) -> Result<V::Value> {
        if self.top_level {
            let name_token = self.next_token()?;
            match name_token {
                Token::Item(name_token) if name_token == name => {},
                got => return Err(Error::Expected(name, format!("{:?}", got))),
            }
            self.top_level = false;
        }
        visitor.visit_newtype_struct(self)
    }

    fn deserialize_seq<V: Visitor<'de>>(self, _visitor: V) -> Result<V::Value> {
        Err(Error::UnsupportedType("seq"))
    }

    fn deserialize_tuple<V: Visitor<'de>>(self, _len: usize, _visitor: V) -> Result<V::Value> {
        Err(Error::UnsupportedType("tuple"))
    }

    fn deserialize_tuple_struct<V: Visitor<'de>>(self, _name: &'static str, _len: usize, _visitor: V) -> Result<V::Value> {
        Err(Error::UnsupportedType("tuple_struct"))
    }

    fn deserialize_map<V: Visitor<'de>>(mut self, visitor: V) -> Result<V::Value> {
        match self.next_token()? {
            Token::GroupStart => {
                let value = visitor.visit_map(TabNewlineSeparated::new(&mut self))?;
                match self.next_token()? {
                    Token::GroupEnd => Ok(value),
                    got => Err(Error::Expected("'}'", format!("{:?}", got))),
                }
            }
            got => Err(Error::Expected("'{'", format!("{:?}", got))),
        }
    }

    fn deserialize_struct<V: Visitor<'de>>(
        self,
        name: &'static str,
        _fields: &'static [&'static str],
        visitor: V
    ) -> Result<V::Value> {
        if self.top_level {
            let name_token = self.next_token()?;
            match name_token {
                Token::Item(name_token) if name_token == name => {},
                got => return Err(Error::Expected(name, format!("{:?}", got))),
            }
            self.top_level = false;
        }
        self.deserialize_map(visitor)
    }

    fn deserialize_enum<V: Visitor<'de>>(self, _name: &'static str, _variants: &'static [&'static str], visitor: V) -> Result<V::Value> {
        visitor.visit_enum(self.next_token_item()?.into_deserializer())
    }

    fn deserialize_identifier<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        self.deserialize_str(visitor)
    }

    serde::forward_to_deserialize_any! {
        ignored_any
    }
}

struct TabNewlineSeparated<'a, 'de: 'a> {
    de: &'a mut Deserializer<'de>,
}

impl<'a, 'de> TabNewlineSeparated<'a, 'de> {
    fn new(de: &'a mut Deserializer<'de>) -> Self {
        Self {
            de,
        }
    }
}

impl<'de, 'a> MapAccess<'de> for TabNewlineSeparated<'a, 'de> {
    type Error = Error;

    fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
        where
            K: DeserializeSeed<'de>,
    {
        // Check if there are no more entries.
        if self.de.peek_token()? == &Token::GroupEnd {
            return Ok(None);
        }
        // Deserialize a map key.
        seed.deserialize(&mut *self.de).map(Some)
    }

    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
        where
            V: DeserializeSeed<'de>,
    {
        // Deserialize a map value.
        seed.deserialize(&mut *self.de)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_struct() {
        #[derive(Deserialize, PartialEq, Debug)]
        struct Inner {
            foo: String,
            bar: bool,
        }

        #[derive(Deserialize, PartialEq, Debug)]
        struct Test {
            int: u32,
            inner: Inner,
        }

        let j = concat!(
            "\"Test\"\n",
            "{\n",
            "\t\"int\"\t\"1\"\n",
            "\t\"inner\"\n",
            "\t{\n",
            "\t\t\"foo\"\t\"baz\"\n",
            "\t\t\"bar\"\t\"0\"\n",
            "\t}\n",
            "}"
        );
        let expected = Test {
            int: 1,
            inner: Inner {
                foo: "baz".to_string(),
                bar: false,
            },
        };
        assert_eq!(expected, from_str(j).unwrap());
    }
}