Skip to main content

reifydb_value/error/
util.rs

1// SPDX-License-Identifier: MIT
2// Copyright (c) 2026 ReifyDB
3
4use crate::value::value_type::ValueType;
5
6pub fn value_max<'a>(value: ValueType) -> &'a str {
7	match value {
8		ValueType::Boolean => unreachable!(),
9		ValueType::Float4 => "+3.4e38",
10		ValueType::Float8 => "+1.8e308",
11		ValueType::Int1 => "127",
12		ValueType::Int2 => "32_767",
13		ValueType::Int4 => "2_147_483_647",
14		ValueType::Int8 => "9_223_372_036_854_775_807",
15		ValueType::Int16 => "170_141_183_460_469_231_731_687_303_715_884_105_727",
16		ValueType::Utf8 => unreachable!(),
17		ValueType::Uint1 => "255",
18		ValueType::Uint2 => "65_535",
19		ValueType::Uint4 => "4_294_967_295",
20		ValueType::Uint8 => "18_446_744_073_709_551_615",
21		ValueType::Uint16 => "340_282_366_920_938_463_463_374_607_431_768_211_455",
22		ValueType::Date => unreachable!(),
23		ValueType::DateTime => unreachable!(),
24		ValueType::Time => unreachable!(),
25		ValueType::Duration => unreachable!(),
26		ValueType::IdentityId => unreachable!(),
27		ValueType::Uuid4 => unreachable!(),
28		ValueType::Uuid7 => unreachable!(),
29		ValueType::Blob => unreachable!(),
30		ValueType::Int => "unlimited",
31		ValueType::Uint => "unlimited",
32		ValueType::Decimal => "unlimited",
33		ValueType::Option(_) => unreachable!(),
34		ValueType::Any => unreachable!(),
35		ValueType::DictionaryId => unreachable!(),
36		ValueType::List(_) => unreachable!(),
37		ValueType::Record(_) => unreachable!(),
38		ValueType::Tuple(_) => unreachable!(),
39	}
40}
41
42pub fn value_range<'a>(value: ValueType) -> &'a str {
43	match value {
44		ValueType::Boolean => unreachable!(),
45		ValueType::Float4 => "-3.4e38 to +3.4e38",
46		ValueType::Float8 => "-1.8e308 to +1.8e308",
47		ValueType::Int1 => "-128 to 127",
48		ValueType::Int2 => "-32_768 to 32_767",
49		ValueType::Int4 => "-2_147_483_648 to 2_147_483_647",
50		ValueType::Int8 => "-9_223_372_036_854_775_808 to 9_223_372_036_854_775_807",
51		ValueType::Int16 => {
52			"-170_141_183_460_469_231_731_687_303_715_884_105_728 to 170_141_183_460_469_231_731_687_303_715_884_105_727"
53		}
54		ValueType::Utf8 => unreachable!(),
55		ValueType::Uint1 => "0 to 255",
56		ValueType::Uint2 => "0 to 65_535",
57		ValueType::Uint4 => "0 to 4_294_967_295",
58		ValueType::Uint8 => "0 to 18_446_744_073_709_551_615",
59		ValueType::Uint16 => "0 to 340_282_366_920_938_463_463_374_607_431_768_211_455",
60		ValueType::Date => unreachable!(),
61		ValueType::DateTime => unreachable!(),
62		ValueType::Time => unreachable!(),
63		ValueType::Duration => unreachable!(),
64		ValueType::IdentityId => unreachable!(),
65		ValueType::Uuid4 => unreachable!(),
66		ValueType::Uuid7 => unreachable!(),
67		ValueType::Blob => unreachable!(),
68		ValueType::Int => "unlimited",
69		ValueType::Uint => "unlimited",
70		ValueType::Decimal => "unlimited",
71		ValueType::Option(_) => unreachable!(),
72		ValueType::Any => unreachable!(),
73		ValueType::DictionaryId => unreachable!(),
74		ValueType::List(_) => unreachable!(),
75		ValueType::Record(_) => unreachable!(),
76		ValueType::Tuple(_) => unreachable!(),
77	}
78}
79
80#[cfg(test)]
81pub mod tests {
82
83	mod value_max {
84		use crate::{error::util::value_max, value::value_type::ValueType};
85
86		#[test]
87		fn test_signed_ints() {
88			assert_eq!(value_max(ValueType::Int1), "127");
89			assert_eq!(value_max(ValueType::Int2), "32_767");
90			assert_eq!(value_max(ValueType::Int4), "2_147_483_647");
91			assert_eq!(value_max(ValueType::Int8), "9_223_372_036_854_775_807");
92			assert_eq!(value_max(ValueType::Int16), "170_141_183_460_469_231_731_687_303_715_884_105_727");
93		}
94
95		#[test]
96		fn test_unsigned_ints() {
97			assert_eq!(value_max(ValueType::Uint1), "255");
98			assert_eq!(value_max(ValueType::Uint2), "65_535");
99			assert_eq!(value_max(ValueType::Uint4), "4_294_967_295");
100			assert_eq!(value_max(ValueType::Uint8), "18_446_744_073_709_551_615");
101			assert_eq!(value_max(ValueType::Uint16), "340_282_366_920_938_463_463_374_607_431_768_211_455");
102		}
103
104		#[test]
105		fn test_floats() {
106			assert_eq!(value_max(ValueType::Float4), "+3.4e38");
107			assert_eq!(value_max(ValueType::Float8), "+1.8e308");
108		}
109	}
110
111	mod value_range {
112		use crate::{error::util::value_range, value::value_type::ValueType};
113
114		#[test]
115		fn test_signed_ints() {
116			assert_eq!(value_range(ValueType::Int1), "-128 to 127");
117			assert_eq!(value_range(ValueType::Int2), "-32_768 to 32_767");
118			assert_eq!(value_range(ValueType::Int4), "-2_147_483_648 to 2_147_483_647");
119			assert_eq!(
120				value_range(ValueType::Int8),
121				"-9_223_372_036_854_775_808 to 9_223_372_036_854_775_807"
122			);
123			assert_eq!(
124				value_range(ValueType::Int16),
125				"-170_141_183_460_469_231_731_687_303_715_884_105_728 to 170_141_183_460_469_231_731_687_303_715_884_105_727"
126			);
127		}
128
129		#[test]
130		fn test_unsigned_ints() {
131			assert_eq!(value_range(ValueType::Uint1), "0 to 255");
132			assert_eq!(value_range(ValueType::Uint2), "0 to 65_535");
133			assert_eq!(value_range(ValueType::Uint4), "0 to 4_294_967_295");
134			assert_eq!(value_range(ValueType::Uint8), "0 to 18_446_744_073_709_551_615");
135			assert_eq!(
136				value_range(ValueType::Uint16),
137				"0 to 340_282_366_920_938_463_463_374_607_431_768_211_455"
138			);
139		}
140
141		#[test]
142		fn test_floats() {
143			assert_eq!(value_range(ValueType::Float4), "-3.4e38 to +3.4e38");
144			assert_eq!(value_range(ValueType::Float8), "-1.8e308 to +1.8e308");
145		}
146	}
147}