reifydb_type/value/constraint/
bytes.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
3
4use std::fmt::{Display, Formatter};
5
6use serde::{Deserialize, Serialize};
7
8/// Maximum bytes constraint for types like UTF8, BLOB, INT, UINT
9#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
10#[repr(transparent)]
11pub struct MaxBytes(u32);
12
13impl MaxBytes {
14	/// Create a new MaxBytes value
15	pub fn new(bytes: u32) -> Self {
16		Self(bytes)
17	}
18
19	/// Get the max bytes value
20	pub fn value(self) -> u32 {
21		self.0
22	}
23
24	/// Maximum value (u32::MAX)
25	pub const MAX: Self = Self(u32::MAX);
26
27	/// Minimum value (0)
28	pub const MIN: Self = Self(0);
29}
30
31impl Display for MaxBytes {
32	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
33		self.0.fmt(f)
34	}
35}
36
37impl From<MaxBytes> for u32 {
38	fn from(max_bytes: MaxBytes) -> Self {
39		max_bytes.0
40	}
41}
42
43impl From<u32> for MaxBytes {
44	fn from(value: u32) -> Self {
45		Self::new(value)
46	}
47}
48
49impl From<MaxBytes> for usize {
50	fn from(max_bytes: MaxBytes) -> Self {
51		max_bytes.0 as usize
52	}
53}
54
55impl From<usize> for MaxBytes {
56	fn from(value: usize) -> Self {
57		Self::new(value as u32)
58	}
59}