commit bf64633a75b0b0a1e5f0d4b2c8ef983a564664fc Author: sergiotarxz Date: Mon Jun 8 13:14:29 2020 +0200 Added basic environment detection functionality. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4fffb2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..05983e3 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "tor_chat" +version = "0.1.0" +authors = ["sergiotarxz "] +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" diff --git a/src/.environment.rs.swp b/src/.environment.rs.swp new file mode 100644 index 0000000..2a8d895 Binary files /dev/null and b/src/.environment.rs.swp differ diff --git a/src/environment.rs b/src/environment.rs new file mode 100644 index 0000000..5c0e602 --- /dev/null +++ b/src/environment.rs @@ -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, +} +impl Environment { + pub fn detect() -> Option { + 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() + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..640a793 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod environment; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..8d48a26 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,6 @@ +fn main() { + let environment = tor_chat::environment::Environment::detect(); + if let Some(environment) = environment { + println!("{:#?}", environment); + } +} diff --git a/src/tor_chat/enviroment.rs b/src/tor_chat/enviroment.rs new file mode 100644 index 0000000..c4f79ce --- /dev/null +++ b/src/tor_chat/enviroment.rs @@ -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 { + 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, + } + } + } + } +}