| 1 |
use std::fmt; |
| 2 |
|
| 3 |
/// A plist `uid` value. These are found exclusively in plists created by `NSKeyedArchiver`. |
| 4 |
#[derive(Clone, Copy, Eq, Hash, PartialEq)] |
| 5 |
pub struct Uid { |
| 6 |
value: u64, |
| 7 |
} |
| 8 |
|
| 9 |
impl Uid { |
| 10 |
/// Creates a new `Uid` containing the given value. |
| 11 |
pub fn new(value: u64) -> Uid { |
| 12 |
Uid { value } |
| 13 |
} |
| 14 |
|
| 15 |
/// Returns the value as a `u64`. |
| 16 |
pub fn get(self) -> u64 { |
| 17 |
self.value |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
impl fmt::Debug for Uid { |
| 22 |
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 23 |
self.value.fmt(f) |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
#[cfg(feature = "serde")] |
| 28 |
pub mod serde_impls { |
| 29 |
use serde::{ |
| 30 |
de::{Deserialize, Deserializer, Error, Visitor}, |
| 31 |
ser::{Serialize, Serializer}, |
| 32 |
}; |
| 33 |
use std::fmt; |
| 34 |
|
| 35 |
use crate::Uid; |
| 36 |
|
| 37 |
pub const UID_NEWTYPE_STRUCT_NAME: &str = "PLIST-UID"; |
| 38 |
|
| 39 |
impl Serialize for Uid { |
| 40 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 41 |
where |
| 42 |
S: Serializer, |
| 43 |
{ |
| 44 |
serializer.serialize_newtype_struct(UID_NEWTYPE_STRUCT_NAME, &self.get()) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
struct UidNewtypeVisitor; |
| 49 |
|
| 50 |
impl<'de> Visitor<'de> for UidNewtypeVisitor { |
| 51 |
type Value = Uid; |
| 52 |
|
| 53 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 54 |
formatter.write_str("a plist uid") |
| 55 |
} |
| 56 |
|
| 57 |
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E> |
| 58 |
where |
| 59 |
E: Error, |
| 60 |
{ |
| 61 |
UidU64Visitor.visit_u64(v) |
| 62 |
} |
| 63 |
|
| 64 |
fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
| 65 |
where |
| 66 |
D: Deserializer<'de>, |
| 67 |
{ |
| 68 |
deserializer.deserialize_u64(UidU64Visitor) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
struct UidU64Visitor; |
| 73 |
|
| 74 |
impl<'de> Visitor<'de> for UidU64Visitor { |
| 75 |
type Value = Uid; |
| 76 |
|
| 77 |
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 78 |
formatter.write_str("a plist uid") |
| 79 |
} |
| 80 |
|
| 81 |
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E> |
| 82 |
where |
| 83 |
E: Error, |
| 84 |
{ |
| 85 |
Ok(Uid::new(v)) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
impl<'de> Deserialize<'de> for Uid { |
| 90 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 91 |
where |
| 92 |
D: Deserializer<'de>, |
| 93 |
{ |
| 94 |
deserializer.deserialize_newtype_struct(UID_NEWTYPE_STRUCT_NAME, UidNewtypeVisitor) |
| 95 |
} |
| 96 |
} |
| 97 |
} |