Struct Tree

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

The struct that allows you to create sub trees in the main tree in the database Sub trees do not auto insert in the main tree of the database You can do that by doing

§Example

// Create the database instance
let mut db = Database::new("./hello.dino");
 
// Load and create the database if does not exist
db.load();

// Create a new sub Tree in the main Tree of the db
let mut data_tree = Tree::new();

// Insert the [data_tree] under the main tree
db.insert_tree("id", data_tree);

Where the key always need to be a String

Implementations§

Source§

impl Tree

Source

pub fn new() -> Tree

Create a new sub tree

Examples found in repository?
examples/database.rs (line 16)
5fn main() {
6    // Create the database instance
7    let mut db = Database::new("./hello.dino");
8
9    // Load and create the database if does not exist
10    db.load();
11
12    // Insert values in the db in the format of key, value
13    db.insert("key", "q");
14
15    // Create a new sub Tree in the main Tree of the db
16    let mut data_tree = Tree::new();
17
18    // Insert the key and value in the sub tree
19    data_tree.insert("b", "c");
20
21    // The length of items in the sub tree in the database
22    // This also shows almost all of the functions in Database are also avaliable in Tree
23    println!("The length of items in the sub tree in the database is: {}", data_tree.len());
24
25    // Insert the [data_tree] under the main tree
26    db.insert_tree("id", data_tree);
27
28    // Print the value of id
29    println!("The value of key: id is:\n{}", db.find("id").unwrap());
30
31    match db.find("not_exists") {
32        Ok(_value) => {
33            println!("This is unfortunate :(")
34        }
35
36        Err(error) => {
37            println!("Everting works! Here is the error for reference: {}", error)
38        }
39    }
40
41    // Remove a key in the database with its value
42    db.remove("id");
43
44    // Now here it wont print that it exists as it does not we removed it ^^^^^
45    if db.contains_key("id") {
46        println!("The key `id` exists!")
47    };
48
49    println!("The length of items in the database is: {}", db.len());
50
51    // Insert a number in the database
52    db.insert_number("test", 1);
53
54    println!("{}", db.find("test").unwrap().to_number() + 1); // This will print 2
55}
Source

pub fn from(value: &str) -> Tree

Create a new Tree from String value

Source

pub fn insert(&mut self, key: &str, value: &str)

Insert data with String value type in the sub tree

Examples found in repository?
examples/database.rs (line 19)
5fn main() {
6    // Create the database instance
7    let mut db = Database::new("./hello.dino");
8
9    // Load and create the database if does not exist
10    db.load();
11
12    // Insert values in the db in the format of key, value
13    db.insert("key", "q");
14
15    // Create a new sub Tree in the main Tree of the db
16    let mut data_tree = Tree::new();
17
18    // Insert the key and value in the sub tree
19    data_tree.insert("b", "c");
20
21    // The length of items in the sub tree in the database
22    // This also shows almost all of the functions in Database are also avaliable in Tree
23    println!("The length of items in the sub tree in the database is: {}", data_tree.len());
24
25    // Insert the [data_tree] under the main tree
26    db.insert_tree("id", data_tree);
27
28    // Print the value of id
29    println!("The value of key: id is:\n{}", db.find("id").unwrap());
30
31    match db.find("not_exists") {
32        Ok(_value) => {
33            println!("This is unfortunate :(")
34        }
35
36        Err(error) => {
37            println!("Everting works! Here is the error for reference: {}", error)
38        }
39    }
40
41    // Remove a key in the database with its value
42    db.remove("id");
43
44    // Now here it wont print that it exists as it does not we removed it ^^^^^
45    if db.contains_key("id") {
46        println!("The key `id` exists!")
47    };
48
49    println!("The length of items in the database is: {}", db.len());
50
51    // Insert a number in the database
52    db.insert_number("test", 1);
53
54    println!("{}", db.find("test").unwrap().to_number() + 1); // This will print 2
55}
Source

pub fn insert_number(&mut self, key: &str, value: usize)

Insert data with usize value type in the sub tree

Source

pub fn len(&mut self) -> usize

Return the length of items that are in the sub tree

Examples found in repository?
examples/database.rs (line 23)
5fn main() {
6    // Create the database instance
7    let mut db = Database::new("./hello.dino");
8
9    // Load and create the database if does not exist
10    db.load();
11
12    // Insert values in the db in the format of key, value
13    db.insert("key", "q");
14
15    // Create a new sub Tree in the main Tree of the db
16    let mut data_tree = Tree::new();
17
18    // Insert the key and value in the sub tree
19    data_tree.insert("b", "c");
20
21    // The length of items in the sub tree in the database
22    // This also shows almost all of the functions in Database are also avaliable in Tree
23    println!("The length of items in the sub tree in the database is: {}", data_tree.len());
24
25    // Insert the [data_tree] under the main tree
26    db.insert_tree("id", data_tree);
27
28    // Print the value of id
29    println!("The value of key: id is:\n{}", db.find("id").unwrap());
30
31    match db.find("not_exists") {
32        Ok(_value) => {
33            println!("This is unfortunate :(")
34        }
35
36        Err(error) => {
37            println!("Everting works! Here is the error for reference: {}", error)
38        }
39    }
40
41    // Remove a key in the database with its value
42    db.remove("id");
43
44    // Now here it wont print that it exists as it does not we removed it ^^^^^
45    if db.contains_key("id") {
46        println!("The key `id` exists!")
47    };
48
49    println!("The length of items in the database is: {}", db.len());
50
51    // Insert a number in the database
52    db.insert_number("test", 1);
53
54    println!("{}", db.find("test").unwrap().to_number() + 1); // This will print 2
55}
Source

pub fn remove(&mut self, key: &str)

Remove a key in the sub tree in the database with its value

Source

pub fn find(&self, key: &str) -> Result<Value, String>

Find a value in the sub tree in the database

Source

pub fn contains_key(&mut self, key: &str) -> bool

Check if the key exists in the sub tree of the main database

Source

pub fn insert_tree(&mut self, key: &str, value: Tree)

Insert a key with a subtree in the subtree!

Trait Implementations§

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

impl Display for Tree So we can print the tree to the display

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