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