55 lines
1.2 KiB
Rust
55 lines
1.2 KiB
Rust
use shared::{
|
|
errors::{Error, ErrorStatus},
|
|
names::Site,
|
|
};
|
|
|
|
use crate::arguments::PrintGroup;
|
|
|
|
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) -> Result<(), Error> {
|
|
let names = read_names(path)?;
|
|
|
|
Ok(for site in names {
|
|
if group.name {
|
|
println!("{}", site.name.unwrap_or_default());
|
|
continue;
|
|
}
|
|
if group.url {
|
|
println!("{}", site.url);
|
|
continue;
|
|
}
|
|
println!("{:?}", site);
|
|
})
|
|
}
|
|
|
|
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(),
|
|
};
|
|
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(())
|
|
}
|