Value

Enum Value 

Source
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

VariantProduced bySerializes as
Mapstring-keyed bracketsk[sub]=v
List[] appendsk[]=v
SparseListexplicit [n] numeric indicesk[n]=v
Stringplain value (k=v)k=v
Emptykey 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>

Source

pub fn into_owned(self) -> Value<'static>

Deep-clone this value into a Value<'static>, converting any Cow::Borrowed strings to owned Strings.

Source

pub fn new_map() -> Self

Builds a querystrong::Value::Map

Source

pub fn new_list() -> Self

Builds a dense querystrong::Value::List (for [] appends)

Source

pub fn new_sparse_list() -> Self

Builds a querystrong::Value::SparseList (for [n] indexed access)

Source

pub fn is_map(&self) -> bool

Returns true if this value is a Map.

Source

pub fn is_string(&self) -> bool

Returns true if this value is a String.

Source

pub fn is_list(&self) -> bool

Returns true for both dense List and SparseList variants.

Source

pub fn is_dense_list(&self) -> bool

Returns true only for the dense List variant (built from [] appends).

Source

pub fn is_sparse_list(&self) -> bool

Returns true only for the SparseList variant (built from [n] indices).

Source

pub fn as_slice(&self) -> Option<&[Self]>

Returns a slice only for the dense List variant; returns None for SparseList.

Source

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.

Source

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.

Source

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.

Source

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

Source

pub fn len(&self) -> usize

For List, returns the number of elements. For SparseList, returns the number of populated entries (not last_index + 1).

Source

pub fn append<'b: 'a, K, V>(&mut self, key: K, value: V) -> Result<'a, ()>
where K: TryInto<IndexPath<'b>>, V: TryInto<Value<'b>>, K::Error: Into<Error<'a>>, V::Error: Into<Error<'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).

Source

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.

Source

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).

Source

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).

Source

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).

Source

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).

Source

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 a SparseList with the slot removed, then potentially promoted to SparseList — so taking the last element stays dense, and taking a middle element leaves a SparseList (symmetric with how insert promotes ListSparseList when a gap is created).
  • SparseList: the entry is removed, then potentially promoted to a dense List if the remaining content is contiguous.

Trait Implementations§

Source§

impl<'a> Clone for Value<'a>

Source§

fn clone(&self) -> Value<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Value<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for Value<'a>

Source§

fn default() -> Value<'a>

Returns the “default value” for a type. Read more
Source§

impl<'a> From<&'a String> for Value<'a>

Source§

fn from(s: &'a String) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a str> for Value<'a>

Source§

fn from(s: &'a str) -> Self

Converts to this type from the input type.
Source§

impl From<()> for Value<'static>

Source§

fn from(_: ()) -> Self

Converts to this type from the input type.
Source§

impl<K, V> From<(K, V)> for Value<'static>
where K: TryInto<IndexPath<'static>>, K::Error: Into<Error<'static>>, V: Into<Value<'static>>,

Source§

fn from(v: (K, V)) -> Self

Converts to this type from the input type.
Source§

impl<'a, V> From<Option<V>> for Value<'a>
where V: Into<Value<'a>>,

Source§

fn from(o: Option<V>) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Value<'static>

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl<'a, I: Into<Value<'a>>> From<Vec<I>> for Value<'a>

Source§

fn from(v: Vec<I>) -> Self

Converts to this type from the input type.
Source§

impl<'a, Key: TryInto<IndexPath<'a>>> Index<Key> for Value<'a>

Source§

type Output = Value<'a>

The returned type after indexing.
Source§

fn index(&self, key: Key) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a: 'b, 'b> IntoIterator for &'a Value<'b>

Source§

type Item = (Option<IndexPath<'b>>, Option<String>)

The type of the elements being iterated over.
Source§

type IntoIter = Box<dyn Iterator<Item = <&'a Value<'b> as IntoIterator>::Item> + 'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq<&str> for Value<'_>

Source§

fn eq(&self, other: &&str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<()> for Value<'_>

Source§

fn eq(&self, _: &()) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<String> for Value<'_>

Source§

fn eq(&self, other: &String) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<str> for Value<'_>

Source§

fn eq(&self, other: &str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> PartialEq for Value<'a>

Source§

fn eq(&self, other: &Value<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Eq for Value<'a>

Source§

impl<'a> StructuralPartialEq for Value<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Value<'a>

§

impl<'a> RefUnwindSafe for Value<'a>

§

impl<'a> Send for Value<'a>

§

impl<'a> Sync for Value<'a>

§

impl<'a> Unpin for Value<'a>

§

impl<'a> UnwindSafe for Value<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.