use derive_more::Constructor;
use serde::{Deserialize, Serialize};
use thiserror::Error;
pub mod util;
pub trait RiskCheck {
type Input;
type Error;
fn name() -> &'static str;
fn check(&self, input: &Self::Input) -> Result<(), Self::Error>;
}
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Deserialize, Serialize, Constructor)]
pub struct CheckHigherThan<T> {
pub limit: T,
}
impl<T> RiskCheck for CheckHigherThan<T>
where
T: Clone + PartialOrd,
{
type Input = T;
type Error = CheckFailHigherThan<T>;
fn name() -> &'static str {
"CheckHigherThan"
}
fn check(&self, input: &Self::Input) -> Result<(), Self::Error> {
if *input <= self.limit {
Ok(())
} else {
Err(CheckFailHigherThan {
limit: self.limit.clone(),
input: input.clone(),
})
}
}
}
#[derive(
Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Deserialize, Serialize, Constructor, Error,
)]
#[error("CheckHigherThanFailed: input {input} > limit {limit}")]
pub struct CheckFailHigherThan<T> {
pub limit: T,
pub input: T,
}