pub enum Value {
Null,
Bit(bool),
Number(f64),
Text(String),
Link(String),
}Expand description
Value types supported by BEAM and Gun.js.
These are the five valid leaf types in the distributed graph. Each variant
maps to a JSON type, with the exception of Value::Link which represents
a Gun.js soul relation ({"#": "node_id"}).
§Wire Format
| Variant | JSON representation | Example |
|---|---|---|
Null | null | Value::Null |
Bit | true / false | Value::Bit(true) |
Number | number | Value::Number(42.0) |
Text | string | Value::Text("hello") |
Link | {"#": "soul"} | Value::Link("node/abc") |
§NaN / Infinity
Value::Number(f64) can technically hold NaN or Infinity, but these are
not valid in the Gun.js wire format. When converting to JSON, NaN/Infinity
will produce null (serde_json behavior). Callers should validate before
constructing Value::Number from untrusted input.
Variants§
Null
Absence of a value (not the same as “deleted”).
Bit(bool)
Boolean flag.
Number(f64)
Floating-point number. Should be finite (not NaN/Infinity).
Text(String)
Unicode text string.
Link(String)
A soul relation — a reference to another node by ID.
Implementations§
Source§impl Value
impl Value
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns the approximate byte size of the value’s payload.
For Value::Text, this is the string’s byte length. For all other
variants, it is the size_of_val of the enum discriminant + data.
This is used for memory budgeting.
Sourcepub fn to_string(&self) -> String
pub fn to_string(&self) -> String
Serializes the value to its Gun.js wire-format string.
Not a [Display] implementation — this produces the exact string
representation used on the wire:
Null→"null"Bit(true)→"true"Bit(false)→"false"Number(42.0)→"42"Text("hello")→"hello"Link("node/abc")→"node/abc"(the soul string, not JSON)
For JSON serialization with proper typing, use
serde_json::to_string(&value) instead.
Sourcepub fn is_null(&self) -> bool
pub fn is_null(&self) -> bool
Returns true if this value is Value::Null.
Sourcepub fn is_bit(&self) -> bool
pub fn is_bit(&self) -> bool
Returns true if this value is Value::Bit.
Sourcepub fn is_number(&self) -> bool
pub fn is_number(&self) -> bool
Returns true if this value is Value::Number.
Sourcepub fn is_text(&self) -> bool
pub fn is_text(&self) -> bool
Returns true if this value is Value::Text.
Sourcepub fn is_link(&self) -> bool
pub fn is_link(&self) -> bool
Returns true if this value is Value::Link.
Sourcepub fn as_bit(&self) -> Option<bool>
pub fn as_bit(&self) -> Option<bool>
Returns Some(bool) if this value is Value::Bit, else None.
Sourcepub fn as_number(&self) -> Option<f64>
pub fn as_number(&self) -> Option<f64>
Returns Some(f64) if this value is Value::Number, else None.
Sourcepub fn as_text(&self) -> Option<&str>
pub fn as_text(&self) -> Option<&str>
Returns Some(&str) if this value is Value::Text, else None.
Sourcepub fn as_link(&self) -> Option<&str>
pub fn as_link(&self) -> Option<&str>
Returns Some(&str) if this value is Value::Link, else None.
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>,
Source§impl From<Value> for Value
Converts a BEAM Value into a serde_json::Value.
impl From<Value> for Value
Converts a BEAM Value into a serde_json::Value.
This is the inverse of [TryFrom<SerdeJsonValue> for Value]. The
Value::Link variant serializes to a Gun.js soul relation object
({"#": "soul"}) so it round-trips correctly through JSON.
Source§fn from(v: Value) -> SerdeJsonValue
fn from(v: Value) -> SerdeJsonValue
impl StructuralPartialEq for Value
Source§impl TryFrom<Value> for Value
Converts a serde_json::Value into a BEAM Value.
impl TryFrom<Value> for Value
Converts a serde_json::Value into a BEAM Value.
This conversion validates that the JSON value is one of the five
supported types. Objects are accepted only if they are a Gun.js
soul relation ({"#": "soul"}); all other objects are rejected.
§Errors
Returns &'static str if the JSON value cannot be represented as a
BEAM Value:
- Arrays →
"cannot convert array into Value" - Non-soul objects →
"cannot convert json object into Value" - Numbers not convertible to f64 →
"not convertible to f64"
§Security Note
For production use, consider wrapping this in a custom error type rather
than &'static str. The string error type limits programmatic error
handling. This is a known limitation to address in a future API revision.