Skip to main content

arbiter/
primitives.rs

1use serde::{Deserialize, Serialize};
2
3/// A confidence score in [0.0, 1.0].
4#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize)]
5#[serde(transparent)]
6pub struct Confidence(f64);
7
8impl Confidence {
9    pub fn new(v: f64) -> Result<Self, String> {
10        if (0.0..=1.0).contains(&v) {
11            Ok(Self(v))
12        } else {
13            Err(format!("confidence {v} is outside [0.0, 1.0]"))
14        }
15    }
16    pub fn value(self) -> f64 {
17        self.0
18    }
19    /// Clamp rather than reject — useful for internal construction from trusted sources.
20    pub fn clamped(v: f64) -> Self {
21        Self(v.clamp(0.0, 1.0))
22    }
23}
24
25/// A non-negative cost in USD.
26#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize)]
27#[serde(transparent)]
28pub struct CostUsd(f64);
29
30impl CostUsd {
31    pub fn new(v: f64) -> Result<Self, String> {
32        if v >= 0.0 {
33            Ok(Self(v))
34        } else {
35            Err(format!("cost {v} must be >= 0.0"))
36        }
37    }
38    pub fn value(self) -> f64 {
39        self.0
40    }
41    pub fn zero() -> Self {
42        Self(0.0)
43    }
44    /// Clamp to non-negative — useful for internal construction from trusted sources.
45    pub fn clamped(v: f64) -> Self {
46        Self(v.max(0.0))
47    }
48}
49
50/// Current number of proposals in a rate window.
51#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
52#[serde(transparent)]
53pub struct ProposalCount(pub usize);
54
55/// Maximum allowed proposals in a rate window.
56#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
57#[serde(transparent)]
58pub struct ProposalLimit(pub usize);
59
60/// Unix epoch timestamp in seconds.
61#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
62#[serde(transparent)]
63pub struct EpochSeconds(pub i64);