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
// jkcoxson

use log::trace;

use crate::{error::PlistError, unsafe_bindings, Plist, PlistType};

impl Plist {
    /// Creates a new plist with the type of an integer
    pub fn new_uint(uint: u64) -> Plist {
        trace!("Generating new uint plist");
        unsafe { unsafe_bindings::plist_new_uint(uint) }.into()
    }
    /// Sets the plist as type integer with the given value
    pub fn set_uint_val(&self, val: u64) {
        trace!("Setting uint value");
        unsafe { unsafe_bindings::plist_set_uint_val(self.plist_t, val) }
    }
    /// Returns the value of the integer
    pub fn get_uint_val(&self) -> Result<u64, PlistError> {
        if self.plist_type != PlistType::Integer {
            return Err(PlistError::InvalidArg);
        }
        let mut val = unsafe { std::mem::zeroed() };
        trace!("Getting uint value");
        Ok(unsafe {
            unsafe_bindings::plist_get_uint_val(self.plist_t, &mut val);
            val
        })
    }
}

impl TryFrom<Plist> for u64 {
    type Error = PlistError;
    fn try_from(plist: Plist) -> Result<Self, Self::Error> {
        plist.get_uint_val()
    }
}

impl From<u64> for Plist {
    fn from(val: u64) -> Self {
        Plist::new_uint(val)
    }
}

impl TryFrom<Plist> for u32 {
    type Error = PlistError;
    fn try_from(plist: Plist) -> Result<Self, Self::Error> {
        plist.get_uint_val().map(|val| val as u32)
    }
}

impl From<u32> for Plist {
    fn from(val: u32) -> Self {
        Plist::new_uint(val as u64)
    }
}

impl TryFrom<Plist> for u16 {
    type Error = PlistError;
    fn try_from(plist: Plist) -> Result<Self, Self::Error> {
        plist.get_uint_val().map(|val| val as u16)
    }
}

impl From<u16> for Plist {
    fn from(val: u16) -> Self {
        Plist::new_uint(val as u64)
    }
}

impl TryFrom<Plist> for u8 {
    type Error = PlistError;
    fn try_from(plist: Plist) -> Result<Self, Self::Error> {
        plist.get_uint_val().map(|val| val as u8)
    }
}

impl From<u8> for Plist {
    fn from(val: u8) -> Self {
        Plist::new_uint(val as u64)
    }
}

impl TryFrom<Plist> for usize {
    type Error = PlistError;
    fn try_from(plist: Plist) -> Result<Self, Self::Error> {
        plist.get_uint_val().map(|x| x as usize)
    }
}

impl From<usize> for Plist {
    fn from(val: usize) -> Self {
        Plist::new_uint(val as u64)
    }
}