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
//! Deserializer for the currency enum.

use std::fmt;

use serde::de::{self, Deserialize, Visitor, Unexpected};

use super::super::Currency;


const EXPECTING_MSG: &str = "currency symbol";


impl<'de> Deserialize<'de> for Currency {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: de::Deserializer<'de>
    {
        deserializer.deserialize_str(CurrencyVisitor)
    }
}

struct CurrencyVisitor;
impl<'de> Visitor<'de> for CurrencyVisitor {
    type Value = Currency;

    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "{}", EXPECTING_MSG)
    }

    fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
        // TODO: consider providing a FromStr implementation for Currency
        if v.is_empty() {
            return Err(de::Error::invalid_length(0, &"non-empty string"));
        }
        // Implementation generated by the build script.
        include!(concat!(env!("OUT_DIR"), "/", "model/de/currency/visit_str.inc.rs"))  // no semicolon!
    }
}