142 lines
3.0 KiB
Rust
142 lines
3.0 KiB
Rust
use std::path::Path;
|
|
|
|
use shared::errors::ErrorStatus;
|
|
use shared::names;
|
|
use shared::{errors::Error, names::Site};
|
|
|
|
use crate::arguments::PrintGroup;
|
|
|
|
fn group_printing(seperator: &Option<char>, site: &Site, group: &PrintGroup) {
|
|
let mut string = String::new();
|
|
let delimiter = seperator.unwrap_or(',');
|
|
|
|
if group.url {
|
|
string += &site.url;
|
|
}
|
|
|
|
if group.name {
|
|
if !string.is_empty() {
|
|
string += &format!(
|
|
"{}{}",
|
|
delimiter,
|
|
site.name.as_ref().unwrap_or(&"None".into())
|
|
)
|
|
} else {
|
|
string += &site.name.as_ref().unwrap_or(&"None".into());
|
|
}
|
|
}
|
|
|
|
log::info!("{}", string);
|
|
}
|
|
|
|
fn json_printing(site: &Site) -> Result<(), Error> {
|
|
match serde_json::to_string(&site) {
|
|
Ok(json) => {
|
|
log::info!("{}", json);
|
|
Ok(())
|
|
}
|
|
Err(err) => Err(Error {
|
|
status: ErrorStatus::ParsingError,
|
|
data: err.to_string(),
|
|
}),
|
|
}
|
|
}
|
|
|
|
fn filter_site(
|
|
site: &Site,
|
|
json: bool,
|
|
seperator: &Option<char>,
|
|
group: &PrintGroup,
|
|
) -> Result<(), Error> {
|
|
if json {
|
|
json_printing(site)?;
|
|
return Ok(());
|
|
}
|
|
|
|
if !group.url && !group.name {
|
|
log::info!("{:?}", site);
|
|
return Ok(());
|
|
}
|
|
|
|
return Ok(group_printing(seperator, site, group));
|
|
}
|
|
|
|
pub(crate) fn print(
|
|
path: &Path,
|
|
filter: &Option<String>,
|
|
group: &PrintGroup,
|
|
seperator: &Option<char>,
|
|
json: bool,
|
|
) -> Result<(), Error> {
|
|
let names_file = names::read_names_file(path)?;
|
|
let mut names = names::load_names(names_file)?;
|
|
|
|
if let Some(filter) = filter {
|
|
names.retain(|f| &f.url == filter);
|
|
if names.len() == 0 {
|
|
return Err(Error {
|
|
status: ErrorStatus::NotFoundError,
|
|
data: "this url was not found in names.json".into(),
|
|
});
|
|
}
|
|
return filter_site(&names[0], json, seperator, group);
|
|
}
|
|
|
|
for site in names {
|
|
if json {
|
|
json_printing(&site)?;
|
|
continue;
|
|
}
|
|
|
|
if !group.url && !group.name {
|
|
log::info!("{:?}", site);
|
|
continue;
|
|
}
|
|
|
|
group_printing(seperator, &site, group);
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub(crate) fn add(path: &Path, url: &String, name: &Option<String>) -> Result<(), Error> {
|
|
let names_file = names::read_names_file(path)?;
|
|
let mut names = names::load_names(names_file)?;
|
|
|
|
if names.iter().any(|site| site.url.contains(url)) {
|
|
return Err(Error {
|
|
status: ErrorStatus::AlreadyExistsError,
|
|
data:
|
|
"this url already exists in names.json. you can't have more then 1 of the same url."
|
|
.into(),
|
|
});
|
|
}
|
|
|
|
let site = Site {
|
|
url: url.to_string(),
|
|
name: name.to_owned(),
|
|
};
|
|
log::debug!("adding {:?} to {}", site, path.display());
|
|
names.push(site.clone());
|
|
let json = serde_json::to_string(&names).unwrap();
|
|
std::fs::write(path, json).unwrap();
|
|
log::info!("added {:?} to names.json", site);
|
|
Ok(())
|
|
}
|
|
|
|
pub(crate) fn remove(path: &Path, url: &String) -> Result<(), Error> {
|
|
let names_file = names::read_names_file(path)?;
|
|
let mut names = names::load_names(names_file)?;
|
|
|
|
names.retain(|site| {
|
|
if &site.url == url {
|
|
log::info!("removing {:?} from names.json", site);
|
|
}
|
|
&site.url != url
|
|
});
|
|
let json = serde_json::to_string(&names).unwrap();
|
|
std::fs::write(path, json).unwrap();
|
|
|
|
Ok(())
|
|
}
|