Files
hyperfine/src/types.rs
T

63 lines
1.5 KiB
Rust
Raw Normal View History

use std::convert::TryFrom;
use std::fmt;
2021-08-22 17:52:55 +02:00
use rust_decimal::prelude::ToPrimitive;
use rust_decimal::Decimal;
2021-08-24 21:33:48 +02:00
use serde::Serialize;
2021-08-22 17:52:55 +02: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
}
}
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(()),
},
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
2019-10-20 13:18:00 +02:00
pub enum ParameterValue {
Text(String),
Numeric(NumericType),
}
2019-10-20 13:18:00 +02:00
impl<'a> ToString for ParameterValue {
fn to_string(&self) -> String {
match self {
2019-10-20 13:18:00 +02:00
ParameterValue::Text(ref value) => value.clone(),
ParameterValue::Numeric(value) => value.to_string(),
}
}
}