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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#[macro_use]
extern crate bitflags;

use std::collections::{HashMap, HashSet};
use std::env;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::result;

mod attributes;
use crate::attributes::{Attribute, Id};

pub trait database {
    fn new();
    fn create_attribute();
    fn remove_attribute();
    fn update_attribute();
}

pub struct Database {
    base_dir: PathBuf,
    attribute_handler: AttributeHandler,
    user_handler: UserHandler,
}

impl Database {
    fn new() -> Self {
        // Create reference to base directory.
        let mut base_dir = match env::current_dir() {
            Ok(dir) => dir,
            Err(err) => panic!(err),
        };
        // Check to see if database directory already exists.
        //
        // TODO: change base dir `database` string
        base_dir.push("database");
        fs::create_dir(&base_dir).unwrap_or(());

        let attribute_handler = AttributeHandler::new(&base_dir);
        let user_handler = UserHandler::new(&base_dir);

        Database {
            base_dir,
            attribute_handler,
            user_handler,
        }
    }

    #[allow(dead_code)]
    fn teardown(&self) {
        fs::remove_dir(&self.base_dir).unwrap_or(());
    }
}

pub struct UserHandler {
    base_dir: PathBuf,
}

impl UserHandler {
    fn new<P: AsRef<Path>>(parent_path: P) -> Self {
        let base_dir = parent_path.as_ref().join("users");
        fs::create_dir(&base_dir).unwrap_or(());

        UserHandler { base_dir }
    }

    #[allow(dead_code)]
    fn teardown(&self) {
        fs::remove_dir(&self.base_dir).unwrap_or(());
    }
}

pub struct AttributeHandler {
    base_dir: PathBuf,
    cache: HashMap<Id, Attribute>,
}

impl AttributeHandler {
    fn new<P: AsRef<Path>>(parent_path: P) -> Self {
        let base_dir = parent_path.as_ref().join("attributes");
        fs::create_dir(&base_dir).unwrap_or(());

        AttributeHandler {
            base_dir,
            cache: HashMap::new(),
        }
    }

    #[allow(dead_code)]
    fn teardown(&self) {
        fs::remove_dir(&self.base_dir).unwrap_or(());
    }
}

mod tests {
    use super::{env, AttributeHandler, Database, UserHandler};

    #[test]
    #[allow(non_snake_case)]
    fn test_AttributeHandler() {
        let current_dir = env::current_dir().expect("Could not enter directory");
        let expected_base_dir = current_dir.join("attributes");

        let attribute_handler = AttributeHandler::new(&current_dir);
        assert_eq!(attribute_handler.base_dir, expected_root);

        attribute_handler.teardown();
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_UserHandler() {
        let current_dir = env::current_dir().expect("Could not enter directory");
        let expected_base_dir = current_dir.join("users");

        let user_handler = UserHandler::new(&current_dir);
        assert_eq!(user_handler.base_dir, expected_root);

        user_handler.teardown();
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_Database() {
        let current_dir = env::current_dir().expect("Could not enter directory");
        let expected_base_dir = current_dir.join("database");

        let db = Database::new();
        assert_eq!(db.base_dir, expected_root);

        db.teardown();
    }
}