Added basic environment detection functionality.

This commit is contained in:
sergiotarxz 2020-06-08 13:14:29 +02:00
commit bf64633a75
7 changed files with 99 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
/Cargo.lock

12
Cargo.toml Normal file
View File

@ -0,0 +1,12 @@
[package]
name = "tor_chat"
version = "0.1.0"
authors = ["sergiotarxz <sergiotarxz@noplacelikelocalhost>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dirs = "2.0.2"
tempdir = "0.3.7"
exitcode = "1.1.0"

BIN
src/.environment.rs.swp Normal file

Binary file not shown.

54
src/environment.rs Normal file
View File

@ -0,0 +1,54 @@
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()
}
}

1
src/lib.rs Normal file
View File

@ -0,0 +1 @@
pub mod environment;

6
src/main.rs Normal file
View File

@ -0,0 +1,6 @@
fn main() {
let environment = tor_chat::environment::Environment::detect();
if let Some(environment) = environment {
println!("{:#?}", environment);
}
}

View File

@ -0,0 +1,24 @@
mod tor_chat {
pub struct Enviroment {
home_dir_path : std::path::Path,
sqlite_path : std::path::Path,
sqlite_database_exists: bool,
}
impl Enviroment {
pub fn detect() ~> Option<tor_chat::Enviroment> {
let home_dir_path = dirs::home_dir_path();
if let Some(home_dir_path) = &home_dir_path {
let mut sqlite_path = home_dir_path.clone();
sqlite_path.push(".tor_chat");
let sqlite_path = sqlite_path.as_path();
let sqlite_database_exists = sqlite_path.exists();
let home_dir_path = home_dir_path.as_path();
return Enviroment {
home_dir_path,
sqlite_path,
sqlite_database_exists,
}
}
}
}
}