1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//! Custom types for various standard library types.

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// A Uint that allows us to do math but rounds to a uint.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize, ts_rs::TS)]
#[ts(export)]
pub struct Uint(f64);

impl Uint {
    pub fn new(value: f64) -> Self {
        if value < 0.0 {
            panic!("Uint cannot be negative");
        }
        Self(value)
    }

    pub fn value(&self) -> f64 {
        self.0
    }

    pub fn u32(&self) -> u32 {
        self.0.round() as u32
    }

    pub fn u64(&self) -> u64 {
        self.0.round() as u64
    }
}

impl JsonSchema for Uint {
    fn schema_name() -> String {
        "Uint".to_string()
    }

    fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        gen.subschema_for::<u32>()
    }
}