2021-08-22 19:08:08 +02:00
|
|
|
use serde::Serialize;
|
2021-05-13 05:54:25 +00:00
|
|
|
use std::convert::TryFrom;
|
2018-03-24 15:46:28 +01:00
|
|
|
use std::fmt;
|
2018-03-22 21:53:36 -04:00
|
|
|
|
2021-08-22 17:52:55 +02:00
|
|
|
use rust_decimal::prelude::ToPrimitive;
|
|
|
|
|
use rust_decimal::Decimal;
|
|
|
|
|
|
2020-09-14 20:28:02 -07:00
|
|
|
#[derive(Debug, Clone, Serialize, Copy, PartialEq, Eq)]
|
2019-08-31 12:48:26 +05:30
|
|
|
#[serde(untagged)]
|
|
|
|
|
pub enum NumericType {
|
|
|
|
|
Int(i32),
|
|
|
|
|
Decimal(Decimal),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for NumericType {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
match *self {
|
|
|
|
|
NumericType::Int(i) => fmt::Display::fmt(&i, f),
|
|
|
|
|
NumericType::Decimal(i) => fmt::Display::fmt(&i, f),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 16:05:03 +02:00
|
|
|
impl From<i32> for NumericType {
|
|
|
|
|
fn from(x: i32) -> NumericType {
|
|
|
|
|
NumericType::Int(x)
|
2019-08-31 12:48:26 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 16:05:03 +02:00
|
|
|
impl From<Decimal> for NumericType {
|
|
|
|
|
fn from(x: Decimal) -> NumericType {
|
|
|
|
|
NumericType::Decimal(x)
|
2019-08-31 12:48:26 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 05:54:25 +00:00
|
|
|
impl TryFrom<NumericType> for usize {
|
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
|
|
fn try_from(numeric: NumericType) -> Result<Self, Self::Error> {
|
|
|
|
|
match numeric {
|
|
|
|
|
NumericType::Int(i) => usize::try_from(i).map_err(|_| ()),
|
|
|
|
|
NumericType::Decimal(d) => match d.to_u64() {
|
|
|
|
|
Some(u) => usize::try_from(u).map_err(|_| ()),
|
|
|
|
|
None => Err(()),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-14 20:28:02 -07:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2019-10-20 13:18:00 +02:00
|
|
|
pub enum ParameterValue {
|
|
|
|
|
Text(String),
|
2019-10-20 09:54:47 +02:00
|
|
|
Numeric(NumericType),
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 13:18:00 +02:00
|
|
|
impl<'a> ToString for ParameterValue {
|
2019-10-20 09:54:47 +02:00
|
|
|
fn to_string(&self) -> String {
|
|
|
|
|
match self {
|
2019-10-20 13:18:00 +02:00
|
|
|
ParameterValue::Text(ref value) => value.clone(),
|
2019-10-20 09:54:47 +02:00
|
|
|
ParameterValue::Numeric(value) => value.to_string(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|