add a remove command and error handling to the cli

This commit is contained in:
Fries 2023-06-30 23:12:55 -07:00
parent c729d2115b
commit 9411e4a097
11 changed files with 65 additions and 91 deletions

38
Cargo.lock generated
View File

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

View File

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

View File

@ -33,6 +33,7 @@ pub(crate) enum Commands {
)]
name: Option<String>,
},
#[command(about = "remove a site from the webring")]
Remove {
#[arg(
long,

View File

@ -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<Site> {
let names = std::fs::read_to_string(path).unwrap();
shared::names::load_names(names).unwrap()
fn read_names(path: &String) -> Result<Vec<Site>, 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<String>) {
let mut names = read_names(path);
pub(crate) fn add(path: &String, url: &String, name: &Option<String>) -> 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<String>) {
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(())
}

View File

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

16
shared/src/errors.rs Normal file
View File

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

View File

@ -1 +1,2 @@
pub mod names;
pub mod errors;

View File

@ -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<String>,
}
#[derive(Debug)]
pub enum NamesError {
FileAccessError,
ParseError,
}
pub fn load_names(names: String) -> Result<Vec<Site>, NamesError> {
pub fn load_names(names: String) -> Result<Vec<Site>, Error> {
match serde_json::from_str::<Vec<Site>>(&names) {
Ok(content) => Ok(content),
Err(_) => Err(NamesError::ParseError),
Err(err) => Err(Error {
status: ErrorStatus::ParsingError,
data: err.to_string(),
}),
}
}

View File

@ -4,7 +4,6 @@ extern crate rocket;
mod assets;
mod links;
mod routes;
mod names;
#[launch]
fn rocket() -> _ {

View File

@ -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,
// }
// }
// }

View File

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