From 9411e4a09724768d9d4965cc52ecd79e5c8fb0a8 Mon Sep 17 00:00:00 2001 From: Fries Date: Fri, 30 Jun 2023 23:12:55 -0700 Subject: [PATCH] add a remove command and error handling to the cli --- Cargo.lock | 38 -------------------------------------- Cargo.toml | 4 ++-- cli/src/arguments.rs | 1 + cli/src/commands.rs | 40 +++++++++++++++++++++++++++++----------- cli/src/main.rs | 15 +++++++++------ shared/src/errors.rs | 16 ++++++++++++++++ shared/src/lib.rs | 1 + shared/src/names.rs | 15 +++++++-------- src/main.rs | 1 - src/names.rs | 24 ------------------------ src/routes.rs | 1 - 11 files changed, 65 insertions(+), 91 deletions(-) create mode 100644 shared/src/errors.rs delete mode 100644 src/names.rs diff --git a/Cargo.lock b/Cargo.lock index e7b8a18..d7d739a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -73,9 +73,6 @@ source = "git+https://github.com/djc/askama.git?rev=b9e51601560398766eac445517fb dependencies = [ "askama_derive", "askama_escape", - "humansize", - "num-traits", - "percent-encoding", ] [[package]] @@ -83,13 +80,11 @@ name = "askama_derive" version = "0.12.1" source = "git+https://github.com/djc/askama.git?rev=b9e51601560398766eac445517fb17c35090a952#b9e51601560398766eac445517fb17c35090a952" dependencies = [ - "basic-toml", "mime", "mime_guess", "nom", "proc-macro2", "quote", - "serde", "syn", ] @@ -167,15 +162,6 @@ dependencies = [ "rustc-demangle", ] -[[package]] -name = "basic-toml" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c0de75129aa8d0cceaf750b89013f0e08804d6ec61416da787b35ad0d7cddf1" -dependencies = [ - "serde", -] - [[package]] name = "binascii" version = "0.1.4" @@ -600,15 +586,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" -[[package]] -name = "humansize" -version = "2.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6cb51c9a029ddc91b07a787f1d86b53ccfa49b0e86688c946ebe8d3555685dd7" -dependencies = [ - "libm", -] - [[package]] name = "hyper" version = "0.14.27" @@ -709,12 +686,6 @@ version = "0.2.147" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" -[[package]] -name = "libm" -version = "0.2.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7012b1bbb0719e1097c47611d3898568c546d597c2e74d66f6087edd5233ff4" - [[package]] name = "linux-raw-sys" version = "0.3.8" @@ -862,15 +833,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "num-traits" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" -dependencies = [ - "autocfg", -] - [[package]] name = "num_cpus" version = "1.15.0" diff --git a/Cargo.toml b/Cargo.toml index 005d5e8..aa5abe7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,14 +27,14 @@ version = "1.0" git = "https://github.com/djc/askama.git" package = "askama_rocket" rev = "b9e51601560398766eac445517fb17c35090a952" +default-features = false [dependencies.askama] git = "https://github.com/djc/askama.git" package = "askama" rev = "b9e51601560398766eac445517fb17c35090a952" version = "0.12" -default-features = true -features = ["with-rocket", "mime", "mime_guess"] +default-features = false [dependencies.shared] path = "./shared" diff --git a/cli/src/arguments.rs b/cli/src/arguments.rs index 413b6f5..952da36 100644 --- a/cli/src/arguments.rs +++ b/cli/src/arguments.rs @@ -33,6 +33,7 @@ pub(crate) enum Commands { )] name: Option, }, + #[command(about = "remove a site from the webring")] Remove { #[arg( long, diff --git a/cli/src/commands.rs b/cli/src/commands.rs index f21906c..e2cdebb 100644 --- a/cli/src/commands.rs +++ b/cli/src/commands.rs @@ -1,17 +1,26 @@ -use shared::names::Site; +use shared::{ + errors::{Error, ErrorStatus}, + names::Site, +}; use crate::arguments::PrintGroup; -fn read_names(path: &String) -> Vec { - let names = std::fs::read_to_string(path).unwrap(); - shared::names::load_names(names).unwrap() +fn read_names(path: &String) -> Result, Error> { + match std::fs::read_to_string(path) { + Ok(names) => { + return shared::names::load_names(names); + } + Err(err) => Err(Error { + status: ErrorStatus::IOError, + data: err.to_string(), + }), + } } -pub(crate) fn print(path: &String, group: &PrintGroup) { - let names = std::fs::read_to_string(path).unwrap(); - let parsed_names = shared::names::load_names(names).unwrap(); +pub(crate) fn print(path: &String, group: &PrintGroup) -> Result<(), Error> { + let names = read_names(path)?; - for site in parsed_names { + Ok(for site in names { if group.name { println!("{}", site.name.unwrap_or_default()); continue; @@ -21,11 +30,11 @@ pub(crate) fn print(path: &String, group: &PrintGroup) { continue; } println!("{:?}", site); - } + }) } -pub(crate) fn add(path: &String, url: &String, name: &Option) { - let mut names = read_names(path); +pub(crate) fn add(path: &String, url: &String, name: &Option) -> Result<(), Error> { + let mut names = read_names(path)?; let site = Site { url: url.to_string(), name: name.to_owned(), @@ -33,4 +42,13 @@ pub(crate) fn add(path: &String, url: &String, name: &Option) { names.push(site); let json = serde_json::to_string(&names).unwrap(); std::fs::write(path, json).unwrap(); + Ok(()) +} + +pub(crate) fn remove(path: &String, url: &String) -> Result<(), Error> { + let mut names = read_names(path)?; + names.retain(|site| &site.url != url); + let json = serde_json::to_string(&names).unwrap(); + std::fs::write(path, json).unwrap(); + Ok(()) } diff --git a/cli/src/main.rs b/cli/src/main.rs index e6f330e..621e6b3 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,16 +1,19 @@ use arguments::{Arguments, Commands}; use clap::Parser; -use commands::{add, print}; +use commands::{add, print, remove}; +use shared::errors::Error; mod arguments; mod commands; -fn main() { +fn main() -> Result<(), Error> { let args = Arguments::parse(); match &args.command { - Commands::Print { group } => print(&args.path, &group), - Commands::Add { url, name } => add(&args.path, url, name), - Commands::Remove { url } => todo!(), - } + Commands::Print { group } => print(&args.path, &group)?, + Commands::Add { url, name } => add(&args.path, url, name)?, + Commands::Remove { url } => remove(&args.path, url)?, + }; + + Ok(()) } diff --git a/shared/src/errors.rs b/shared/src/errors.rs new file mode 100644 index 0000000..8a033d5 --- /dev/null +++ b/shared/src/errors.rs @@ -0,0 +1,16 @@ +#[derive(Debug)] +pub enum ErrorStatus { + IOError, + ParsingError +} + +pub struct Error { + pub status: ErrorStatus, + pub data: String +} + +impl core::fmt::Debug for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "A {:?} error has occured.\nDetails: {}", self.status, self.data) + } +} diff --git a/shared/src/lib.rs b/shared/src/lib.rs index d2f54b1..c986a43 100644 --- a/shared/src/lib.rs +++ b/shared/src/lib.rs @@ -1 +1,2 @@ pub mod names; +pub mod errors; diff --git a/shared/src/names.rs b/shared/src/names.rs index cbf8e4f..6408d6c 100644 --- a/shared/src/names.rs +++ b/shared/src/names.rs @@ -1,20 +1,19 @@ use serde::{Deserialize, Serialize}; +use crate::errors::{Error, ErrorStatus}; + #[derive(Serialize, Deserialize, Debug)] pub struct Site { pub url: String, pub name: Option, } -#[derive(Debug)] -pub enum NamesError { - FileAccessError, - ParseError, -} - -pub fn load_names(names: String) -> Result, NamesError> { +pub fn load_names(names: String) -> Result, Error> { match serde_json::from_str::>(&names) { Ok(content) => Ok(content), - Err(_) => Err(NamesError::ParseError), + Err(err) => Err(Error { + status: ErrorStatus::ParsingError, + data: err.to_string(), + }), } } diff --git a/src/main.rs b/src/main.rs index 2647299..ae43a88 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,6 @@ extern crate rocket; mod assets; mod links; mod routes; -mod names; #[launch] fn rocket() -> _ { diff --git a/src/names.rs b/src/names.rs deleted file mode 100644 index ab58c1c..0000000 --- a/src/names.rs +++ /dev/null @@ -1,24 +0,0 @@ -use serde::{Deserialize, Serialize}; -use serde_json::{Map, Value}; -use std::fs::File; -use std::io::{BufReader, Read}; - - - -// impl Site { -// pub fn new(url: &str, name: Option<&str>) -> Self { -// Site { -// url: url.to_string(), -// name: name.map(str::to_string), -// } -// } -// pub fn url(&self) -> &String { -// &self.url -// } -// pub fn name(&self) -> Option<&String> { -// match &self.name { -// Some(name) => Some(&name), -// None => None, -// } -// } -// } diff --git a/src/routes.rs b/src/routes.rs index 7b600cb..576669b 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,5 +1,4 @@ use crate::{links::{next_url, previous_url}, assets::ErrorTemplate}; -static NAMES: [&str; 3] = ["mossfet.xyz", "fries.gay", "ta-kev.digital"]; use rocket::{ http::Status,