use serde::{Deserialize, Serialize};
use std::{
cmp::Ordering, error::Error, fmt::{self, Display}, str::FromStr
};
use super::AmadeusOrd;
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Debug)]
pub enum Decimal {
Int32 {
value: [u8; 4],
precision: i32,
scale: i32,
},
Int64 {
value: [u8; 8],
precision: i32,
scale: i32,
},
Bytes {
value: Vec<u8>,
precision: i32,
scale: i32,
},
}
impl Decimal {
pub fn from_bytes(value: Vec<u8>, precision: i32, scale: i32) -> Self {
Self::Bytes {
value,
precision,
scale,
}
}
pub fn data(&self) -> &[u8] {
match *self {
Self::Int32 { ref value, .. } => value,
Self::Int64 { ref value, .. } => value,
Self::Bytes { ref value, .. } => value,
}
}
pub fn precision(&self) -> i32 {
match *self {
Self::Int32 { precision, .. }
| Self::Int64 { precision, .. }
| Self::Bytes { precision, .. } => precision,
}
}
pub fn scale(&self) -> i32 {
match *self {
Self::Int32 { scale, .. } | Self::Int64 { scale, .. } | Self::Bytes { scale, .. } => {
scale
}
}
}
}
impl AmadeusOrd for Decimal {
fn amadeus_cmp(&self, other: &Self) -> Ordering {
Ord::cmp(self, other)
}
}
impl Display for Decimal {
fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result {
unimplemented!()
}
}
impl FromStr for Decimal {
type Err = ParseDecimalError;
fn from_str(_s: &str) -> Result<Self, Self::Err> {
unimplemented!()
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ParseDecimalError;
impl Display for ParseDecimalError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "error parsing decimal")
}
}
impl Error for ParseDecimalError {}