1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use avlon_db::AvlonDB;
use serde::{Deserialize, Serialize};

/// A struct representing a user account.
#[derive(Serialize, Deserialize, Debug)]
pub struct Account {
    pub username: String,
    pub password: String,
    pub age: i32,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Define the database name and will create a same named directory in the current directory.
    let db_name = String::from("test_db");

    // Create an instance of Account
    let account = Account {
        username: String::from("johndoe"),
        password: String::from("secretpassword"),
        age: 30,
    };

    // Initialize the database
    let db = AvlonDB::new(db_name);

    // Save the account to the database
    db.save(account.username.clone(), account)?;
    println!("Account saved to database!");

    // Load the account from the database
    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
        println!("Account loaded from database: {:?}", loaded_account);
    } else {
        println!("Account not found in the database.");
    }

    // Update the account in the database
    let updated_account = Account {
        username: String::from("joker"),
        password: String::from("123654987"),
        age: 99,
    };
    db.update("johndoe", updated_account)?;

    // Load the updated account from the database
    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
        println!("Updated account loaded from database: {:?}", loaded_account);
    } else {
        println!("Account not found in the database.");
    }

    // Remove the account from the database
    db.remove("johndoe")?;

    // Attempt to load the account again after removal
    if let Some(loaded_account) = db.load::<Account>("johndoe")? {
        println!(
            "Account loaded from database after removal: {:?}",
            loaded_account
        );
    } else {
        println!("Account successfully removed from the database.");
    }

    Ok(())
}