nardol/bytes/
from_bytes.rsuse chrono::{DateTime, NaiveDateTime, Utc};
use crate::bytes::Bytes;
use crate::error::{Error, ErrorKind};
impl TryFrom<Bytes> for usize {
type Error = Error;
fn try_from(value: Bytes) -> Result<Self, Self::Error> {
if None == value.get(7) {
return Err(Error::new(
ErrorKind::InvalidBufferSize,
Some(
"impl TryFrom<Bytes> for usize requires Bytes of length of 8 bytes."
.to_string(),
),
));
}
let mut arr = [0_u8; 8];
for (index, value) in value.0.iter().enumerate() {
arr[index] = *value;
}
Ok(usize::from_be_bytes(arr))
}
}
impl TryFrom<&Bytes> for usize {
type Error = Error;
fn try_from(value: &Bytes) -> Result<Self, Self::Error> {
if None == value.get(7) {
return Err(Error::new(
ErrorKind::InvalidBufferSize,
Some(
"impl TryFrom<&Bytes> for usize requires Bytes of length of 8 bytes."
.to_string(),
),
));
}
let mut arr = [0_u8; 8];
for (index, value) in value.0.iter().enumerate() {
arr[index] = *value;
}
Ok(usize::from_be_bytes(arr))
}
}
impl TryFrom<Bytes> for u8 {
type Error = Error;
fn try_from(value: Bytes) -> Result<Self, Self::Error> {
if None == value.get(0) {
return Err(Error::new(
ErrorKind::InvalidBufferSize,
Some("impl TryFrom<Bytes> for u8 requires Bytes of length of 1 byte.".to_string()),
));
}
let mut arr = [0_u8; 1];
for (index, value) in value.0.iter().enumerate() {
arr[index] = *value;
}
Ok(u8::from_be_bytes(arr))
}
}
impl TryFrom<&Bytes> for u8 {
type Error = Error;
fn try_from(value: &Bytes) -> Result<Self, Self::Error> {
if None == value.get(0) {
return Err(Error::new(
ErrorKind::InvalidBufferSize,
Some("impl TryFrom<&Bytes> for u8 requires Bytes of length of 1 byte.".to_string()),
));
}
let mut arr = [0_u8; 1];
for (index, value) in value.0.iter().enumerate() {
arr[index] = *value;
}
Ok(u8::from_be_bytes(arr))
}
}
impl TryFrom<Bytes> for u16 {
type Error = Error;
fn try_from(value: Bytes) -> Result<Self, Self::Error> {
if None == value.get(1) {
return Err(Error::new(
ErrorKind::InvalidBufferSize,
Some(
"impl TryFrom<Bytes> for u16 requires Bytes of length of 2 bytes.".to_string(),
),
));
}
let mut arr = [0_u8; 2];
for (index, value) in value.0.iter().enumerate() {
arr[index] = *value;
}
Ok(u16::from_be_bytes(arr))
}
}
impl TryFrom<&Bytes> for u16 {
type Error = Error;
fn try_from(value: &Bytes) -> Result<Self, Self::Error> {
if None == value.get(1) {
return Err(Error::new(
ErrorKind::InvalidBufferSize,
Some(
"impl TryFrom<Bytes> for u16 requires Bytes of length of 2 bytes.".to_string(),
),
));
}
let mut arr = [0_u8; 2];
for (index, value) in value.0.iter().enumerate() {
arr[index] = *value;
}
Ok(u16::from_be_bytes(arr))
}
}
impl TryFrom<Bytes> for u32 {
type Error = Error;
fn try_from(value: Bytes) -> Result<Self, Self::Error> {
if None == value.get(3) {
return Err(Error::new(
ErrorKind::InvalidBufferSize,
Some(
"impl TryFrom<Bytes> for u32 requires Bytes of length of 4 bytes.".to_string(),
),
));
}
let mut arr = [0_u8; 4];
for (index, value) in value.0.iter().enumerate() {
arr[index] = *value;
}
Ok(u32::from_be_bytes(arr))
}
}
impl TryFrom<&Bytes> for u32 {
type Error = Error;
fn try_from(value: &Bytes) -> Result<Self, Self::Error> {
if None == value.get(3) {
return Err(Error::new(
ErrorKind::InvalidBufferSize,
Some(
"impl TryFrom<Bytes> for u32 requires Bytes of length of 4 bytes.".to_string(),
),
));
}
let mut arr = [0_u8; 4];
for (index, value) in value.0.iter().enumerate() {
arr[index] = *value;
}
Ok(u32::from_be_bytes(arr))
}
}
impl TryFrom<Bytes> for String {
type Error = Error;
fn try_from(value: Bytes) -> Result<Self, Self::Error> {
let string = String::from_utf8_lossy(&value.0).into_owned();
Ok(string)
}
}
impl TryFrom<&Bytes> for String {
type Error = Error;
fn try_from(value: &Bytes) -> Result<Self, Self::Error> {
let string = String::from_utf8_lossy(&value.0).into_owned();
Ok(string)
}
}
impl TryFrom<Bytes> for DateTime<Utc> {
type Error = Error;
fn try_from(value: Bytes) -> Result<Self, Self::Error> {
if value.get(7).is_none() {
return Err(Error::new(
ErrorKind::InvalidBufferSize,
Some(
"impl TryFrom<Bytes> for DateTime<Utc> requires Bytes of length of 8 bytes."
.to_string(),
),
));
}
let naive_datetime = NaiveDateTime::from_timestamp(usize::try_from(value)? as i64, 0);
Ok(DateTime::from_utc(naive_datetime, Utc))
}
}
impl TryFrom<&Bytes> for DateTime<Utc> {
type Error = Error;
fn try_from(value: &Bytes) -> Result<Self, Self::Error> {
if value.get(7).is_none() {
return Err(Error::new(
ErrorKind::InvalidBufferSize,
Some(
"impl TryFrom<&Bytes> for DateTime<Utc> requires Bytes of length of 8 bytes."
.to_string(),
),
));
}
let naive_datetime = NaiveDateTime::from_timestamp(usize::try_from(value)? as i64, 0);
Ok(DateTime::from_utc(naive_datetime, Utc))
}
}