Skip to main content

titan_api_types/common/
bounded.rs

1//! Contains helper types used to represent bounded values, with and without defaults.
2//!
3//! Mainly used to represent settings on the server and parameter bounds in requests.
4
5#[cfg(feature = "serde")]
6use serde::{Deserialize, Serialize};
7use std::fmt::Debug;
8
9/// A set of bounds on a value.
10///
11/// Used in the API to return bounds settings.
12pub struct BoundedValue<T> {
13    /// Minimum allowed value for this parameter.
14    pub min: T,
15    /// Maximum allowed value for this parameter.
16    pub max: T,
17}
18
19impl<T> BoundedValue<T> {
20    /// Creates a new value with the given bounds.
21    pub const fn new(min: T, max: T) -> Self {
22        Self { min, max }
23    }
24
25    /// Returns whether the given value is within the bounds.
26    #[inline]
27    pub fn contains<V>(&self, value: V) -> bool
28    where
29        V: PartialOrd<T>,
30    {
31        (value >= self.min) && (value <= self.max)
32    }
33}
34
35/// Implement [`Debug`] if inner type implements it.
36impl<T> Debug for BoundedValue<T>
37where
38    T: Debug,
39{
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        f.debug_struct("BoundedValue")
42            .field("min", &self.min)
43            .field("max", &self.max)
44            .finish()
45    }
46}
47
48/// Implement [`Default`] if inner type implements it.
49impl<T> Default for BoundedValue<T>
50where
51    T: Default,
52{
53    fn default() -> Self {
54        Self {
55            min: Default::default(),
56            max: Default::default(),
57        }
58    }
59}
60
61/// Implement [`Clone`] if inner type implements it.
62impl<T> Clone for BoundedValue<T>
63where
64    T: Clone,
65{
66    fn clone(&self) -> Self {
67        Self {
68            min: self.min.clone(),
69            max: self.max.clone(),
70        }
71    }
72}
73
74/// Implement [`Copy`] if inner type implements it.
75impl<T> Copy for BoundedValue<T> where T: Copy {}
76
77/// Implement [`PartialEq`] if inner type implements it.
78impl<T> PartialEq for BoundedValue<T>
79where
80    T: PartialEq,
81{
82    fn eq(&self, other: &Self) -> bool {
83        (self.min == other.min) && (self.max == other.max)
84    }
85}
86
87impl<T> Eq for BoundedValue<T> where T: Eq {}
88
89/// A set of bounds and default value.
90///
91/// Used in the API to return bounds and defaults for optional settings.
92#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
93pub struct BoundedValueWithDefault<T> {
94    /// Minimum allowed value for this parameter.
95    pub min: T,
96    /// Maximum allowed value for this parameter.
97    pub max: T,
98    /// Default value for this parameter.
99    pub default: T,
100}
101
102impl<T> BoundedValueWithDefault<T> {
103    /// Creates a new value with the given bounds and default.
104    pub const fn new(min: T, max: T, default: T) -> Self {
105        Self { min, max, default }
106    }
107
108    /// Returns whether the given value is within the bounds.
109    #[inline]
110    pub fn contains<V>(&self, value: V) -> bool
111    where
112        V: PartialOrd<T>,
113    {
114        (value >= self.min) && (value <= self.max)
115    }
116}
117
118/// Implement [`Debug`] if inner type implements it.
119impl<T> Debug for BoundedValueWithDefault<T>
120where
121    T: Debug,
122{
123    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
124        f.debug_struct("BoundedValueWithDefault")
125            .field("min", &self.min)
126            .field("max", &self.max)
127            .field("default", &self.default)
128            .finish()
129    }
130}
131
132/// Implement [`Default`] if inner type implements it.
133impl<T> Default for BoundedValueWithDefault<T>
134where
135    T: Default,
136{
137    fn default() -> Self {
138        Self {
139            min: Default::default(),
140            max: Default::default(),
141            default: Default::default(),
142        }
143    }
144}
145
146/// Implement [`Clone`] if inner type implements it.
147impl<T> Clone for BoundedValueWithDefault<T>
148where
149    T: Clone,
150{
151    fn clone(&self) -> Self {
152        Self {
153            min: self.min.clone(),
154            max: self.max.clone(),
155            default: self.default.clone(),
156        }
157    }
158}
159
160/// Implement [`Copy`] if inner type implements it.
161impl<T> Copy for BoundedValueWithDefault<T> where T: Copy {}
162
163/// Implement [`PartialEq`] if inner type implements it.
164impl<T> PartialEq for BoundedValueWithDefault<T>
165where
166    T: PartialEq,
167{
168    fn eq(&self, other: &Self) -> bool {
169        (self.min == other.min) && (self.max == other.max) && (self.default == other.default)
170    }
171}
172
173impl<T> Eq for BoundedValueWithDefault<T> where T: Eq {}