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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
pub mod encodings;
use crate::{
HttpError,
data::encodings::{ Encoding, Utf8Subset, Integer }
};
use std::{
str, u128, convert::TryFrom, marker::PhantomData, num::ParseIntError, ops::Deref,
hash::{ Hash, Hasher }, fmt::{ self, Display, Formatter }
};
#[macro_export] macro_rules! data {
($str:expr) => ({
let str: &'static str = $str;
::std::convert::TryInto::try_into(str).unwrap()
});
}
#[derive(Copy, Clone, Debug)]
pub struct Data<'a, E: Encoding> {
underlying: &'a[u8],
_mode: PhantomData<E>
}
impl<'a, E: Encoding> Data<'a, E> {
pub fn validate(bytes: &[u8]) -> bool {
E::is_valid(bytes)
}
pub fn as_slice(&self) -> &'a[u8] {
self.underlying
}
}
impl<'a, E: Encoding> Deref for Data<'a, E> {
type Target = [u8];
fn deref(&self) -> &Self::Target {
self.underlying
}
}
impl<'a, E: Encoding> AsRef<[u8]> for Data<'a, E> {
fn as_ref(&self) -> &[u8] {
self.underlying
}
}
impl<'a, E: Encoding + Utf8Subset> AsRef<str> for Data<'a, E> {
fn as_ref(&self) -> &str {
str::from_utf8(self.underlying).unwrap()
}
}
impl<'a, E: Encoding + Utf8Subset> Display for Data<'a, E> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str(self.as_ref())
}
}
impl<'a, 'b, E: Encoding> PartialEq<Data<'b, E>> for Data<'a, E> {
fn eq(&self, other: &Data<'b, E>) -> bool {
E::is_eq(self, other)
}
}
impl<'a, E: Encoding> PartialEq<&str> for Data<'a, E> {
fn eq(&self, other: &&str) -> bool {
E::is_eq(self, other.as_bytes())
}
}
impl<'a, E: Encoding> PartialEq<Data<'a, E>> for &str {
fn eq(&self, other: &Data<'a, E>) -> bool {
E::is_eq(self.as_bytes(), other)
}
}
impl<'a, E: Encoding> Eq for Data<'a, E> {}
impl<'a, E: Encoding> Hash for Data<'a, E> {
fn hash<H: Hasher>(&self, state: &mut H) {
E::hash(self.underlying, state)
}
}
impl<'a, 'b: 'a, E: Encoding> TryFrom<&'b str> for Data<'a, E> {
type Error = HttpError;
fn try_from(source: &'b str) -> Result<Self, Self::Error> {
Self::try_from(source.as_bytes())
}
}
impl<'a, 'b: 'a, E: Encoding> TryFrom<&'b[u8]> for Data<'a, E> {
type Error = HttpError;
fn try_from(bytes: &'b[u8]) -> Result<Self, Self::Error> {
match Self::validate(bytes) {
true => Ok(Self{ underlying: bytes, _mode: PhantomData }),
false => Err(HttpError::InvalidEncoding)
}
}
}
impl<'a> TryFrom<Data<'a, Integer>> for u128 {
type Error = ParseIntError;
fn try_from(data: Data<'a, Integer>) -> Result<Self, Self::Error> {
Self::from_str_radix(data.as_ref(), 10)
}
}
impl<'a> TryFrom<Data<'a, Integer>> for u16 {
type Error = ParseIntError;
fn try_from(data: Data<'a, Integer>) -> Result<Self, Self::Error> {
Self::from_str_radix(data.as_ref(), 10)
}
}