use core::fmt;
use std::{
ops::{Deref, Index},
str::FromStr,
};
use yaserde::{DefaultYaSerde, PrimitiveYaSerde};
#[cfg(test)]
use crate::{deserialize, serialize};
use crate::traits::Validate;
#[derive(Default, Hash, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Debug, PrimitiveYaSerde)]
pub struct Uint8(pub u8);
impl Deref for Uint8 {
type Target = u8;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Validate for Uint8 {}
#[derive(Default, Hash, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Debug, PrimitiveYaSerde)]
pub struct Uint16(pub u16);
impl Deref for Uint16 {
type Target = u16;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Validate for Uint16 {}
#[derive(Default, Hash, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Debug, PrimitiveYaSerde)]
pub struct Uint32(pub u32);
impl Uint32 {
pub fn get(&self) -> u32 {
self.0
}
}
impl Validate for Uint32 {}
#[derive(Default, Hash, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Debug, PrimitiveYaSerde)]
pub struct Uint40(pub u64);
impl Deref for Uint40 {
type Target = u64;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Validate for Uint40 {
fn validate(&self) -> Result<(), String> {
if self.0 > "281474976710655".parse::<u64>().unwrap() {
return Err(format!("MaxInclusive validation error: invalid value of 0! \nExpected: 0 <= 281474976710655.\nActual: 0 == {}", self.0));
}
Ok(())
}
}
#[derive(Default, Hash, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Debug, PrimitiveYaSerde)]
pub struct Uint48(pub u64);
impl Deref for Uint48 {
type Target = u64;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Validate for Uint48 {
fn validate(&self) -> Result<(), String> {
if self.0 > "281474976710655".parse::<u64>().unwrap() {
return Err(format!("MaxInclusive validation error: invalid value of 0! \nExpected: 0 <= 281474976710655.\nActual: 0 == {}", self.0));
}
Ok(())
}
}
#[derive(Default, Hash, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Debug, PrimitiveYaSerde)]
pub struct Uint64(pub u64);
impl Deref for Uint64 {
type Target = u64;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Validate for Uint64 {}
#[derive(Default, Hash, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Debug, PrimitiveYaSerde)]
pub struct Int8(pub i8);
impl Deref for Int8 {
type Target = i8;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Validate for Int8 {}
#[derive(Default, Hash, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Debug, PrimitiveYaSerde)]
pub struct Int16(pub i16);
impl Deref for Int16 {
type Target = i16;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Validate for Int16 {}
#[derive(Default, Hash, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Debug, PrimitiveYaSerde)]
pub struct Int32(pub i32);
impl Deref for Int32 {
type Target = i32;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Validate for Int32 {}
#[derive(Default, Hash, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Debug, PrimitiveYaSerde)]
pub struct Int48(pub i64);
impl Deref for Int48 {
type Target = i64;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Validate for Int48 {
fn validate(&self) -> Result<(), String> {
if self.0 > "140737488355328".parse::<i64>().unwrap() {
return Err(format!("MaxInclusive validation error: invalid value! \nExpected: 0 <= 140737488355328.\nActual: 0 == {}", self.0));
}
if self.0 < "-140737488355328".parse::<i64>().unwrap() {
return Err(format!("MinInclusive validation error: invalid value! \nExpected: 0 >= -140737488355328.\nActual: 0 == {}", self.0));
}
Ok(())
}
}
#[derive(Default, Hash, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Debug, PrimitiveYaSerde)]
pub struct Int64(pub i64);
impl Int64 {
pub fn get(&self) -> i64 {
self.0
}
}
impl Validate for Int64 {}
#[derive(Default, Hash, PartialEq, Eq, Debug, Clone, Copy, DefaultYaSerde)]
pub struct HexBinary8(pub u8);
impl Validate for HexBinary8 {}
impl fmt::Display for HexBinary8 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:#04x?}", self.0)
}
}
impl FromStr for HexBinary8 {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(HexBinary8(
u8::from_str_radix(&s[2..], 16).map_err(|_| "could not parse hexbinary8")?,
))
}
}
#[derive(Default, Hash, PartialEq, PartialOrd, Eq, Ord, Debug, Clone, Copy, DefaultYaSerde)]
pub struct HexBinary16(pub u16);
impl Validate for HexBinary16 {}
impl fmt::Display for HexBinary16 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:#06x?}", self.0)
}
}
impl FromStr for HexBinary16 {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(HexBinary16(
u16::from_str_radix(&s[2..], 16).map_err(|_| "could not parse hexbinary16")?,
))
}
}
#[derive(Default, Hash, PartialEq, Eq, Debug, Clone, Copy, DefaultYaSerde)]
pub struct HexBinary32(pub u32);
impl Validate for HexBinary32 {}
impl fmt::Display for HexBinary32 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:#010x?}", self.0)
}
}
impl FromStr for HexBinary32 {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(HexBinary32(
u32::from_str_radix(&s[2..], 16).map_err(|_| "could not parse hexbinary32")?,
))
}
}
#[derive(Default, Hash, PartialEq, Eq, Debug, Clone, Copy, DefaultYaSerde)]
pub struct HexBinary48(pub u64);
impl Validate for HexBinary48 {
fn validate(&self) -> Result<(), String> {
let a = &self.0;
if a > &281474976710656 {
Err(format!("Validation error: invalid value! \nExpected: 0 <= 281474976710656.\nActual: 0 == {}", a))
} else {
Ok(())
}
}
}
impl fmt::Display for HexBinary48 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let a = &self.0;
write!(f, "{:#014x?}", a)
}
}
impl FromStr for HexBinary48 {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(HexBinary48(
u64::from_str_radix(&s[2..], 16).map_err(|_| "could not parse hexbinary48")?,
))
}
}
#[derive(Default, Hash, PartialEq, PartialOrd, Eq, Ord, Debug, Clone, Copy, DefaultYaSerde)]
pub struct HexBinary64(pub u64);
impl Validate for HexBinary64 {}
impl fmt::Display for HexBinary64 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let a = &self.0;
write!(f, "{:#018x?}", a)
}
}
impl FromStr for HexBinary64 {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(HexBinary64(
u64::from_str_radix(&s[2..], 16).map_err(|_| "could not parse hexbinary48")?,
))
}
}
#[derive(Default, Hash, PartialEq, PartialOrd, Eq, Ord, Debug, Clone, Copy, DefaultYaSerde)]
pub struct HexBinary128(pub u128);
impl Validate for HexBinary128 {}
impl fmt::Display for HexBinary128 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:#034x?}", self.0)
}
}
impl FromStr for HexBinary128 {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(HexBinary128(
u128::from_str_radix(&s[2..], 16).map_err(|_| "could not parse hexbinary128")?,
))
}
}
#[derive(Default, Hash, PartialEq, PartialOrd, Eq, Ord, Debug, Clone, Copy, DefaultYaSerde)]
pub struct HexBinary160(pub [u8; 20]); impl Validate for HexBinary160 {}
impl Index<usize> for HexBinary160 {
type Output = u8;
fn index(&self, index: usize) -> &Self::Output {
&self.0[index]
}
}
impl AsRef<[u8]> for HexBinary160 {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl fmt::Display for HexBinary160 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let a = self
.0
.iter()
.map(|byte| format!("{:02X}", byte))
.collect::<Vec<String>>()
.join("");
write!(f, "{a}")
}
}
impl FromStr for HexBinary160 {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() % 2 != 0 {
return Err("Hex string must be even".to_owned());
}
let mut out: [u8; 20] = [0; 20];
let s = format!("{:0>40}", s);
for i in (0..s.len()).step_by(2) {
let hex_pair = &s[i..(i + 2)];
if let Ok(byte) = u8::from_str_radix(hex_pair, 16) {
out[i / 2] = byte;
} else {
return Err("Invalid Hex String".to_owned());
}
}
Ok(HexBinary160(out))
}
}
#[derive(Default, Hash, PartialEq, Eq, Debug, Clone, DefaultYaSerde)]
pub struct LFDI(pub HexBinary160);
impl Validate for LFDI {}
impl fmt::Display for LFDI {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let hexstring = format!("{}", self.0);
write!(
f,
"{}",
hexstring
.chars()
.enumerate()
.flat_map(|(i, c)| {
if i > 0 && i % 4 == 0 { Some('-') } else { None }
.into_iter()
.chain(Some(c))
})
.collect::<String>()
)
}
}
impl FromStr for LFDI {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.replace('-', "");
Ok(LFDI(HexBinary160::from_str(&s)?))
}
}
#[derive(Default, Hash, PartialEq, Eq, PartialOrd, Ord, Debug, Clone, PrimitiveYaSerde)]
pub struct String6(pub String);
impl Validate for String6 {
fn validate(&self) -> Result<(), String> {
if self.0.len() > 6 {
return Err(format!(
"MaxLength validation error. \nExpected: 0 length <= 6 \nActual: 0 length == {}",
self.0.len()
));
}
Ok(())
}
}
#[derive(Default, Hash, PartialEq, Eq, PartialOrd, Ord, Debug, Clone, PrimitiveYaSerde)]
pub struct String16(pub String);
impl Validate for String16 {
fn validate(&self) -> Result<(), String> {
if self.0.len() > 16 {
return Err(format!(
"MaxLength validation error. \nExpected: 0 length <= 16 \nActual: 0 length == {}",
self.0.len()
));
}
Ok(())
}
}
#[derive(Default, Hash, PartialEq, PartialOrd, Eq, Ord, Debug, Clone, PrimitiveYaSerde)]
pub struct String20(pub String);
impl Validate for String20 {
fn validate(&self) -> Result<(), String> {
if self.0.len() > 20 {
return Err(format!(
"MaxLength validation error. \nExpected: 0 length <= 20 \nActual: 0 length == {}",
self.0.len()
));
}
Ok(())
}
}
#[derive(Default, Hash, PartialEq, PartialOrd, Eq, Ord, Debug, Clone, PrimitiveYaSerde)]
pub struct String32(pub String);
impl Validate for String32 {
fn validate(&self) -> Result<(), String> {
if self.0.len() > 32 {
return Err(format!(
"MaxLength validation error. \nExpected: 0 length <= 32 \nActual: 0 length == {}",
self.0.len()
));
}
Ok(())
}
}
#[derive(Default, Hash, PartialEq, PartialOrd, Eq, Ord, Debug, Clone, PrimitiveYaSerde)]
pub struct String42(pub String);
impl Validate for String42 {
fn validate(&self) -> Result<(), String> {
if self.0.len() > 42 {
return Err(format!(
"MaxLength validation error. \nExpected: 0 length <= 42 \nActual: 0 length == {}",
self.0.len()
));
}
Ok(())
}
}
#[derive(Default, Hash, PartialEq, Eq, Debug, Clone, PrimitiveYaSerde)]
pub struct String192(pub String);
impl Validate for String192 {
fn validate(&self) -> Result<(), String> {
if self.0.len() > 192 {
return Err(format!(
"MaxLength validation error. \nExpected: 0 length <= 192 \nActual: 0 length == {}",
self.0.len()
));
}
Ok(())
}
}
#[test]
fn default_hexbinary8() {
let orig = HexBinary8::default();
let new: HexBinary8 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_hexbinary16() {
let orig = HexBinary16::default();
let new: HexBinary16 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_hexbinary32() {
let orig = HexBinary32::default();
let new: HexBinary32 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_hexbinary48() {
let orig = HexBinary48::default();
let new: HexBinary48 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_hexbinary64() {
let orig = HexBinary64::default();
let new: HexBinary64 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_hexbinary128() {
let orig = HexBinary128::default();
let new: HexBinary128 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_hexbinary160() {
let orig = HexBinary160::default();
let new: HexBinary160 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_lfdi() {
let orig = LFDI::default();
let new: LFDI = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn example_hexbinary160() {
let orig: HexBinary160 = HexBinary160::from_str("C0FFEE00").unwrap();
let new = orig.to_string();
assert_eq!(orig, HexBinary160::from_str(&new).unwrap());
}
#[test]
fn example_lfdi() {
let orig: LFDI = LFDI::from_str("C0FFEE00").unwrap();
let new = orig.to_string();
assert_eq!(orig, LFDI::from_str(&new).unwrap());
}
#[test]
fn default_uint8() {
let orig = Uint8::default();
let new: Uint8 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_uint16() {
let orig = Uint16::default();
let new: Uint16 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_uint32() {
let orig = Uint32::default();
let new: Uint32 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_uint40() {
let orig = Uint40::default();
let new: Uint40 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_uint48() {
let orig = Uint48::default();
let new: Uint48 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_uint64() {
let orig = Uint64::default();
let new: Uint64 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_int8() {
let orig = Int8::default();
let new: Int8 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_int16() {
let orig = Int16::default();
let new: Int16 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_int32() {
let orig = Int32::default();
let new: Int32 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_int48() {
let orig = Int48::default();
let new: Int48 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_int64() {
let orig = Int64::default();
let new: Int64 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_string6() {
let orig = String6::default();
let new: String6 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_string16() {
let orig = String16::default();
let new: String16 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_string20() {
let orig = String20::default();
let new: String20 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_string32() {
let orig = String32::default();
let new: String32 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_string42() {
let orig = String42::default();
let new: String42 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}
#[test]
fn default_string192() {
let orig = String192::default();
let new: String192 = deserialize(&serialize(&orig).unwrap()).unwrap();
assert_eq!(orig, new);
}