graph_api_lib/
value.rs

1use std::ops::Range;
2use uuid::Uuid;
3
4/// A polymorphic value type for graph element properties.
5///
6/// This enum represents the various types of values that can be stored in graph
7/// vertices and edges. It supports numeric types, boolean values, UUIDs, and strings.
8///
9/// The lifetime parameter allows the enum to store borrowed string data.
10#[derive(Clone)]
11pub enum Value<'a> {
12    /// An unsigned size value
13    USize(usize),
14    /// A 128-bit unsigned integer
15    U128(u128),
16    /// A 64-bit unsigned integer
17    U64(u64),
18    /// A 32-bit unsigned integer
19    U32(u32),
20    /// A 16-bit unsigned integer
21    U16(u16),
22    /// An 8-bit unsigned integer
23    U8(u8),
24    /// A 128-bit signed integer
25    I128(i128),
26    /// A 64-bit signed integer
27    I64(i64),
28    /// A 32-bit signed integer
29    I32(i32),
30    /// A 16-bit signed integer
31    I16(i16),
32    /// An 8-bit signed integer
33    I8(i8),
34    /// A 64-bit floating point number
35    F64(f64),
36    /// A 32-bit floating point number
37    F32(f32),
38    /// A boolean value
39    Bool(bool),
40    /// A UUID
41    Uuid(Uuid),
42    /// A string slice
43    Str(&'a str),
44}
45
46/// A range of Values for range-based queries and indexes.
47///
48/// This struct wraps a standard Rust Range of Value types, allowing for
49/// range-based lookups and queries in indexed graphs.
50///
51/// The lifetime parameter corresponds to the lifetime of any string data in the range.
52#[derive(Clone)]
53pub struct ValueRange<'a>(pub(crate) Range<Value<'a>>);
54
55macro_rules! value_coercion {
56    ($ty: ty,  $ident: ident) => {
57        impl<'a> From<&'a $ty> for Value<'a> {
58            fn from(value: &'a $ty) -> Self {
59                Self::$ident(*value)
60            }
61        }
62        impl<'a> From<&'a mut $ty> for Value<'a> {
63            fn from(value: &'a mut $ty) -> Self {
64                Self::$ident(*value)
65            }
66        }
67        impl<'a> From<$ty> for Value<'a> {
68            fn from(value: $ty) -> Self {
69                Self::$ident(value)
70            }
71        }
72    };
73}
74value_coercion!(&'a String, Str);
75value_coercion!(&'a str, Str);
76value_coercion!(u128, U128);
77value_coercion!(u64, U64);
78value_coercion!(u32, U32);
79value_coercion!(u16, U16);
80value_coercion!(u8, U8);
81value_coercion!(i128, I128);
82value_coercion!(i64, I64);
83value_coercion!(i32, I32);
84value_coercion!(i16, I16);
85value_coercion!(i8, I8);
86value_coercion!(f64, F64);
87value_coercion!(f32, F32);
88value_coercion!(bool, Bool);
89value_coercion!(Uuid, Uuid);
90
91macro_rules! value_or_range_coercion {
92    ($ty: ty) => {
93        impl<'a> From<Range<$ty>> for ValueRange<'a> {
94            fn from(range: Range<$ty>) -> Self {
95                ValueRange(range.start.into()..range.end.into())
96            }
97        }
98    };
99}
100
101value_or_range_coercion!(&'a str);
102value_or_range_coercion!(u128);
103value_or_range_coercion!(u64);
104value_or_range_coercion!(u32);
105value_or_range_coercion!(u16);
106value_or_range_coercion!(u8);
107value_or_range_coercion!(i128);
108value_or_range_coercion!(i64);
109value_or_range_coercion!(i32);
110value_or_range_coercion!(i16);
111value_or_range_coercion!(i8);
112value_or_range_coercion!(f64);
113value_or_range_coercion!(f32);
114value_or_range_coercion!(bool);
115value_or_range_coercion!(Uuid);
116value_or_range_coercion!(&'a String);