Alice_DBMS/
utils.rs

1/*                          MIT License
2
3Copyright (c) 2024 Daniil Ermolaev
4
5Permission is hereby granted, free of charge, to any person obtaining a copy
6of this software and associated documentation files (the "Software"), to deal
7in the Software without restriction, including without limitation the rights
8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9copies of the Software, and to permit persons to whom the Software is
10furnished to do so, subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all
13copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21SOFTWARE. */
22
23use std::{ fs, env };
24use std::io::{ self, Read, Write };
25use std::path::{ PathBuf, Path };
26
27use log::{ info, error, trace };
28use simplelog::*;
29
30use chrono::Local;
31
32const ROOT_DIR: &str = "Alice-Database-Data";
33const ADB_DATA_DIR: &str = "ADB_Data";
34const JSON_ENGINE_DIR: &str = "json_engine";
35const ADB_LOGS_DIR: &str = "ADB_Logs";
36
37pub fn get_root_path() -> PathBuf {
38    let root_path = match prepare() {
39        Ok(k) => k,
40        _ => panic!("Errors while preparing..."),
41    };
42    return root_path;
43}
44
45
46pub fn prepare() -> std::io::Result<PathBuf> {
47    print_ascii();
48    // Get the home directory
49    let home_dir = env::home_dir().expect("Failed to get home directory");
50    let base_path = home_dir.join(ROOT_DIR);
51    let adb_data_path = base_path.join(ADB_DATA_DIR);
52    let adb_logs_path = base_path.join(ADB_LOGS_DIR);
53
54    // Ensure the base and log directories exist
55    fs::create_dir_all(&adb_data_path).expect("Failed to create ADB_Data directory");
56    fs::create_dir_all(&adb_logs_path).expect("Failed to create ADB_Logs directory");
57
58    // Define the data path for JSON documents
59    let root_path = adb_data_path.join(JSON_ENGINE_DIR);
60
61    // Ensure the JSON engine directory exists
62    fs::create_dir_all(&root_path).expect("Failed to create json_engine directory");
63    // Generate a unique log filename using timestamp
64    let timestamp = Local::now().format("%Y-%m-%d_%H-%M-%S").to_string();
65    let log_file_path = format!("{}/{}.adb.log", adb_logs_path.display(), timestamp);
66
67    // Set up logging configuration
68    let log_config = ConfigBuilder::new().build();
69
70    CombinedLogger::init(
71        vec![
72            TermLogger::new(
73                LevelFilter::Trace,
74                log_config.clone(),
75                TerminalMode::Mixed,
76                ColorChoice::Auto
77            ),
78            WriteLogger::new(
79                LevelFilter::Trace,
80                log_config.clone(),
81                fs::File::create(log_file_path)
82                    .unwrap()
83            ),
84        ]
85    ).unwrap();
86    Ok(root_path.clone())
87    
88}
89
90pub fn print_ascii() {
91    println!(r"
92    @---------------------------------------------------------------@
93    |        ______     __         __     ______     ______         |
94    |       /\  __ \   /\ \       /\ \   /\  ___\   /\  ___\        |
95    |       \ \  __ \  \ \ \____  \ \ \  \ \ \____  \ \  __\        |
96    |        \ \_\ \_\  \ \_____\  \ \_\  \ \_____\  \ \_____\      |
97    |         \/_/\/_/   \/_____/   \/_/   \/_____/   \/_____/      |
98    |                                                               |
99    |                     _____     ______                          |
100    |                    /\  __-.  /\  == \                         |
101    |                    \ \ \/\ \ \ \  __<                         |
102    |                     \ \____-  \ \_____\                       |
103    |                      \/____/   \/_____/                       |
104    |                                                               |
105    @---------------------------------------------------------------@
106    ")
107}