pub enum Value {
Null,
Bool(bool),
Int(i64),
Float(f64),
String(String),
List(Vec<Value>),
Map(ValueMap),
Vertex(VertexId),
Edge(EdgeId),
Point(Point),
Polygon(Polygon),
}Expand description
A dynamic value type for graph properties and traversal results.
Value is Interstellar’s universal data type, capable of representing
any property value or traversal result. It’s similar to JSON but extended
with graph-specific types for vertex and edge references.
§Variants
| Variant | Rust Type | Description |
|---|---|---|
Null | - | Absence of a value |
Bool | bool | Boolean true/false |
Int | i64 | 64-bit signed integer |
Float | f64 | 64-bit floating point |
String | String | UTF-8 text |
List | Vec<Value> | Ordered collection |
Map | HashMap<String, Value> | Key-value pairs |
Vertex | VertexId | Reference to a vertex |
Edge | EdgeId | Reference to an edge |
§Type Conversions
Value implements From for many common types:
use interstellar::prelude::*;
// Primitives
let _: Value = true.into();
let _: Value = 42i64.into();
let _: Value = 3.14f64.into();
let _: Value = "hello".into();
// Graph elements
let _: Value = VertexId(1).into();
let _: Value = EdgeId(2).into();§Type Checking and Extraction
Use the as_* methods to safely extract typed values:
use interstellar::prelude::*;
let val: Value = 42i64.into();
// Type-safe extraction
if let Some(n) = val.as_i64() {
println!("Got integer: {}", n);
}
// Type checking
assert!(!val.is_null());
assert!(!val.is_vertex());§Hashing
Value implements Hash, allowing it to be used in hash-based
collections. Map values are hashed in sorted key order to ensure
consistent hashing regardless of insertion order.
use interstellar::prelude::*;
use std::collections::HashSet;
let mut seen: HashSet<Value> = HashSet::new();
seen.insert(42i64.into());
seen.insert("hello".into());Variants§
Null
The null/absent value.
Bool(bool)
A boolean value.
Int(i64)
A 64-bit signed integer.
Float(f64)
A 64-bit floating-point number.
String(String)
A UTF-8 string.
List(Vec<Value>)
An ordered list of values.
Map(ValueMap)
A map of string keys to values. Iteration order is the insertion order.
Vertex(VertexId)
A vertex reference (for traversal).
Edge(EdgeId)
An edge reference (for traversal).
Point(Point)
A geospatial point (WGS84 longitude/latitude).
Polygon(Polygon)
A geospatial polygon (WGS84, simple closed ring, no holes).
Implementations§
Source§impl Value
impl Value
Sourcepub fn serialize(&self, buf: &mut Vec<u8>)
pub fn serialize(&self, buf: &mut Vec<u8>)
Serialize this value to a compact binary format.
The binary format uses a type tag byte followed by the value data. This format is suitable for persistence and network transmission.
§Format
| Tag | Type | Data |
|---|---|---|
| 0x00 | Null | (none) |
| 0x01 | Bool(false) | (none) |
| 0x02 | Bool(true) | (none) |
| 0x03 | Int | 8 bytes (little-endian i64) |
| 0x04 | Float | 8 bytes (little-endian f64) |
| 0x05 | String | 4-byte length + UTF-8 bytes |
| 0x06 | List | 4-byte count + serialized items |
| 0x07 | Map | 4-byte count + (key, value) pairs |
| 0x08 | Vertex | 8 bytes (little-endian u64) |
| 0x09 | Edge | 8 bytes (little-endian u64) |
§Example
use interstellar::prelude::*;
let value = Value::Int(42);
let mut buf = Vec::new();
value.serialize(&mut buf);
// Deserialize it back
let mut pos = 0;
let parsed = Value::deserialize(&buf, &mut pos).unwrap();
assert_eq!(parsed, value);Sourcepub fn deserialize(buf: &[u8], pos: &mut usize) -> Option<Value>
pub fn deserialize(buf: &[u8], pos: &mut usize) -> Option<Value>
Deserialize a value from a binary buffer.
Reads a value starting at position pos in the buffer, advancing
pos past the consumed bytes. Returns None if the buffer is
malformed or truncated.
§Arguments
buf- The byte buffer to read frompos- Mutable position indicator, updated to point past the read value
§Returns
Some(Value) if deserialization succeeds, None if the data is invalid.
§Example
use interstellar::prelude::*;
// Serialize a value
let original = Value::String("hello".to_string());
let mut buf = Vec::new();
original.serialize(&mut buf);
// Deserialize it
let mut pos = 0;
let parsed = Value::deserialize(&buf, &mut pos).unwrap();
assert_eq!(parsed, original);
assert_eq!(pos, buf.len()); // Position advanced to endSourcepub fn to_comparable(&self) -> ComparableValue
pub fn to_comparable(&self) -> ComparableValue
Convert this value to a comparable version with total ordering.
Returns a ComparableValue that mirrors this value but implements
Ord, enabling sorting and use in ordered collections.
§Example
use interstellar::prelude::*;
let values = vec![
Value::Int(3),
Value::Int(1),
Value::Int(2),
];
let mut comparable: Vec<_> = values.iter()
.map(Value::to_comparable)
.collect();
comparable.sort();
// Now sorted: [Int(1), Int(2), Int(3)]Sourcepub fn as_bool(&self) -> Option<bool>
pub fn as_bool(&self) -> Option<bool>
Extract the value as a boolean, if it is one.
Returns Some(bool) if this is a Bool variant, None otherwise.
§Example
use interstellar::prelude::*;
assert_eq!(Value::Bool(true).as_bool(), Some(true));
assert_eq!(Value::Int(1).as_bool(), None);Sourcepub fn as_i64(&self) -> Option<i64>
pub fn as_i64(&self) -> Option<i64>
Extract the value as an i64 integer, if it is one.
Returns Some(i64) if this is an Int variant, None otherwise.
§Example
use interstellar::prelude::*;
assert_eq!(Value::Int(42).as_i64(), Some(42));
assert_eq!(Value::Float(42.0).as_i64(), None); // Type mismatchSourcepub fn as_f64(&self) -> Option<f64>
pub fn as_f64(&self) -> Option<f64>
Extract the value as an f64 float, if it is one.
Returns Some(f64) if this is a Float variant, None otherwise.
§Example
use interstellar::prelude::*;
assert_eq!(Value::Float(3.14).as_f64(), Some(3.14));
assert_eq!(Value::Int(3).as_f64(), None); // Type mismatchSourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
Extract the value as a string slice, if it is one.
Returns Some(&str) if this is a String variant, None otherwise.
§Example
use interstellar::prelude::*;
let val = Value::String("hello".to_string());
assert_eq!(val.as_str(), Some("hello"));
assert_eq!(Value::Int(42).as_str(), None);Sourcepub fn as_list(&self) -> Option<&Vec<Value>>
pub fn as_list(&self) -> Option<&Vec<Value>>
Extract the value as a list reference, if it is one.
Returns Some(&Vec<Value>) if this is a List variant, None otherwise.
§Example
use interstellar::prelude::*;
let val = Value::List(vec![Value::Int(1), Value::Int(2)]);
if let Some(items) = val.as_list() {
assert_eq!(items.len(), 2);
}Sourcepub fn as_map(&self) -> Option<&ValueMap>
pub fn as_map(&self) -> Option<&ValueMap>
Extract the value as a map reference, if it is one.
Returns Some(&ValueMap) if this is a Map variant,
None otherwise. The returned map preserves insertion order.
§Example
use interstellar::prelude::*;
use interstellar::value::ValueMap;
let mut map = ValueMap::new();
map.insert("name".to_string(), Value::String("Alice".to_string()));
let val = Value::Map(map);
if let Some(m) = val.as_map() {
assert!(m.contains_key("name"));
}Sourcepub fn is_null(&self) -> bool
pub fn is_null(&self) -> bool
Check if this value is null.
Returns true if this is the Null variant, false otherwise.
§Example
use interstellar::prelude::*;
assert!(Value::Null.is_null());
assert!(!Value::Int(0).is_null());
assert!(!Value::String("".to_string()).is_null());Sourcepub fn as_vertex_id(&self) -> Option<VertexId>
pub fn as_vertex_id(&self) -> Option<VertexId>
Extract the value as a vertex ID, if it is one.
Returns Some(VertexId) if this is a Vertex variant, None otherwise.
§Example
use interstellar::prelude::*;
let val = Value::Vertex(VertexId(42));
assert_eq!(val.as_vertex_id(), Some(VertexId(42)));
assert_eq!(Value::Edge(EdgeId(42)).as_vertex_id(), None);Sourcepub fn as_edge_id(&self) -> Option<EdgeId>
pub fn as_edge_id(&self) -> Option<EdgeId>
Extract the value as an edge ID, if it is one.
Returns Some(EdgeId) if this is an Edge variant, None otherwise.
§Example
use interstellar::prelude::*;
let val = Value::Edge(EdgeId(99));
assert_eq!(val.as_edge_id(), Some(EdgeId(99)));
assert_eq!(Value::Vertex(VertexId(99)).as_edge_id(), None);Sourcepub fn is_vertex(&self) -> bool
pub fn is_vertex(&self) -> bool
Check if this value is a vertex reference.
Returns true if this is a Vertex variant, false otherwise.
§Example
use interstellar::prelude::*;
assert!(Value::Vertex(VertexId(1)).is_vertex());
assert!(!Value::Edge(EdgeId(1)).is_vertex());
assert!(!Value::Int(1).is_vertex());Sourcepub fn is_edge(&self) -> bool
pub fn is_edge(&self) -> bool
Check if this value is an edge reference.
Returns true if this is an Edge variant, false otherwise.
§Example
use interstellar::prelude::*;
assert!(Value::Edge(EdgeId(1)).is_edge());
assert!(!Value::Vertex(VertexId(1)).is_edge());
assert!(!Value::Int(1).is_edge());Sourcepub fn as_polygon(&self) -> Option<&Polygon>
pub fn as_polygon(&self) -> Option<&Polygon>
Extract the value as a geospatial polygon reference, if it is one.
Sourcepub fn is_polygon(&self) -> bool
pub fn is_polygon(&self) -> bool
Check if this value is a geospatial polygon.
Sourcepub fn discriminant(&self) -> u8
pub fn discriminant(&self) -> u8
Returns the type discriminant (tag) for this value.
The discriminant is the type tag byte used in the binary serialization format. This is useful for storage backends that need to know the value type without fully deserializing it.
§Discriminant Values
| Discriminant | Type |
|---|---|
| 0x00 | Null |
| 0x01 | Bool(false) |
| 0x02 | Bool(true) |
| 0x03 | Int |
| 0x04 | Float |
| 0x05 | String |
| 0x06 | List |
| 0x07 | Map |
| 0x08 | Vertex |
| 0x09 | Edge |
§Example
use interstellar::prelude::*;
assert_eq!(Value::Null.discriminant(), 0x00);
assert_eq!(Value::Bool(false).discriminant(), 0x01);
assert_eq!(Value::Bool(true).discriminant(), 0x02);
assert_eq!(Value::Int(42).discriminant(), 0x03);
assert_eq!(Value::Float(3.14).discriminant(), 0x04);
assert_eq!(Value::String("hello".to_string()).discriminant(), 0x05);
assert_eq!(Value::List(vec![]).discriminant(), 0x06);Trait Implementations§
Source§impl<'de> Deserialize<'de> for Value
impl<'de> Deserialize<'de> for Value
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for Value
impl StructuralPartialEq for Value
Auto Trait Implementations§
impl Freeze for Value
impl RefUnwindSafe for Value
impl Send for Value
impl Sync for Value
impl Unpin for Value
impl UnsafeUnpin for Value
impl UnwindSafe for Value
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.