reifydb_type/value/
mod.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the MIT, see license.md file
3
4use std::{
5	cmp::Ordering,
6	fmt::{Display, Formatter},
7};
8
9use serde::{Deserialize, Serialize};
10
11mod as_string;
12pub mod blob;
13pub mod boolean;
14pub mod constraint;
15mod date;
16mod datetime;
17pub mod decimal;
18mod duration;
19mod identity;
20pub mod int;
21mod into;
22pub mod is;
23pub mod number;
24mod ordered_f32;
25mod ordered_f64;
26pub mod row_number;
27pub mod temporal;
28mod time;
29mod r#type;
30pub mod uint;
31pub mod uuid;
32
33pub use blob::Blob;
34pub use constraint::{Constraint, TypeConstraint};
35pub use date::Date;
36pub use datetime::DateTime;
37pub use decimal::Decimal;
38pub use duration::Duration;
39pub use identity::IdentityId;
40pub use int::Int;
41pub use into::IntoValue;
42pub use ordered_f32::OrderedF32;
43pub use ordered_f64::OrderedF64;
44pub use row_number::RowNumber;
45pub use time::Time;
46pub use r#type::{GetType, Type};
47pub use uint::Uint;
48pub use uuid::{Uuid4, Uuid7};
49
50/// A RQL value, represented as a native Rust type.
51#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
52pub enum Value {
53	/// Value is not defined (think null in common programming languages)
54	Undefined,
55	/// A boolean: true or false.
56	Boolean(bool),
57	/// A 4-byte floating point
58	Float4(OrderedF32),
59	/// An 8-byte floating point
60	Float8(OrderedF64),
61	/// A 1-byte signed integer
62	Int1(i8),
63	/// A 2-byte signed integer
64	Int2(i16),
65	/// A 4-byte signed integer
66	Int4(i32),
67	/// An 8-byte signed integer
68	Int8(i64),
69	/// A 16-byte signed integer
70	Int16(i128),
71	/// A UTF-8 encoded text. Maximum 255 bytes
72	Utf8(String),
73	/// A 1-byte unsigned integer
74	Uint1(u8),
75	/// A 2-byte unsigned integer
76	Uint2(u16),
77	/// A 4-byte unsigned integer
78	Uint4(u32),
79	/// A 8-byte unsigned integer
80	Uint8(u64),
81	/// A 16-byte unsigned integer
82	Uint16(u128),
83	/// A date value (year, month, day)
84	Date(Date),
85	/// A date and time value with nanosecond precision in SVTC
86	DateTime(DateTime),
87	/// A time value (hour, minute, second, nanosecond)
88	Time(Time),
89	/// A duration representing a duration
90	Duration(Duration),
91	/// A encoded number (8-byte unsigned integer)
92	RowNumber(RowNumber),
93	/// An identity identifier (UUID v7)
94	IdentityId(IdentityId),
95	/// A UUID version 4 (random)
96	Uuid4(Uuid4),
97	/// A UUID version 7 (timestamp-based)
98	Uuid7(Uuid7),
99	/// A binary large object (BLOB)
100	Blob(Blob),
101	/// An arbitrary-precision signed integer
102	Int(Int),
103	/// An arbitrary-precision unsigned integer
104	Uint(Uint),
105	/// An arbitrary-precision decimal
106	Decimal(Decimal),
107	/// A container that can hold any value type
108	Any(Box<Value>),
109}
110
111impl Value {
112	pub fn undefined() -> Self {
113		Value::Undefined
114	}
115
116	pub fn bool(v: impl Into<bool>) -> Self {
117		Value::Boolean(v.into())
118	}
119
120	pub fn float4(v: impl Into<f32>) -> Self {
121		OrderedF32::try_from(v.into()).map(Value::Float4).unwrap_or(Value::Undefined)
122	}
123
124	pub fn float8(v: impl Into<f64>) -> Self {
125		OrderedF64::try_from(v.into()).map(Value::Float8).unwrap_or(Value::Undefined)
126	}
127
128	pub fn int1(v: impl Into<i8>) -> Self {
129		Value::Int1(v.into())
130	}
131
132	pub fn int2(v: impl Into<i16>) -> Self {
133		Value::Int2(v.into())
134	}
135
136	pub fn int4(v: impl Into<i32>) -> Self {
137		Value::Int4(v.into())
138	}
139
140	pub fn int8(v: impl Into<i64>) -> Self {
141		Value::Int8(v.into())
142	}
143
144	pub fn int16(v: impl Into<i128>) -> Self {
145		Value::Int16(v.into())
146	}
147
148	pub fn utf8(v: impl Into<String>) -> Self {
149		Value::Utf8(v.into())
150	}
151
152	pub fn uint1(v: impl Into<u8>) -> Self {
153		Value::Uint1(v.into())
154	}
155
156	pub fn uint2(v: impl Into<u16>) -> Self {
157		Value::Uint2(v.into())
158	}
159
160	pub fn uint4(v: impl Into<u32>) -> Self {
161		Value::Uint4(v.into())
162	}
163
164	pub fn uint8(v: impl Into<u64>) -> Self {
165		Value::Uint8(v.into())
166	}
167
168	pub fn uint16(v: impl Into<u128>) -> Self {
169		Value::Uint16(v.into())
170	}
171
172	pub fn date(v: impl Into<Date>) -> Self {
173		Value::Date(v.into())
174	}
175
176	pub fn datetime(v: impl Into<DateTime>) -> Self {
177		Value::DateTime(v.into())
178	}
179
180	pub fn time(v: impl Into<Time>) -> Self {
181		Value::Time(v.into())
182	}
183
184	pub fn duration(v: impl Into<Duration>) -> Self {
185		Value::Duration(v.into())
186	}
187
188	pub fn row_number(v: impl Into<RowNumber>) -> Self {
189		Value::RowNumber(v.into())
190	}
191
192	pub fn identity_id(v: impl Into<IdentityId>) -> Self {
193		Value::IdentityId(v.into())
194	}
195
196	pub fn uuid4(v: impl Into<Uuid4>) -> Self {
197		Value::Uuid4(v.into())
198	}
199
200	pub fn uuid7(v: impl Into<Uuid7>) -> Self {
201		Value::Uuid7(v.into())
202	}
203
204	pub fn blob(v: impl Into<Blob>) -> Self {
205		Value::Blob(v.into())
206	}
207
208	pub fn any(v: impl Into<Value>) -> Self {
209		Value::Any(Box::new(v.into()))
210	}
211}
212
213impl PartialOrd for Value {
214	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
215		match (self, other) {
216			(Value::Boolean(l), Value::Boolean(r)) => l.partial_cmp(r),
217			(Value::Float4(l), Value::Float4(r)) => l.partial_cmp(r),
218			(Value::Float8(l), Value::Float8(r)) => l.partial_cmp(r),
219			(Value::Int1(l), Value::Int1(r)) => l.partial_cmp(r),
220			(Value::Int2(l), Value::Int2(r)) => l.partial_cmp(r),
221			(Value::Int4(l), Value::Int4(r)) => l.partial_cmp(r),
222			(Value::Int8(l), Value::Int8(r)) => l.partial_cmp(r),
223			(Value::Int16(l), Value::Int16(r)) => l.partial_cmp(r),
224			(Value::Utf8(l), Value::Utf8(r)) => l.partial_cmp(r),
225			(Value::Uint1(l), Value::Uint1(r)) => l.partial_cmp(r),
226			(Value::Uint2(l), Value::Uint2(r)) => l.partial_cmp(r),
227			(Value::Uint4(l), Value::Uint4(r)) => l.partial_cmp(r),
228			(Value::Uint8(l), Value::Uint8(r)) => l.partial_cmp(r),
229			(Value::Uint16(l), Value::Uint16(r)) => l.partial_cmp(r),
230			(Value::Date(l), Value::Date(r)) => l.partial_cmp(r),
231			(Value::DateTime(l), Value::DateTime(r)) => l.partial_cmp(r),
232			(Value::Time(l), Value::Time(r)) => l.partial_cmp(r),
233			(Value::Duration(l), Value::Duration(r)) => l.partial_cmp(r),
234			(Value::RowNumber(l), Value::RowNumber(r)) => l.partial_cmp(r),
235			(Value::IdentityId(l), Value::IdentityId(r)) => l.partial_cmp(r),
236			(Value::Uuid4(l), Value::Uuid4(r)) => l.partial_cmp(r),
237			(Value::Uuid7(l), Value::Uuid7(r)) => l.partial_cmp(r),
238			(Value::Blob(l), Value::Blob(r)) => l.partial_cmp(r),
239			(Value::Int(l), Value::Int(r)) => l.partial_cmp(r),
240			(Value::Uint(l), Value::Uint(r)) => l.partial_cmp(r),
241			(Value::Decimal(l), Value::Decimal(r)) => l.partial_cmp(r),
242			(Value::Any(_), Value::Any(_)) => None, // Any values are not comparable
243			(Value::Undefined, Value::Undefined) => Some(Ordering::Equal),
244			// Undefined sorts after all other values (similar to NULL in SQL)
245			(Value::Undefined, _) => Some(Ordering::Greater),
246			(_, Value::Undefined) => Some(Ordering::Less),
247			(left, right) => {
248				unimplemented!("partial cmp {left:?} {right:?}")
249			}
250		}
251	}
252}
253
254impl Ord for Value {
255	fn cmp(&self, other: &Self) -> Ordering {
256		match (self, other) {
257			(Value::Boolean(l), Value::Boolean(r)) => l.cmp(r),
258			(Value::Float4(l), Value::Float4(r)) => l.cmp(r),
259			(Value::Float8(l), Value::Float8(r)) => l.cmp(r),
260			(Value::Int1(l), Value::Int1(r)) => l.cmp(r),
261			(Value::Int2(l), Value::Int2(r)) => l.cmp(r),
262			(Value::Int4(l), Value::Int4(r)) => l.cmp(r),
263			(Value::Int8(l), Value::Int8(r)) => l.cmp(r),
264			(Value::Int16(l), Value::Int16(r)) => l.cmp(r),
265			(Value::Utf8(l), Value::Utf8(r)) => l.cmp(r),
266			(Value::Uint1(l), Value::Uint1(r)) => l.cmp(r),
267			(Value::Uint2(l), Value::Uint2(r)) => l.cmp(r),
268			(Value::Uint4(l), Value::Uint4(r)) => l.cmp(r),
269			(Value::Uint8(l), Value::Uint8(r)) => l.cmp(r),
270			(Value::Uint16(l), Value::Uint16(r)) => l.cmp(r),
271			(Value::Date(l), Value::Date(r)) => l.cmp(r),
272			(Value::DateTime(l), Value::DateTime(r)) => l.cmp(r),
273			(Value::Time(l), Value::Time(r)) => l.cmp(r),
274			(Value::Duration(l), Value::Duration(r)) => l.cmp(r),
275			(Value::RowNumber(l), Value::RowNumber(r)) => l.cmp(r),
276			(Value::IdentityId(l), Value::IdentityId(r)) => l.cmp(r),
277			(Value::Uuid4(l), Value::Uuid4(r)) => l.cmp(r),
278			(Value::Uuid7(l), Value::Uuid7(r)) => l.cmp(r),
279			(Value::Blob(l), Value::Blob(r)) => l.cmp(r),
280			(Value::Int(l), Value::Int(r)) => l.cmp(r),
281			(Value::Uint(l), Value::Uint(r)) => l.cmp(r),
282			(Value::Decimal(l), Value::Decimal(r)) => l.cmp(r),
283			(Value::Any(_), Value::Any(_)) => unreachable!("Any values are not orderable"),
284			_ => unimplemented!(),
285		}
286	}
287}
288
289impl Display for Value {
290	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
291		match self {
292			Value::Boolean(true) => f.write_str("true"),
293			Value::Boolean(false) => f.write_str("false"),
294			Value::Float4(value) => Display::fmt(value, f),
295			Value::Float8(value) => Display::fmt(value, f),
296			Value::Int1(value) => Display::fmt(value, f),
297			Value::Int2(value) => Display::fmt(value, f),
298			Value::Int4(value) => Display::fmt(value, f),
299			Value::Int8(value) => Display::fmt(value, f),
300			Value::Int16(value) => Display::fmt(value, f),
301			Value::Utf8(value) => Display::fmt(value, f),
302			Value::Uint1(value) => Display::fmt(value, f),
303			Value::Uint2(value) => Display::fmt(value, f),
304			Value::Uint4(value) => Display::fmt(value, f),
305			Value::Uint8(value) => Display::fmt(value, f),
306			Value::Uint16(value) => Display::fmt(value, f),
307			Value::Date(value) => Display::fmt(value, f),
308			Value::DateTime(value) => Display::fmt(value, f),
309			Value::Time(value) => Display::fmt(value, f),
310			Value::Duration(value) => Display::fmt(value, f),
311			Value::RowNumber(value) => Display::fmt(value, f),
312			Value::IdentityId(value) => Display::fmt(value, f),
313			Value::Uuid4(value) => Display::fmt(value, f),
314			Value::Uuid7(value) => Display::fmt(value, f),
315			Value::Blob(value) => Display::fmt(value, f),
316			Value::Int(value) => Display::fmt(value, f),
317			Value::Uint(value) => Display::fmt(value, f),
318			Value::Decimal(value) => Display::fmt(value, f),
319			Value::Any(value) => Display::fmt(value, f),
320			Value::Undefined => f.write_str("undefined"),
321		}
322	}
323}
324
325impl Value {
326	pub fn get_type(&self) -> Type {
327		match self {
328			Value::Undefined => Type::Undefined,
329			Value::Boolean(_) => Type::Boolean,
330			Value::Float4(_) => Type::Float4,
331			Value::Float8(_) => Type::Float8,
332			Value::Int1(_) => Type::Int1,
333			Value::Int2(_) => Type::Int2,
334			Value::Int4(_) => Type::Int4,
335			Value::Int8(_) => Type::Int8,
336			Value::Int16(_) => Type::Int16,
337			Value::Utf8(_) => Type::Utf8,
338			Value::Uint1(_) => Type::Uint1,
339			Value::Uint2(_) => Type::Uint2,
340			Value::Uint4(_) => Type::Uint4,
341			Value::Uint8(_) => Type::Uint8,
342			Value::Uint16(_) => Type::Uint16,
343			Value::Date(_) => Type::Date,
344			Value::DateTime(_) => Type::DateTime,
345			Value::Time(_) => Type::Time,
346			Value::Duration(_) => Type::Duration,
347			Value::RowNumber(_) => Type::RowNumber,
348			Value::IdentityId(_) => Type::IdentityId,
349			Value::Uuid4(_) => Type::Uuid4,
350			Value::Uuid7(_) => Type::Uuid7,
351			Value::Blob(_) => Type::Blob,
352			Value::Int(_) => Type::Int,
353			Value::Uint(_) => Type::Uint,
354			Value::Decimal(_) => Type::Decimal,
355			Value::Any(_) => Type::Any,
356		}
357	}
358}