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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
use std::fmt::{self, Display};
use std::str::FromStr;
use std::mem::replace;
use std::{error, io, num, result, str};
use serde::de::{self, Deserialize, DeserializeOwned, DeserializeSeed, Visitor, MapAccess, IntoDeserializer};
use parse::{self, Item};

pub trait Trait {
    fn next(&mut self) -> Option<result::Result<Item, Error>>;
}

impl<E, T: Iterator<Item=result::Result<Item, E>>> Trait for T where Error: From<E> {
    fn next(&mut self) -> Option<result::Result<Item, Error>> {
        Iterator::next(self).map(|v| v.map_err(Into::into))
    }
}

#[derive(Debug, Clone)]
pub enum Error {
    Custom(String),
    UnexpectedEof,
    InvalidState,
}

impl From<num::ParseIntError> for Error {
    fn from(e: num::ParseIntError) -> Self {
        Error::Custom(e.to_string())
    }
}

impl From<num::ParseFloatError> for Error {
    fn from(e: num::ParseFloatError) -> Self {
        Error::Custom(e.to_string())
    }
}

impl<E: error::Error> From<parse::Error<E>> for Error {
    fn from(e: parse::Error<E>) -> Self {
        Error::Custom(e.to_string())
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        "INI deserialization error"
    }
}

impl de::Error for Error {
    fn custom<T: Display>(msg: T) -> Self {
        Error::Custom(msg.to_string())
    }
}

pub type Result<T> = result::Result<T, Error>;

enum PeekKind {
    Value,
    Section,
}

#[derive(Debug, Copy, Clone)]
enum Next<T> {
    Init,
    Eof,
    Some(T),
}

#[derive(Debug)]
pub struct Deserializer<T> {
    input: T,
    next: Next<Result<Item>>,
}

impl<T> Deserializer<T> {
    pub fn new(input: T) -> Self {
        Deserializer {
            input: input,
            next: Next::Init,
        }
    }
}

impl<T: Trait> Deserializer<T> {
    fn populate(&mut self) {
        while let Next::Init = self.next {
            let next = self.input.next();
            self.next = match next {
                Some(Ok(Item::Comment { .. })) => Next::Init,
                Some(Ok(Item::Empty)) => Next::Init,
                Some(v) => Next::Some(v),
                None => Next::Eof,
            };
        }
    }

    fn next_item(&mut self) -> Result<Item> {
        let next = match self.next {
            Next::Eof | Next::Some(Err(..)) => Next::Eof,
            _ => Next::Init,
        };
        let next = replace(&mut self.next, next);
        match next {
            Next::Some(v) => v,
            Next::Eof => Err(Error::UnexpectedEof),
            Next::Init => unreachable!(),
        }
    }

    fn peek_item(&mut self) -> Result<Option<&mut Item>> {
        match &mut self.next {
            &mut Next::Some(Ok(ref mut v)) => Ok(Some(v)),
            e @ &mut Next::Some(Err(..)) => {
                if let Next::Some(Err(e)) = replace(e, Next::Eof) {
                    Err(e)
                } else {
                    unreachable!()
                }
            },
            &mut Next::Eof => Ok(None),
            &mut Next::Init => unreachable!(),
        }
    }

    fn peek_kind(&mut self) -> Result<Option<PeekKind>> {
        self.populate();
        Ok(match self.peek_item()? {
            Some(&mut Item::Value { .. }) => Some(PeekKind::Value),
            Some(&mut Item::Section { .. }) => Some(PeekKind::Section),
            None => None,
            Some(..) => unreachable!(),
        })
    }

    fn next_key(&mut self) -> Result<String> {
        self.populate();
        match self.peek_item()? {
            Some(&mut Item::Value { ref mut key, .. }) => Ok(replace(key, String::new())),
            Some(..) => Err(Error::InvalidState),
            None => Err(Error::UnexpectedEof),
        }
    }

    fn next_value(&mut self) -> Result<String> {
        self.populate();
        match self.next_item()? {
            Item::Value { value, .. } => Ok(value),
            _ => Err(Error::InvalidState),
        }
    }

    fn next_section(&mut self) -> Result<String> {
        self.populate();
        match self.peek_item()? {
            Some(&mut Item::Section { ref mut name }) => Ok(replace(name, String::new())),
            Some(..) => Err(Error::InvalidState),
            None => Err(Error::UnexpectedEof),
        }
    }

    fn next_section_commit(&mut self) -> Result<String> {
        self.populate();
        match self.next_item()? {
            Item::Section { name } => Ok(name),
            _ => Err(Error::InvalidState),
        }
    }

    fn assert_eof(&mut self) -> Result<()> {
        self.populate();
        match self.peek_item()? {
            Some(..) => Err(Error::InvalidState),
            None => Ok(()),
        }
    }
}

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

    fn deserialize_any<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_map(MapAccessTop(self))
    }

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

    forward_to_deserialize_any! {
        bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
        byte_buf unit unit_struct newtype_struct seq tuple tuple_struct
        map struct identifier ignored_any enum
    }
}

impl<R: io::BufRead> Deserializer<parse::Parser<io::Lines<R>>> {
    /// Creates an INI deserializer from an `io::BufRead`.
    pub fn from_bufread(reader: R) -> Self {
        Deserializer::new(parse::Parser::from_bufread(reader))
    }
}

impl<R: io::Read> Deserializer<parse::Parser<io::Lines<io::BufReader<R>>>> {
    /// Creates an INI deserializer from a reader.
    pub fn from_read(reader: R) -> Self {
        Deserializer::new(parse::Parser::from_read(reader))
    }
}

impl<'a> Deserializer<parse::Parser<parse::OkIter<str::Lines<'a>>>> {
    /// Creates an INI deserializer from a `&str`.
    pub fn from_str(s: &'a str) -> Self {
        Deserializer::new(parse::Parser::from_str(s))
    }
}

pub struct SectionDeserializer<'a, T: 'a>(&'a mut Deserializer<T>);

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

    fn deserialize_any<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_map(MapAccessSection(self.0))
    }

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

    forward_to_deserialize_any! {
        bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
        byte_buf unit unit_struct newtype_struct seq tuple tuple_struct
        map struct identifier ignored_any enum
    }
}

pub struct ValueDeserializer<'a, T: 'a>(&'a mut Deserializer<T>);

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

    fn deserialize_any<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        match (self.0).peek_kind()? {
            Some(PeekKind::Value) => self.deserialize_str(visitor),
            None | Some(PeekKind::Section) => Err(Error::InvalidState),
        }
    }

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

    fn deserialize_i8<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_i8(FromStr::from_str(&(self.0).next_value()?)?)
    }

    fn deserialize_i16<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_i16(FromStr::from_str(&(self.0).next_value()?)?)
    }

    fn deserialize_i32<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_i32(FromStr::from_str(&(self.0).next_value()?)?)
    }

    fn deserialize_i64<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_i64(FromStr::from_str(&(self.0).next_value()?)?)
    }

    fn deserialize_u8<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_u8(FromStr::from_str(&(self.0).next_value()?)?)
    }

    fn deserialize_u16<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_u16(FromStr::from_str(&(self.0).next_value()?)?)
    }

    fn deserialize_u32<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_u32(FromStr::from_str(&(self.0).next_value()?)?)
    }

    fn deserialize_u64<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_u64(FromStr::from_str(&(self.0).next_value()?)?)
    }

    fn deserialize_f32<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_f32(FromStr::from_str(&(self.0).next_value()?)?)
    }

    fn deserialize_f64<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_f64(FromStr::from_str(&(self.0).next_value()?)?)
    }

    fn deserialize_char<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
        let value = (self.0).next_value()?;
        let mut chars = value.chars();
        if let Some(c) = chars.next() {
            if chars.next().is_some() {
                // >1 char long
                visitor.visit_str(&value)
            } else {
                visitor.visit_char(c)
            }
        } else {
            // 0 chars long
            visitor.visit_str(&value)
        }
    }

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

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

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

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

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

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

    // Unit struct means a named value containing no data.
    fn deserialize_unit_struct<V: Visitor<'de>>(self, _name: &'static str, visitor: V) -> Result<V::Value> {
        self.deserialize_unit(visitor)
    }

    fn deserialize_newtype_struct<V: Visitor<'de>>(self, _name: &'static str, visitor: V) -> Result<V::Value> {
        visitor.visit_newtype_struct(self)
    }

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

    fn deserialize_tuple<V: Visitor<'de>>(self, _len: usize, visitor: V) -> Result<V::Value> {
        self.deserialize_any(visitor)
    }

    fn deserialize_tuple_struct<V: Visitor<'de>>(self, _name: &'static str, _len: usize, visitor: V) -> Result<V::Value> {
        self.deserialize_any(visitor)
    }

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

    fn deserialize_struct<V: Visitor<'de>>(self, _name: &'static str, _fields: &'static [&'static str], visitor: V) -> Result<V::Value> {
        self.deserialize_map(visitor)
    }

    fn deserialize_enum<V: Visitor<'de>>(self, _name: &'static str, _variants: &'static [&'static str], visitor: V) -> Result<V::Value> {
        match (self.0).peek_kind()? {
            Some(PeekKind::Value) => visitor.visit_enum((self.0).next_value()?.into_deserializer()),
            None | Some(PeekKind::Section) => Err(Error::InvalidState),
        }
    }

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

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

struct MapAccessTop<'a, T: Trait + 'a>(&'a mut Deserializer<T>);

impl<'de, 'a, T: Trait + 'a> MapAccess<'de> for MapAccessTop<'a, T> {
    type Error = Error;

    fn next_key_seed<K: DeserializeSeed<'de>>(&mut self, seed: K) -> Result<Option<K::Value>> {
        match (self.0).peek_kind()? {
            Some(PeekKind::Value) => seed.deserialize((self.0).next_key()?.into_deserializer()),
            Some(PeekKind::Section) => seed.deserialize((self.0).next_section()?.into_deserializer()),
            None => return Ok(None),
        }.map(Some)
    }

    fn next_value_seed<V: DeserializeSeed<'de>>(&mut self, seed: V) -> Result<V::Value> {
        match (self.0).peek_kind()? {
            Some(PeekKind::Value) => seed.deserialize(&mut ValueDeserializer(self.0)),
            Some(PeekKind::Section) => {
                (self.0).next_section_commit()?;
                seed.deserialize(&mut SectionDeserializer(self.0))
            },
            None => Err(Error::UnexpectedEof),
        }
    }
}

struct MapAccessSection<'a, T: Trait + 'a>(&'a mut Deserializer<T>);

impl<'de, 'a, T: Trait + 'a> MapAccess<'de> for MapAccessSection<'a, T> {
    type Error = Error;

    fn next_key_seed<K: DeserializeSeed<'de>>(&mut self, seed: K) -> Result<Option<K::Value>> {
        match (self.0).peek_kind()? {
            Some(PeekKind::Value) => seed.deserialize((self.0).next_key()?.into_deserializer()).map(Some),
            None | Some(PeekKind::Section) => Ok(None),
        }
    }

    fn next_value_seed<V: DeserializeSeed<'de>>(&mut self, seed: V) -> Result<V::Value> {
        seed.deserialize(&mut ValueDeserializer(self.0))
    }
}

/*
struct Enum<'a, 'de: 'a> {
    de: &'a mut Deserializer<'de>,
}

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

// `EnumAccess` is provided to the `Visitor` to give it the ability to determine
// which variant of the enum is supposed to be deserialized.
//
// Note that all enum deserialization methods in Serde refer exclusively to the
// "externally tagged" enum representation.
impl<'de, 'a> EnumAccess<'de> for Enum<'a, 'de> {
    type Error = Error;
    type Variant = Self;

    fn variant_seed<V: DeserializeSeed<'de>>(self, seed: V) -> Result<(V::Value, Self::Variant)> {
        // The `deserialize_enum` method parsed a `{` character so we are
        // currently inside of a map. The seed will be deserializing itself from
        // the key of the map.
        let val = seed.deserialize(&mut *self.de)?;
        // Parse the colon separating map key from value.
        if (self.0).next_char()? == ':' {
            Ok((val, self))
        } else {
            Err(Error::ExpectedMapColon)
        }
    }
}

// `VariantAccess` is provided to the `Visitor` to give it the ability to see
// the content of the single variant that it decided to deserialize.
impl<'de, 'a> VariantAccess<'de> for Enum<'a, 'de> {
    type Error = Error;

    // If the `Visitor` expected this variant to be a unit variant, the input
    // should have been the plain string case handled in `deserialize_enum`.
    fn unit_variant(self) -> Result<()> {
        Err(Error::ExpectedString)
    }

    // Newtype variants are represented in JSON as `{ NAME: VALUE }` so
    // deserialize the value here.
    fn newtype_variant_seed<T: DeserializeSeed<'de>>(self, seed: T) -> Result<T::Value> {
        seed.deserialize(self.de)
    }

    // Tuple variants are represented in JSON as `{ NAME: [DATA...] }` so
    // deserialize the sequence of data here.
    fn tuple_variant<V: Visitor<'de>>(self, _len: usize, visitor: V) -> Result<V::Value> {
        de::Deserializer::deserialize_seq(self.de, visitor)
    }

    // Struct variants are represented in JSON as `{ NAME: { K: V, ... } }` so
    // deserialize the inner map here.
    fn struct_variant<V: Visitor<'de>>(self, _fields: &'static [&'static str], visitor: V) -> Result<V::Value> {
        de::Deserializer::deserialize_map(self.de, visitor)
    }
}*/

/// Deserialize an instance of type `T` from a string of INI text.
pub fn from_str<T: DeserializeOwned>(s: &str) -> Result<T> {
    let mut de = Deserializer::new(parse::Parser::from_str(s.as_ref()));
    let value = Deserialize::deserialize(&mut de)?;

    de.assert_eof()?;
    Ok(value)
}

/// Deserialize an instance of type `T` from a buffered IO stream of INI.
pub fn from_bufread<R: io::BufRead, T: DeserializeOwned>(reader: R) -> Result<T> {
    let mut de = Deserializer::new(parse::Parser::from_bufread(reader));
    let value = Deserialize::deserialize(&mut de)?;

    de.assert_eof()?;
    Ok(value)
}

/// Deserialize an instance of type `T` from a stream of INI data.
pub fn from_read<R: io::Read, T: DeserializeOwned>(reader: R) -> Result<T> {
    let mut de = Deserializer::new(parse::Parser::from_read(reader));
    let value = Deserialize::deserialize(&mut de)?;

    de.assert_eof()?;
    Ok(value)
}