nardol/bytes/
from_bytes.rs1use chrono::{DateTime, NaiveDateTime, Utc};
2
3use crate::bytes::Bytes;
4use crate::error::{Error, ErrorKind};
5
6impl TryFrom<Bytes> for usize {
7 type Error = Error;
8
9 fn try_from(value: Bytes) -> Result<Self, Self::Error> {
10 if None == value.get(7) {
12 return Err(Error::new(
13 ErrorKind::InvalidBufferSize,
14 Some(
15 "impl TryFrom<Bytes> for usize requires Bytes of length of 8 bytes."
16 .to_string(),
17 ),
18 ));
19 }
20
21 let mut arr = [0_u8; 8];
22 for (index, value) in value.0.iter().enumerate() {
23 arr[index] = *value;
24 }
25 Ok(usize::from_be_bytes(arr))
26 }
27}
28
29impl TryFrom<&Bytes> for usize {
30 type Error = Error;
31
32 fn try_from(value: &Bytes) -> Result<Self, Self::Error> {
33 if None == value.get(7) {
35 return Err(Error::new(
36 ErrorKind::InvalidBufferSize,
37 Some(
38 "impl TryFrom<&Bytes> for usize requires Bytes of length of 8 bytes."
39 .to_string(),
40 ),
41 ));
42 }
43
44 let mut arr = [0_u8; 8];
45 for (index, value) in value.0.iter().enumerate() {
46 arr[index] = *value;
47 }
48 Ok(usize::from_be_bytes(arr))
49 }
50}
51
52impl TryFrom<Bytes> for u8 {
53 type Error = Error;
54
55 fn try_from(value: Bytes) -> Result<Self, Self::Error> {
56 if None == value.get(0) {
58 return Err(Error::new(
59 ErrorKind::InvalidBufferSize,
60 Some("impl TryFrom<Bytes> for u8 requires Bytes of length of 1 byte.".to_string()),
61 ));
62 }
63
64 let mut arr = [0_u8; 1];
65 for (index, value) in value.0.iter().enumerate() {
66 arr[index] = *value;
67 }
68 Ok(u8::from_be_bytes(arr))
69 }
70}
71
72impl TryFrom<&Bytes> for u8 {
73 type Error = Error;
74
75 fn try_from(value: &Bytes) -> Result<Self, Self::Error> {
76 if None == value.get(0) {
78 return Err(Error::new(
79 ErrorKind::InvalidBufferSize,
80 Some("impl TryFrom<&Bytes> for u8 requires Bytes of length of 1 byte.".to_string()),
81 ));
82 }
83
84 let mut arr = [0_u8; 1];
85 for (index, value) in value.0.iter().enumerate() {
86 arr[index] = *value;
87 }
88 Ok(u8::from_be_bytes(arr))
89 }
90}
91
92impl TryFrom<Bytes> for u16 {
93 type Error = Error;
94
95 fn try_from(value: Bytes) -> Result<Self, Self::Error> {
96 if None == value.get(1) {
98 return Err(Error::new(
99 ErrorKind::InvalidBufferSize,
100 Some(
101 "impl TryFrom<Bytes> for u16 requires Bytes of length of 2 bytes.".to_string(),
102 ),
103 ));
104 }
105 let mut arr = [0_u8; 2];
106 for (index, value) in value.0.iter().enumerate() {
107 arr[index] = *value;
108 }
109 Ok(u16::from_be_bytes(arr))
110 }
111}
112
113impl TryFrom<&Bytes> for u16 {
114 type Error = Error;
115
116 fn try_from(value: &Bytes) -> Result<Self, Self::Error> {
117 if None == value.get(1) {
119 return Err(Error::new(
120 ErrorKind::InvalidBufferSize,
121 Some(
122 "impl TryFrom<Bytes> for u16 requires Bytes of length of 2 bytes.".to_string(),
123 ),
124 ));
125 }
126 let mut arr = [0_u8; 2];
127 for (index, value) in value.0.iter().enumerate() {
128 arr[index] = *value;
129 }
130 Ok(u16::from_be_bytes(arr))
131 }
132}
133
134impl TryFrom<Bytes> for u32 {
135 type Error = Error;
136
137 fn try_from(value: Bytes) -> Result<Self, Self::Error> {
138 if None == value.get(3) {
140 return Err(Error::new(
141 ErrorKind::InvalidBufferSize,
142 Some(
143 "impl TryFrom<Bytes> for u32 requires Bytes of length of 4 bytes.".to_string(),
144 ),
145 ));
146 }
147
148 let mut arr = [0_u8; 4];
149 for (index, value) in value.0.iter().enumerate() {
150 arr[index] = *value;
151 }
152 Ok(u32::from_be_bytes(arr))
153 }
154}
155
156impl TryFrom<&Bytes> for u32 {
157 type Error = Error;
158
159 fn try_from(value: &Bytes) -> Result<Self, Self::Error> {
160 if None == value.get(3) {
162 return Err(Error::new(
163 ErrorKind::InvalidBufferSize,
164 Some(
165 "impl TryFrom<Bytes> for u32 requires Bytes of length of 4 bytes.".to_string(),
166 ),
167 ));
168 }
169
170 let mut arr = [0_u8; 4];
171 for (index, value) in value.0.iter().enumerate() {
172 arr[index] = *value;
173 }
174 Ok(u32::from_be_bytes(arr))
175 }
176}
177
178impl TryFrom<Bytes> for String {
179 type Error = Error;
180
181 fn try_from(value: Bytes) -> Result<Self, Self::Error> {
182 let string = String::from_utf8_lossy(&value.0).into_owned();
183
184 Ok(string)
185 }
186}
187
188impl TryFrom<&Bytes> for String {
189 type Error = Error;
190
191 fn try_from(value: &Bytes) -> Result<Self, Self::Error> {
192 let string = String::from_utf8_lossy(&value.0).into_owned();
193
194 Ok(string)
195 }
196}
197
198impl TryFrom<Bytes> for DateTime<Utc> {
199 type Error = Error;
200
201 fn try_from(value: Bytes) -> Result<Self, Self::Error> {
202 if value.get(7).is_none() {
204 return Err(Error::new(
205 ErrorKind::InvalidBufferSize,
206 Some(
207 "impl TryFrom<Bytes> for DateTime<Utc> requires Bytes of length of 8 bytes."
208 .to_string(),
209 ),
210 ));
211 }
212
213 let naive_datetime = NaiveDateTime::from_timestamp(usize::try_from(value)? as i64, 0);
214
215 Ok(DateTime::from_utc(naive_datetime, Utc))
216 }
217}
218
219impl TryFrom<&Bytes> for DateTime<Utc> {
220 type Error = Error;
221
222 fn try_from(value: &Bytes) -> Result<Self, Self::Error> {
223 if value.get(7).is_none() {
224 return Err(Error::new(
225 ErrorKind::InvalidBufferSize,
226 Some(
227 "impl TryFrom<&Bytes> for DateTime<Utc> requires Bytes of length of 8 bytes."
228 .to_string(),
229 ),
230 ));
231 }
232
233 let naive_datetime = NaiveDateTime::from_timestamp(usize::try_from(value)? as i64, 0);
234
235 Ok(DateTime::from_utc(naive_datetime, Utc))
236 }
237}