summaryrefslogtreecommitdiffstats
path: root/vendor/globset/src/serde_impl.rs
blob: 6affc59041da02571cb0dac1cb97024b670256bd (plain)
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
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::Glob;

impl Serialize for Glob {
    fn serialize<S: Serializer>(
        &self,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(self.glob())
    }
}

impl<'de> Deserialize<'de> for Glob {
    fn deserialize<D: Deserializer<'de>>(
        deserializer: D,
    ) -> Result<Self, D::Error> {
        let glob = <&str as Deserialize>::deserialize(deserializer)?;
        Glob::new(glob).map_err(D::Error::custom)
    }
}

#[cfg(test)]
mod tests {
    use Glob;

    #[test]
    fn glob_json_works() {
        let test_glob = Glob::new("src/**/*.rs").unwrap();

        let ser = serde_json::to_string(&test_glob).unwrap();
        assert_eq!(ser, "\"src/**/*.rs\"");

        let de: Glob = serde_json::from_str(&ser).unwrap();
        assert_eq!(test_glob, de);
    }
}