croncat_mod_generic/
value_ordering.rs

1use cosmwasm_schema::cw_serde;
2
3use cosmwasm_std::{StdError, StdResult, Uint512};
4use serde_cw_value::Value;
5
6#[cw_serde]
7pub enum ValueOrdering {
8    UnitAbove,
9    UnitAboveEqual,
10    UnitBelow,
11    UnitBelowEqual,
12    Equal,
13    NotEqual,
14}
15
16impl ValueOrdering {
17    pub fn val_cmp(&self, lhs: &Value, rhs: &Value) -> StdResult<bool> {
18        let res = match self {
19            ValueOrdering::UnitAbove => lhs.bt_g(rhs)?,
20            ValueOrdering::UnitAboveEqual => lhs.be_g(rhs)?,
21            ValueOrdering::UnitBelow => lhs.lt_g(rhs)?,
22            ValueOrdering::UnitBelowEqual => lhs.le_g(rhs)?,
23            ValueOrdering::Equal => lhs.eq(rhs),
24            ValueOrdering::NotEqual => lhs.ne(rhs),
25        };
26        Ok(res)
27    }
28}
29
30pub trait ValueOrd {
31    fn lt_g(&self, other: &Self) -> StdResult<bool>;
32    fn le_g(&self, other: &Self) -> StdResult<bool>;
33    fn bt_g(&self, other: &Self) -> StdResult<bool>;
34    fn be_g(&self, other: &Self) -> StdResult<bool>;
35    fn equal(&self, other: &Self) -> bool;
36}
37
38/// Only supporting numbers and big numbers for now
39impl ValueOrd for Value {
40    fn lt_g(&self, other: &Self) -> StdResult<bool> {
41        match (self, other) {
42            (Value::String(str_num), Value::String(oth)) => {
43                let big_num: Uint512 = str_num.parse()?;
44                let big_oth: Uint512 = oth.parse()?;
45                Ok(big_num < big_oth)
46            }
47            (Value::U64(n), Value::U64(o)) => Ok(n < o),
48            (Value::U32(n), Value::U32(o)) => Ok(n < o),
49            (Value::U16(n), Value::U16(o)) => Ok(n < o),
50            (Value::U8(n), Value::U8(o)) => Ok(n < o),
51            _ => Err(StdError::parse_err(
52                "number",
53                "Failed to parse to Uint512 and to u64",
54            )),
55        }
56    }
57
58    fn le_g(&self, other: &Self) -> StdResult<bool> {
59        match (self, other) {
60            (Value::String(str_num), Value::String(oth)) => {
61                let big_num: Uint512 = str_num.parse()?;
62                let big_oth: Uint512 = oth.parse()?;
63                Ok(big_num <= big_oth)
64            }
65            (Value::U64(n), Value::U64(o)) => Ok(n <= o),
66            (Value::U32(n), Value::U32(o)) => Ok(n <= o),
67            (Value::U16(n), Value::U16(o)) => Ok(n <= o),
68            (Value::U8(n), Value::U8(o)) => Ok(n <= o),
69            _ => Err(StdError::parse_err(
70                "number",
71                "Failed to parse to Uint512 and to u64",
72            )),
73        }
74    }
75
76    fn bt_g(&self, other: &Self) -> StdResult<bool> {
77        match (self, other) {
78            (Value::String(str_num), Value::String(oth)) => {
79                let big_num: Uint512 = str_num.parse()?;
80                let big_oth: Uint512 = oth.parse()?;
81                Ok(big_num > big_oth)
82            }
83            (Value::U64(n), Value::U64(o)) => Ok(n > o),
84            (Value::U32(n), Value::U32(o)) => Ok(n > o),
85            (Value::U16(n), Value::U16(o)) => Ok(n > o),
86            (Value::U8(n), Value::U8(o)) => Ok(n > o),
87            _ => Err(StdError::parse_err(
88                "number",
89                "Failed to parse to Uint512 and to u64",
90            )),
91        }
92    }
93
94    fn be_g(&self, other: &Self) -> StdResult<bool> {
95        match (self, other) {
96            (Value::String(str_num), Value::String(oth)) => {
97                let big_num: Uint512 = str_num.parse()?;
98                let big_oth: Uint512 = oth.parse()?;
99                Ok(big_num >= big_oth)
100            }
101            (Value::U64(n), Value::U64(o)) => Ok(n >= o),
102            (Value::U32(n), Value::U32(o)) => Ok(n >= o),
103            (Value::U16(n), Value::U16(o)) => Ok(n >= o),
104            (Value::U8(n), Value::U8(o)) => Ok(n >= o),
105            _ => Err(StdError::parse_err(
106                "number",
107                "Failed to parse to Uint512 and to u64",
108            )),
109        }
110    }
111
112    fn equal(&self, other: &Self) -> bool {
113        self.eq(other)
114    }
115}