Browse Source

Added basic environment detection functionality.

a
sergiotarxz 1 year ago
commit
bf64633a75
7 changed files with 99 additions and 0 deletions
  1. +2
    -0
      .gitignore
  2. +12
    -0
      Cargo.toml
  3. BIN
      src/.environment.rs.swp
  4. +54
    -0
      src/environment.rs
  5. +1
    -0
      src/lib.rs
  6. +6
    -0
      src/main.rs
  7. +24
    -0
      src/tor_chat/enviroment.rs

+ 2
- 0
.gitignore View File

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

+ 12
- 0
Cargo.toml 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 View File


+ 54
- 0
src/environment.rs 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
- 0
src/lib.rs View File

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

+ 6
- 0
src/main.rs View File

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

+ 24
- 0
src/tor_chat/enviroment.rs 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,
}
}
}
}
}

Loading…
Cancel
Save