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
#[cfg(feature = "std")]
use std::{fs::File, io::Read, path::Path};
use subtle_encoding::Encoding;
#[cfg(feature = "std")]
use zeroize::Zeroize;
use error::Error;
pub trait Decode: Sized {
fn decode<E: Encoding>(encoded: &[u8], encoding: &E) -> Result<Self, Error>;
fn decode_from_str<S, E>(encoded_str: S, encoding: &E) -> Result<Self, Error>
where
S: AsRef<str>,
E: Encoding,
{
Self::decode(encoded_str.as_ref().as_bytes(), encoding)
}
#[cfg(feature = "std")]
fn decode_from_reader<R, E>(reader: &mut R, encoding: &E) -> Result<Self, Error>
where
R: Read,
E: Encoding,
{
let mut bytes = vec![];
reader.read_to_end(bytes.as_mut())?;
let result = Self::decode(&bytes, encoding);
bytes.zeroize();
result
}
#[cfg(feature = "std")]
fn decode_from_file<P, E>(path: P, encoding: &E) -> Result<Self, Error>
where
P: AsRef<Path>,
E: Encoding,
{
Self::decode_from_reader(&mut File::open(path.as_ref())?, encoding)
}
}