reifydb_type/value/constraint/
precision.rs1use std::fmt::{self, Display, Formatter};
5
6use serde::{Deserialize, Serialize};
7
8use crate::error::{Error, TypeError};
9
10#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
11#[repr(transparent)]
12pub struct Precision(u8);
13
14impl Precision {
15 pub fn new(precision: u8) -> Self {
16 assert!(precision > 0, "Precision must be at least 1, got {}", precision);
17 Self(precision)
18 }
19
20 pub fn try_new(precision: u8) -> Result<Self, Error> {
21 if precision == 0 {
22 return Err(TypeError::DecimalPrecisionInvalid {
23 precision,
24 }
25 .into());
26 }
27 Ok(Self(precision))
28 }
29
30 pub fn value(self) -> u8 {
31 self.0
32 }
33
34 pub const MAX: Self = Self(255);
35
36 pub const MIN: Self = Self(1);
37}
38
39impl Display for Precision {
40 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
41 self.0.fmt(f)
42 }
43}
44
45impl From<Precision> for u8 {
46 fn from(precision: Precision) -> Self {
47 precision.0
48 }
49}
50
51impl From<u8> for Precision {
52 fn from(value: u8) -> Self {
53 Self::new(value)
54 }
55}