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
use alloc::string::String;
use core::fmt::{Display, Formatter};
use serde::{Deserialize, Serialize};

/// A type that can deserialize from any data to string type.
///
/// It just like [`Yaml`](crate::Yaml) but no null value, anchor type and containers.
///
/// Calling [`ToString::to_string`] can convert the data into string.
///
/// ```
/// use serde::Deserialize;
/// use yaml_peg::{node, serde::Stringify};
///
/// #[derive(Deserialize)]
/// struct Content {
///     width: Stringify,
/// }
///
/// let n_value = node!({"width" => 20});
/// let n_percent = node!({"width" => "20%"});
/// let value = Content::deserialize(n_value).unwrap();
/// let percent = Content::deserialize(n_percent).unwrap();
/// assert_eq!("20", value.width.to_string());
/// assert_eq!("20%", percent.width.to_string());
/// ```
#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(untagged)]
pub enum Stringify {
    /// Boolean value.
    Bool(bool),
    /// Integer value.
    Int(i32),
    /// Float value.
    Float(f32),
    /// String value.
    Str(String),
}

impl Display for Stringify {
    fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
        match self {
            Self::Bool(true) => write!(f, "true"),
            Self::Bool(false) => write!(f, "false"),
            Self::Int(n) => write!(f, "{}", n),
            Self::Float(n) => write!(f, "{}", n),
            Self::Str(s) => write!(f, "{}", s),
        }
    }
}

impl Default for Stringify {
    fn default() -> Self {
        Self::Str(String::new())
    }
}