diff --git a/Cargo.lock b/Cargo.lock index 2361b82..16629f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -767,11 +767,13 @@ version = "0.1.0" dependencies = [ "askama", "askama_rocket", + "log", "rocket", "rust-embed", "serde", "serde_json", "shared", + "simple_logger", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index aa5abe7..40bebfb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,9 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[dependencies] +log = "0.4" + [dependencies.rocket] version = "=0.5.0-rc.3" default_features = false @@ -38,3 +41,7 @@ default-features = false [dependencies.shared] path = "./shared" + +[dependencies.simple_logger] +version = "4" +default-features = false diff --git a/cli/src/logging.rs b/cli/src/logging.rs new file mode 100644 index 0000000..112ccf6 --- /dev/null +++ b/cli/src/logging.rs @@ -0,0 +1,18 @@ +use log::LevelFilter; +use shared::errors::{Error, ErrorStatus}; +use simple_logger::SimpleLogger; + +pub fn initialize_logger() -> Result<(), Error> { + if let Err(err) = SimpleLogger::new() + .with_level(LevelFilter::Info) + .env() + .init() + { + return Err(Error { + status: ErrorStatus::LoggerInitializationError, + data: err.to_string(), + }); + } + + Ok(()) +} diff --git a/cli/src/main.rs b/cli/src/main.rs index 98f76fd..508a927 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,29 +1,18 @@ use std::path::Path; - use arguments::{Arguments, Commands}; use clap::Parser; use commands::{add, print, remove}; -use log::LevelFilter; -use shared::{ - directories, - errors::{Error, ErrorStatus}, -}; -use simple_logger::SimpleLogger; +use shared::{directories, errors::Error}; mod arguments; mod commands; +mod logging; fn main() -> Result<(), Error> { - if let Err(err) = SimpleLogger::new().with_level(LevelFilter::Info).env().init() { - return Err(Error { - status: ErrorStatus::LoggerInitializationError, - data: err.to_string(), - }); - } - - let directory = directories::get_project_dir()?; + logging::initialize_logger()?; + + let default_path = directories::get_names_path()?; let args = Arguments::parse(); - let default_path = directories::get_file_from_directory(directory.data_dir(), "names.json")?; let path = match &args.path { Some(path) => Path::new(path), diff --git a/shared/src/directories.rs b/shared/src/directories.rs index 195cf46..8f56795 100644 --- a/shared/src/directories.rs +++ b/shared/src/directories.rs @@ -21,6 +21,11 @@ pub fn get_file_from_directory(path: &Path, filename: &str) -> Result Result { + let directory = get_project_dir()?; + return get_file_from_directory(directory.data_dir(), "names.json"); +} + fn create_directory(path: &Path) -> Result<(), Error> { match std::fs::create_dir_all(path) { Ok(_) => { diff --git a/shared/src/names.rs b/shared/src/names.rs index 3683914..734587e 100644 --- a/shared/src/names.rs +++ b/shared/src/names.rs @@ -1,8 +1,6 @@ -use std::path::Path; - -use serde::{Deserialize, Serialize}; - use crate::errors::{Error, ErrorStatus}; +use serde::{Deserialize, Serialize}; +use std::path::Path; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Site { diff --git a/src/main.rs b/src/main.rs index ae43a88..505df51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use shared::{directories, names}; + #[macro_use] extern crate rocket; @@ -7,8 +9,11 @@ mod routes; #[launch] fn rocket() -> _ { - let names_file = std::fs::read_to_string("names.json").unwrap(); - let names = shared::names::load_names(names_file).unwrap(); + let names_path = directories::get_names_path().unwrap(); + println!("names.json path: {}", names_path.display()); + let names_file = names::read_names_file(&names_path).unwrap(); + let names = names::load_names(names_file).unwrap(); + rocket::build() .manage(names) .mount(