add an error if you try to add a url that exists.

This commit is contained in:
Fries 2023-07-01 22:14:33 -07:00
parent a08502cc73
commit 0c5fce59ca
2 changed files with 21 additions and 3 deletions

View file

@ -42,7 +42,12 @@ fn json_printing(site: &Site) -> Result<(), Error> {
} }
} }
fn filter_site(site: &Site, json: bool, seperator: &Option<char>, group: &PrintGroup) -> Result<(), Error> { fn filter_site(
site: &Site,
json: bool,
seperator: &Option<char>,
group: &PrintGroup,
) -> Result<(), Error> {
if json { if json {
json_printing(site)?; json_printing(site)?;
return Ok(()); return Ok(());
@ -69,7 +74,10 @@ pub(crate) fn print(
if let Some(filter) = filter { if let Some(filter) = filter {
names.retain(|f| &f.url == filter); names.retain(|f| &f.url == filter);
if names.len() == 0 { if names.len() == 0 {
return Err(Error { status: ErrorStatus::NotFoundError, data: "this url was not found in names.json".into() }) return Err(Error {
status: ErrorStatus::NotFoundError,
data: "this url was not found in names.json".into(),
});
} }
return filter_site(&names[0], json, seperator, group); return filter_site(&names[0], json, seperator, group);
} }
@ -95,6 +103,15 @@ pub(crate) fn add(path: &Path, url: &String, name: &Option<String>) -> Result<()
let names_file = names::read_names_file(path)?; let names_file = names::read_names_file(path)?;
let mut names = names::load_names(names_file)?; 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 { let site = Site {
url: url.to_string(), url: url.to_string(),
name: name.to_owned(), name: name.to_owned(),

View file

@ -4,7 +4,8 @@ pub enum ErrorStatus {
ParsingError, ParsingError,
DirectoriesError, DirectoriesError,
LoggerInitializationError, LoggerInitializationError,
NotFoundError NotFoundError,
AlreadyExistsError
} }
pub struct Error { pub struct Error {