pub enum Value<'a> {
Map(BTreeMap<Cow<'a, str>, Value<'a>>),
List(Vec<Value<'a>>),
SparseList(BTreeMap<usize, Value<'a>>),
String(Cow<'a, str>),
Empty,
}Expand description
A node in the parsed query-string value tree.
The lifetime 'a reflects zero-copy borrowing: string data that needs no
percent-decoding or +-as-space substitution is stored as a Cow::Borrowed
pointing into the original input. Call Value::into_owned to obtain a
'static value.
§Variant summary
| Variant | Produced by | Serializes as |
|---|---|---|
Map | string-keyed brackets | k[sub]=v |
List | [] appends | k[]=v |
SparseList | explicit [n] numeric indices | k[n]=v |
String | plain value (k=v) | k=v |
Empty | key with no value (k) | k |
Variants§
Map(BTreeMap<Cow<'a, str>, Value<'a>>)
A string-keyed map, produced by bracket-notation keys (a[b]=v).
List(Vec<Value<'a>>)
A dense, contiguous list produced by empty-bracket appends (a[]=v).
Serializes with [] notation.
SparseList(BTreeMap<usize, Value<'a>>)
A sparse list produced by explicit numeric indices (a[5]=v).
Backed by a BTreeMap<usize, Value> so a large index like a[999999]=v
allocates exactly one map slot rather than 1 000 000 Vec elements.
Absent slots within 0..=max_key are treated as Value::Empty;
slots beyond max_key return None from Value::get.
Serializes with [n] notation, preserving the original indices.
String(Cow<'a, str>)
A string value, possibly borrowed from the input when no decoding was needed.
Empty
The absence of a value, produced by a key with no = (e.g. bare k).
Implementations§
Source§impl<'a> Value<'a>
impl<'a> Value<'a>
Sourcepub fn into_owned(self) -> Value<'static>
pub fn into_owned(self) -> Value<'static>
Deep-clone this value into a Value<'static>, converting any
Cow::Borrowed strings to owned Strings.
Sourcepub fn new_sparse_list() -> Self
pub fn new_sparse_list() -> Self
Builds a querystrong::Value::SparseList (for [n] indexed access)
Sourcepub fn is_dense_list(&self) -> bool
pub fn is_dense_list(&self) -> bool
Returns true only for the dense List variant (built from [] appends).
Sourcepub fn is_sparse_list(&self) -> bool
pub fn is_sparse_list(&self) -> bool
Returns true only for the SparseList variant (built from [n] indices).
Sourcepub fn as_slice(&self) -> Option<&[Self]>
pub fn as_slice(&self) -> Option<&[Self]>
Returns a slice only for the dense List variant; returns None for SparseList.
Sourcepub fn as_map(&self) -> Option<&BTreeMap<Cow<'a, str>, Value<'a>>>
pub fn as_map(&self) -> Option<&BTreeMap<Cow<'a, str>, Value<'a>>>
Returns a reference to the inner map if this is a Map,
otherwise None.
Sourcepub fn as_sparse_list(&self) -> Option<&BTreeMap<usize, Value<'a>>>
pub fn as_sparse_list(&self) -> Option<&BTreeMap<usize, Value<'a>>>
Returns a reference to the inner map if this is a SparseList,
otherwise None.
Sourcepub fn as_str<'b: 'a>(&'b self) -> Option<&'a str>
pub fn as_str<'b: 'a>(&'b self) -> Option<&'a str>
Returns the inner string slice if this is a String,
otherwise None.
When the original input was not percent-encoded, the returned &str
points directly into the source &'a str without copying.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
this is a general-purpose predicate that is broader than just whether the value is an Empty. Zero-length Values of any sort will return true from is_empty
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
For List, returns the number of elements.
For SparseList, returns the number of populated entries (not last_index + 1).
Sourcepub fn append<'b: 'a, K, V>(&mut self, key: K, value: V) -> Result<'a, ()>
pub fn append<'b: 'a, K, V>(&mut self, key: K, value: V) -> Result<'a, ()>
Insert or merge value at the path described by key.
key accepts anything convertible to an IndexPath: a &str like
"a[b][c]", a bare usize, an Indexer, or a pre-built IndexPath.
value accepts anything convertible to a Value: &str, String,
Option<V>, (), Vec<V>, or a (key, value) pair.
Returns an error when the existing tree structure is incompatible with
the requested path (e.g. appending a[b]=2 when a is already a
string).
Sourcepub fn get<'b>(&self, key: impl TryInto<IndexPath<'b>>) -> Option<&Value<'a>>
pub fn get<'b>(&self, key: impl TryInto<IndexPath<'b>>) -> Option<&Value<'a>>
Traverse the value tree along key, returning the node at that path.
Returns None if any segment of the path does not match the current
value’s type or if the key is absent.
For a SparseList, indices within 0..=max_index
that have no stored value return Some(&Value::Empty); indices beyond
max_index return None.
Sourcepub fn get_str<'b>(&self, key: impl TryInto<IndexPath<'b>>) -> Option<&str>
pub fn get_str<'b>(&self, key: impl TryInto<IndexPath<'b>>) -> Option<&str>
Convenience wrapper around get that extracts a &str.
Equivalent to self.get(key).and_then(Value::as_str).
Sourcepub fn get_slice<'b>(
&self,
key: impl TryInto<IndexPath<'b>>,
) -> Option<&[Value<'a>]>
pub fn get_slice<'b>( &self, key: impl TryInto<IndexPath<'b>>, ) -> Option<&[Value<'a>]>
Convenience wrapper around get that extracts a &[Value].
Only succeeds for dense List values; returns None for
SparseList. Equivalent to
self.get(key).and_then(Value::as_slice).
Sourcepub fn get_map<'b>(
&self,
key: impl TryInto<IndexPath<'b>>,
) -> Option<&BTreeMap<Cow<'a, str>, Value<'a>>>
pub fn get_map<'b>( &self, key: impl TryInto<IndexPath<'b>>, ) -> Option<&BTreeMap<Cow<'a, str>, Value<'a>>>
Convenience wrapper around get that extracts the inner map.
Equivalent to self.get(key).and_then(Value::as_map).
Sourcepub fn get_sparse_list<'b>(
&self,
key: impl TryInto<IndexPath<'b>>,
) -> Option<&BTreeMap<usize, Value<'a>>>
pub fn get_sparse_list<'b>( &self, key: impl TryInto<IndexPath<'b>>, ) -> Option<&BTreeMap<usize, Value<'a>>>
Convenience wrapper around get that extracts the sparse-list map.
Equivalent to self.get(key).and_then(Value::as_sparse_list).
Sourcepub fn take<'b>(
&mut self,
key: impl TryInto<IndexPath<'b>>,
) -> Option<Value<'a>>
pub fn take<'b>( &mut self, key: impl TryInto<IndexPath<'b>>, ) -> Option<Value<'a>>
Remove and return the value at key, leaving Value::Empty in its place.
Returns None if the path does not exist (same conditions as get).
Behaviour by container type:
Map: the key is removed from the map after the value is taken.List: converted to aSparseListwith the slot removed, then potentially promoted to SparseList — so taking the last element stays dense, and taking a middle element leaves aSparseList(symmetric with how insert promotesList→SparseListwhen a gap is created).SparseList: the entry is removed, then potentially promoted to a denseListif the remaining content is contiguous.