55 lines
1.7 KiB
Rust
55 lines
1.7 KiB
Rust
extern crate dirs;
|
|
extern crate exitcode;
|
|
extern crate tempdir;
|
|
|
|
#[derive(Debug)]
|
|
pub struct Environment {
|
|
home_dir_path: std::path::PathBuf,
|
|
sqlite_path: std::path::PathBuf,
|
|
sqlite_database_exists: bool,
|
|
tmp_dir: Option<tempdir::TempDir>,
|
|
}
|
|
impl Environment {
|
|
pub fn detect() -> Option<crate::environment::Environment> {
|
|
let home_dir_path = dirs::home_dir();
|
|
if let Some(home_dir_path) = home_dir_path {
|
|
let mut sqlite_path = home_dir_path.clone();
|
|
sqlite_path.push(".tor_chat");
|
|
let sqlite_database_exists = sqlite_path.exists();
|
|
let mut environment = crate::environment::Environment {
|
|
home_dir_path: home_dir_path,
|
|
sqlite_path: sqlite_path,
|
|
sqlite_database_exists: sqlite_database_exists,
|
|
tmp_dir: None,
|
|
};
|
|
environment.generate_tmpdir();
|
|
let environment = Some(environment);
|
|
return environment;
|
|
}
|
|
return None;
|
|
}
|
|
|
|
pub fn generate_tmpdir(&mut self) {
|
|
let result_tmpdir = tempdir::TempDir::new("");
|
|
if let Ok(tmp_dir) = result_tmpdir {
|
|
self.tmp_dir = Some(tmp_dir);
|
|
} else if let Err(err) = result_tmpdir {
|
|
println!("{}", err);
|
|
println!("The crate tempdir isn't supporting your OS, file a bug.");
|
|
std::process::exit(1);
|
|
}
|
|
}
|
|
|
|
pub fn home_dir_path(self) -> std::path::PathBuf {
|
|
self.home_dir_path.clone()
|
|
}
|
|
|
|
pub fn sqlite_path(self) -> std::path::PathBuf {
|
|
self.sqlite_path.clone()
|
|
}
|
|
|
|
pub fn sqlite_database_exists(self) -> bool {
|
|
self.sqlite_database_exists.clone()
|
|
}
|
|
}
|