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
use core::fmt::Debug;
use crate::Error;
pub trait DecodedTagged<'a> {
type Output: Debug;
type Error: Debug;
fn decode_len(buff: &'a [u8], len: usize) -> Result<Self::Output, Self::Error>;
}
impl <'a>DecodedTagged<'a> for &[u8] {
type Output = &'a [u8];
type Error = Error;
fn decode_len(buff: &'a [u8], len: usize) -> Result<Self::Output, Self::Error> {
if buff.len() < len {
return Err(Error::Length);
}
Ok(&buff[..len])
}
}
impl <'a>DecodedTagged<'a> for &str {
type Output = &'a str;
type Error = Error;
fn decode_len(buff: &'a [u8], len: usize) -> Result<Self::Output, Self::Error> {
if buff.len() < len {
return Err(Error::Length);
}
match core::str::from_utf8(&buff[..len]) {
Ok(v) => Ok(v),
Err(_e) => Err(Error::Utf8),
}
}
}