Struct Tree

Source
pub struct Tree { /* private fields */ }

Implementations§

Source§

impl Tree

Source

pub fn get(&self, key: &str) -> Result<Tree, NanoDBError>

Retrieves the value associated with a given key in the inner JSON object of the tree.

§Arguments
  • key - The key to retrieve the value for.
§Returns
  • Ok(Tree) - A new Tree object that represents the value associated with key.
  • Err(NanoDBError::NotAnObject) - If the inner value of the tree is not an object.
  • Err(NanoDBError::KeyNotFound(key)) - If key does not exist in the JSON object.
Source

pub fn at(&self, index: usize) -> Result<Tree, NanoDBError>

Retrieves the value at a given index in the inner JSON array of the tree.

§Arguments
  • index - The index to retrieve the value from.
§Returns
  • Ok(Tree) - A new Tree object that represents the value at index.
  • Err(NanoDBError::NotAnArray) - If the inner value of the tree is not an array.
  • Err(NanoDBError::IndexOutOfBounds(index)) - If index is out of bounds of the array.
Source

pub fn inner(&self) -> Value

Returns a clone of the inner JSON value of the Tree instance.

§Returns
  • serde_json::Value - A clone of the inner JSON value.
Source

pub fn path_string(&self) -> String

Returns the path of the Tree instance as a dot-separated string.

§Returns
  • String - The path as a dot-separated string.
Source

pub fn into<T: for<'de> Deserialize<'de>>(self) -> Result<T, NanoDBError>

Converts the inner JSON value of the Tree instance into a specified type.

§Type Parameters
  • T - The type to convert the JSON value into. This type must implement the Deserialize trait.
§Returns
  • Ok(T) - The JSON value converted into the specified type.
  • Err(NanoDBError) - If there was an error during the conversion.
Source

pub fn tree_type(&self) -> TreeType

Returns the type of the inner value of the tree.

§Returns
  • TreeType - The type of the inner value of the tree. This can be one of Null, Bool, Number, String, Array, or Object.
Source

pub fn insert<T: Serialize>( &mut self, key: &str, value: T, ) -> Result<Tree, NanoDBError>

Inserts a key-value pair into the inner JSON object of the Tree instance.

§Arguments
  • key - The key to insert the value for.
  • value - The value to insert. This value must implement the Serialize trait.
§Returns
  • Ok(Tree) - The Tree instance itself after the insertion. This allows for method chaining.
  • Err(NanoDBError::SerializationError) - If there was an error serializing value.
Source

pub fn remove(&mut self, key: &str) -> Result<Tree, NanoDBError>

Removes a key-value pair from the inner JSON object of the Tree instance.

§Arguments
  • key - The key to remove the value for.
§Returns
  • Ok(Tree) - A clone of the Tree instance after the removal.
  • Err(NanoDBError) - If the inner JSON value is not an object or the key does not exist.
Source

pub fn remove_at(&mut self, index: usize) -> Result<Tree, NanoDBError>

Removes an element at a specific index from the array stored in the inner field of the Tree instance.

§Arguments
  • index - The index at which to remove the element.
§Returns
  • Ok(Tree) - A clone of the Tree instance after the removal.
  • Err(NanoDBError) - If the inner field is not an array or the index is out of bounds.
§Errors

This function will return an error if the inner field is not an array or the index is out of bounds.

Source

pub fn merge_from(&mut self, other: Tree) -> Result<&mut Self, NanoDBError>

Merges a Tree (other) into the JSON data of the NanoDB instance It does so by respecting the path of the other Tree instance.

§Arguments
  • tree - The Tree to merge into the JSON data.
§Returns
  • Ok(()) - If the operation was successful.
  • Err(NanoDBError::RwLockWriteError) - If there was an error acquiring the write lock.
  • Err(NanoDBError::InvalidJSONPath) - If the path does not exist in the JSON data or if a path step is not valid for the current value (e.g., using a key on an array or an index on an object).
  • Err(NanoDBError::IndexOutOfBounds) - If an index path step is out of bounds of the array.
Source

pub fn push<T: Serialize>(&mut self, value: T) -> Result<Tree, NanoDBError>

Pushes a value to the tree if it’s an array.

§Arguments
  • value - A value of type T that implements the Serialize trait. This value will be serialized to JSON and pushed to the array.
§Returns
  • Ok(Tree) - A new Tree object that represents the current state of the tree after the value has been pushed.
  • Err(NanoDBError::NotAnArray) - If the inner value of the tree is not an array.
Source

pub fn for_each<F>(&mut self, f: F) -> Result<Tree, NanoDBError>
where F: FnMut(&mut Value),

Applies a function to each element of the inner array of the tree.

§Arguments
  • f - A mutable function that takes a mutable reference to a serde_json::Value and returns ().
§Returns
  • Ok(Tree) - A new Tree object that represents the current state of the tree after the function has been applied to each element.
  • Err(NanoDBError::NotAnArray) - If the inner value of the tree is not an array.
Source

pub fn len(&self) -> Result<usize, NanoDBError>

Returns the length of the inner array of the tree.

§Returns
  • Ok(usize) - The length of the array if the inner value of the tree is an array.
  • Err(NanoDBError::NotAnArray) - If the inner value of the tree is not an array.
Source

pub fn is_empty(&self) -> bool

Checks if the inner JSON array of the Tree instance is empty.

§Returns
  • true - If the inner JSON value empty.
  • false - If the inner JSON value is not empty.

Trait Implementations§

Source§

impl Clone for Tree

Source§

fn clone(&self) -> Tree

Returns a copy 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 Tree

Source§

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

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

impl Display for Tree

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Tree

§

impl RefUnwindSafe for Tree

§

impl Send for Tree

§

impl Sync for Tree

§

impl Unpin for Tree

§

impl UnwindSafe for Tree

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.