sevenz_rust/
password.rs

1use byteorder::{LittleEndian, WriteBytesExt};
2
3#[derive(Debug, Default, Clone, PartialEq)]
4pub struct Password(Vec<u8>);
5
6impl Password {
7    pub fn empty() -> Self {
8        Self(Default::default())
9    }
10    pub fn to_vec(self) -> Vec<u8> {
11        self.0
12    }
13
14    pub fn as_slice(&self) -> &[u8] {
15        &self.0
16    }
17
18    pub fn is_empty(&self) -> bool {
19        self.0.is_empty()
20    }
21}
22impl AsRef<[u8]> for Password {
23    fn as_ref(&self) -> &[u8] {
24        &self.0
25    }
26}
27
28impl From<&str> for Password {
29    fn from(s: &str) -> Self {
30        let mut result = Vec::with_capacity(s.len() * 2);
31        let utf16 = s.encode_utf16();
32        for u in utf16 {
33            let _ = result.write_u16::<LittleEndian>(u);
34        }
35        Self(result)
36    }
37}
38
39impl From<&[u16]> for Password {
40    fn from(s: &[u16]) -> Self {
41        let mut result = Vec::with_capacity(s.len() * 2);
42        for u in s {
43            let _ = result.write_u16::<LittleEndian>(*u);
44        }
45        Self(result)
46    }
47}