meowy_webring::main now uses the shared crate

this has abstractions for getting the names.json file and reading them
and parsing them. i also moved the cli logger initialization code into
its own module.
This commit is contained in:
Fries 2023-07-01 19:49:48 -07:00
parent 8206cc6105
commit b58b0cd78f
7 changed files with 46 additions and 22 deletions

2
Cargo.lock generated
View file

@ -767,11 +767,13 @@ version = "0.1.0"
dependencies = [
"askama",
"askama_rocket",
"log",
"rocket",
"rust-embed",
"serde",
"serde_json",
"shared",
"simple_logger",
]
[[package]]

View file

@ -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

18
cli/src/logging.rs Normal file
View file

@ -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(())
}

View file

@ -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),

View file

@ -21,6 +21,11 @@ pub fn get_file_from_directory(path: &Path, filename: &str) -> Result<PathBuf, E
Ok(path.join(filename))
}
pub fn get_names_path() -> Result<PathBuf, Error> {
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(_) => {

View file

@ -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 {

View file

@ -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(