pub struct Tree { /* private fields */ }
Implementations§
Source§impl Tree
impl Tree
Sourcepub fn get(&self, key: &str) -> Result<Tree, NanoDBError>
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 withkey
.Err(NanoDBError::NotAnObject)
- If the inner value of the tree is not an object.Err(NanoDBError::KeyNotFound(key))
- Ifkey
does not exist in the JSON object.
Sourcepub fn at(&self, index: usize) -> Result<Tree, NanoDBError>
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 atindex
.Err(NanoDBError::NotAnArray)
- If the inner value of the tree is not an array.Err(NanoDBError::IndexOutOfBounds(index))
- Ifindex
is out of bounds of the array.
Sourcepub fn inner(&self) -> Value
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.
Sourcepub fn path_string(&self) -> String
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.
Sourcepub fn into<T: for<'de> Deserialize<'de>>(self) -> Result<T, NanoDBError>
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 theDeserialize
trait.
§Returns
Ok(T)
- The JSON value converted into the specified type.Err(NanoDBError)
- If there was an error during the conversion.
Sourcepub fn tree_type(&self) -> TreeType
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 ofNull
,Bool
,Number
,String
,Array
, orObject
.
Sourcepub fn insert<T: Serialize>(
&mut self,
key: &str,
value: T,
) -> Result<Tree, NanoDBError>
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 theSerialize
trait.
§Returns
Ok(Tree)
- The Tree instance itself after the insertion. This allows for method chaining.Err(NanoDBError::SerializationError)
- If there was an error serializingvalue
.
Sourcepub fn remove_at(&mut self, index: usize) -> Result<Tree, NanoDBError>
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 theTree
instance after the removal.Err(NanoDBError)
- If theinner
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.
Sourcepub fn merge_from(&mut self, other: Tree) -> Result<&mut Self, NanoDBError>
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.
Sourcepub fn push<T: Serialize>(&mut self, value: T) -> Result<Tree, NanoDBError>
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.
Sourcepub fn for_each<F>(&mut self, f: F) -> Result<Tree, NanoDBError>
pub fn for_each<F>(&mut self, f: F) -> Result<Tree, NanoDBError>
Applies a function to each element of the inner array of the tree.
§Arguments
f
- A mutable function that takes a mutable reference to aserde_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.