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
use crate::{
decode::Decode,
encode::Encode,
into_type::IntoType,
Error,
};
use std::{
borrow::Cow,
convert::{
TryFrom,
TryInto,
},
};
pub struct Function(pub [u8; 32]);
impl TryFrom<&str> for Function {
type Error = Error;
fn try_from(value: &str) -> Result<Function, Self::Error> {
let s = match value.len() {
48 => value,
50 => value.split_at(2).1,
_ => value,
};
let slice = hex::decode(&s)?;
let slice: [u8; 24] = slice.as_slice().try_into()?;
let mut buf = [0u8; 32];
buf[0..24].copy_from_slice(&slice);
Ok(Function(buf))
}
}
impl TryFrom<&[u8]> for Function {
type Error = Error;
fn try_from(value: &[u8]) -> Result<Function, Self::Error> {
let slice: [u8; 24] = value.try_into()?;
let mut buf = [0u8; 32];
buf[0..24].copy_from_slice(&slice);
Ok(Function(buf))
}
}
impl TryFrom<&Vec<u8>> for Function {
type Error = Error;
fn try_from(value: &Vec<u8>) -> Result<Function, Self::Error> {
let slice: [u8; 24] = value.as_slice().try_into()?;
let mut buf = [0u8; 32];
buf[0..24].copy_from_slice(&slice);
Ok(Function(buf))
}
}
impl TryFrom<Vec<u8>> for Function {
type Error = Error;
fn try_from(value: Vec<u8>) -> Result<Function, Self::Error> {
let slice: [u8; 24] = value.as_slice().try_into()?;
let mut buf = [0u8; 32];
buf[0..24].copy_from_slice(&slice);
Ok(Function(buf))
}
}
impl Encode for Function {
fn encode(&self) -> Vec<u8> {
self.0.to_vec()
}
}
impl<'a> Decode<'a> for Function {
fn decode(buf: &'a [u8]) -> Self {
Function(buf[0..32].try_into().unwrap())
}
}
impl IntoType for Function {
fn into_type() -> Cow<'static, str> {
Cow::Borrowed("function")
}
}