opus_db/
lib.rs

1#[macro_use]
2extern crate bitflags;
3
4use std::collections::{HashMap, HashSet};
5use std::env;
6use std::fs;
7use std::io;
8use std::path::{Path, PathBuf};
9use std::result;
10
11mod attributes;
12use crate::attributes::{Attribute, Id};
13
14pub trait database {
15    fn new();
16    fn create_attribute();
17    fn remove_attribute();
18    fn update_attribute();
19}
20
21pub struct Database {
22    base_dir: PathBuf,
23    attribute_handler: AttributeHandler,
24    user_handler: UserHandler,
25}
26
27impl Database {
28    fn new() -> Self {
29        // Create reference to base directory.
30        let mut base_dir = match env::current_dir() {
31            Ok(dir) => dir,
32            Err(err) => panic!(err),
33        };
34        // Check to see if database directory already exists.
35        //
36        // TODO: change base dir `database` string
37        base_dir.push("database");
38        fs::create_dir(&base_dir).unwrap_or(());
39
40        let attribute_handler = AttributeHandler::new(&base_dir);
41        let user_handler = UserHandler::new(&base_dir);
42
43        Database {
44            base_dir,
45            attribute_handler,
46            user_handler,
47        }
48    }
49
50    #[allow(dead_code)]
51    fn teardown(&self) {
52        fs::remove_dir(&self.base_dir).unwrap_or(());
53    }
54}
55
56pub struct UserHandler {
57    base_dir: PathBuf,
58}
59
60impl UserHandler {
61    fn new<P: AsRef<Path>>(parent_path: P) -> Self {
62        let base_dir = parent_path.as_ref().join("users");
63        fs::create_dir(&base_dir).unwrap_or(());
64
65        UserHandler { base_dir }
66    }
67
68    #[allow(dead_code)]
69    fn teardown(&self) {
70        fs::remove_dir(&self.base_dir).unwrap_or(());
71    }
72}
73
74pub struct AttributeHandler {
75    base_dir: PathBuf,
76    cache: HashMap<Id, Attribute>,
77}
78
79impl AttributeHandler {
80    fn new<P: AsRef<Path>>(parent_path: P) -> Self {
81        let base_dir = parent_path.as_ref().join("attributes");
82        fs::create_dir(&base_dir).unwrap_or(());
83
84        AttributeHandler {
85            base_dir,
86            cache: HashMap::new(),
87        }
88    }
89
90    #[allow(dead_code)]
91    fn teardown(&self) {
92        fs::remove_dir(&self.base_dir).unwrap_or(());
93    }
94}
95
96mod tests {
97    use super::{env, AttributeHandler, Database, UserHandler};
98
99    #[test]
100    #[allow(non_snake_case)]
101    fn test_AttributeHandler() {
102        let current_dir = env::current_dir().expect("Could not enter directory");
103        let expected_base_dir = current_dir.join("attributes");
104
105        let attribute_handler = AttributeHandler::new(&current_dir);
106        assert_eq!(attribute_handler.base_dir, expected_root);
107
108        attribute_handler.teardown();
109    }
110
111    #[test]
112    #[allow(non_snake_case)]
113    fn test_UserHandler() {
114        let current_dir = env::current_dir().expect("Could not enter directory");
115        let expected_base_dir = current_dir.join("users");
116
117        let user_handler = UserHandler::new(&current_dir);
118        assert_eq!(user_handler.base_dir, expected_root);
119
120        user_handler.teardown();
121    }
122
123    #[test]
124    #[allow(non_snake_case)]
125    fn test_Database() {
126        let current_dir = env::current_dir().expect("Could not enter directory");
127        let expected_base_dir = current_dir.join("database");
128
129        let db = Database::new();
130        assert_eq!(db.base_dir, expected_root);
131
132        db.teardown();
133    }
134}