pub trait ContractContext {
fn poll_valid(&self) -> bool {
true
}
}
#[derive(Debug)]
pub enum ContextErrorKind {
ExpiredContext,
}
#[derive(Debug)]
pub struct ContextError {
kind: ContextErrorKind,
}
impl ContextError {
pub fn from(kind: ContextErrorKind) -> Self {
Self { kind }
}
}
impl std::fmt::Display for ContextError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use ContextErrorKind::*;
let error = match &self.kind {
k @ ExpiredContext => {
format!("{:?}: context is no longer available in this contract", k)
}
};
write!(f, "{}", error)
}
}
impl std::error::Error for ContextError {}
impl ContractContext for bool {
fn poll_valid(&self) -> bool {
*self
}
}
impl ContractContext for () {}
impl ContractContext for u8 {}
impl ContractContext for u16 {}
impl ContractContext for u32 {}
impl ContractContext for u64 {}
impl ContractContext for u128 {}
impl ContractContext for usize {}
impl ContractContext for i8 {}
impl ContractContext for i16 {}
impl ContractContext for i32 {}
impl ContractContext for i64 {}
impl ContractContext for i128 {}
impl ContractContext for isize {}
#[derive(Copy, Clone)]
pub struct DefaultContext;
impl ContractContext for DefaultContext {}
impl Default for DefaultContext {
fn default() -> Self {
Self::new()
}
}
impl DefaultContext {
pub fn new() -> Self {
Self
}
}
pub mod cmp {
use super::ContractContext;
pub struct EqContext<A, B>(pub A, pub B);
impl<A, B> ContractContext for EqContext<A, B>
where
A: PartialEq<B>,
{
fn poll_valid(&self) -> bool {
self.0 == self.1
}
}
pub struct NqContext<A, B>(pub A, pub B);
impl<A, B> ContractContext for NqContext<A, B>
where
A: PartialEq<B>,
{
fn poll_valid(&self) -> bool {
self.0 != self.1
}
}
pub struct LtContext<A>(pub A, pub A);
impl<A> ContractContext for LtContext<A>
where
A: Ord,
{
fn poll_valid(&self) -> bool {
self.0 < self.1
}
}
pub struct LeContext<A>(pub A, pub A);
impl<A> ContractContext for LeContext<A>
where
A: Ord,
{
fn poll_valid(&self) -> bool {
self.0 <= self.1
}
}
pub struct GtContext<A>(pub A, pub A);
impl<A> ContractContext for GtContext<A>
where
A: Ord,
{
fn poll_valid(&self) -> bool {
self.0 > self.1
}
}
pub struct GeContext<A>(pub A, pub A);
impl<A> ContractContext for GeContext<A>
where
A: Ord,
{
fn poll_valid(&self) -> bool {
self.0 > self.1
}
}
}