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
/// Provides serialization and deserialization functions which should use a default 
/// string instead of `null` on the wire.
#[allow(unused_macros)]
macro_rules! optional_string {
    ($n:ident : $s:tt) => {
        pub mod $n {
            use serde::de::value::StringDeserializer;
            use serde::{Serialize, Serializer, Deserialize, Deserializer};

            /// The string which represents `null` on the wire.
            pub static DEFAULT: &'static str = $s;

            /// Serialize an optional value, replacing `None` with the module-specified default.
            pub fn serialize<S: Serializer, T: Serialize>(opt: &Option<T>, s: S) -> Result<S::Ok, S::Error> {
                match *opt {
                    None => s.serialize_str(DEFAULT),
                    Some(ref value) => s.serialize_some(value),
                }
            }

            /// Deserialize a string, replacing the module-specified default with `None`.
            pub fn deserialize<D: Deserializer, T: Deserialize>(d: D) -> Result<Option<T>, D::Error> {
                
                let s = String::deserialize(d)?;
                if s == DEFAULT {
                    Ok(None)
                } else {
                    T::deserialize(s.into_deserializer()).map(Some)
                }
            }
        }
    }
}

/// Generate a `FromStr` implementation which uses serde's value deserializer.
/// 
/// # Usage
/// For an already-defined type, add deserialization with `fromstr_deserialize!(Type)`. 
/// This will create an implementation of `std::str::FromStr` where `Err=String`, containing
/// the error message returned by the deserializer.
///
/// To create a deserialization that uses a custom error type, instead write
/// `fromstr_deserialize!(Type, Err = YourError)`. This requires 
/// `YourError: From<::serde::de::value::Error>`.
macro_rules! fromstr_deserialize {
    ($t:ident) => {
        impl ::std::str::FromStr for $t {
            type Err = String;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                use std::string::ToString;

                use serde::Deserialize;
                use serde::de::IntoDeserializer;
                use serde::de::value;

                $t::deserialize(s.into_deserializer()).map_err(|e: value::Error| e.to_string())
            }
        }
    };
    
    ($t:ident, Err = $err:ident) => {
        impl ::std::str::FromStr for $t {
            type Err = $err;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                use serde::Deserialize;
                use serde::de::value::{self, ValueDeserializer};

                $t::deserialize(s.into_deserializer()).map_err(|e: value::Error| $err::from(e))
            }
        }
    }
}