summaryrefslogtreecommitdiffstats
path: root/third_party/rust/plist/src/uid.rs
blob: f2e107993a17bd516550e9a95e95a53df8c43f0e (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
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
use std::fmt;

/// A plist `uid` value. These are found exclusively in plists created by `NSKeyedArchiver`.
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct Uid {
    value: u64,
}

impl Uid {
    /// Creates a new `Uid` containing the given value.
    pub fn new(value: u64) -> Uid {
        Uid { value }
    }

    /// Returns the value as a `u64`.
    pub fn get(self) -> u64 {
        self.value
    }
}

impl fmt::Debug for Uid {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        self.value.fmt(f)
    }
}

#[cfg(feature = "serde")]
pub mod serde_impls {
    use serde::{
        de::{Deserialize, Deserializer, Error, Visitor},
        ser::{Serialize, Serializer},
    };
    use std::fmt;

    use crate::Uid;

    pub const UID_NEWTYPE_STRUCT_NAME: &str = "PLIST-UID";

    impl Serialize for Uid {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            serializer.serialize_newtype_struct(UID_NEWTYPE_STRUCT_NAME, &self.get())
        }
    }

    struct UidNewtypeVisitor;

    impl<'de> Visitor<'de> for UidNewtypeVisitor {
        type Value = Uid;

        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            formatter.write_str("a plist uid")
        }

        fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
        where
            E: Error,
        {
            UidU64Visitor.visit_u64(v)
        }

        fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
        where
            D: Deserializer<'de>,
        {
            deserializer.deserialize_u64(UidU64Visitor)
        }
    }

    struct UidU64Visitor;

    impl<'de> Visitor<'de> for UidU64Visitor {
        type Value = Uid;

        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            formatter.write_str("a plist uid")
        }

        fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
        where
            E: Error,
        {
            Ok(Uid::new(v))
        }
    }

    impl<'de> Deserialize<'de> for Uid {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: Deserializer<'de>,
        {
            deserializer.deserialize_newtype_struct(UID_NEWTYPE_STRUCT_NAME, UidNewtypeVisitor)
        }
    }
}