sevenz_rust2/
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}
22
23impl AsRef<[u8]> for Password {
24    fn as_ref(&self) -> &[u8] {
25        &self.0
26    }
27}
28
29impl From<&str> for Password {
30    fn from(s: &str) -> Self {
31        let mut result = Vec::with_capacity(s.len() * 2);
32        let utf16 = s.encode_utf16();
33        for u in utf16 {
34            let _ = result.write_u16::<LittleEndian>(u);
35        }
36        Self(result)
37    }
38}
39
40impl From<&[u16]> for Password {
41    fn from(s: &[u16]) -> Self {
42        let mut result = Vec::with_capacity(s.len() * 2);
43        for u in s {
44            let _ = result.write_u16::<LittleEndian>(*u);
45        }
46        Self(result)
47    }
48}