Skip to main content

reifydb_type/error/
util.rs

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