From 0c5fce59cadfa58fb40b2123da2e5868150cf2af Mon Sep 17 00:00:00 2001 From: Fries Date: Sat, 1 Jul 2023 22:14:33 -0700 Subject: [PATCH] add an error if you try to add a url that exists. --- cli/src/commands.rs | 21 +++++++++++++++++++-- shared/src/errors.rs | 3 ++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/cli/src/commands.rs b/cli/src/commands.rs index ecfac66..24f91c6 100644 --- a/cli/src/commands.rs +++ b/cli/src/commands.rs @@ -42,7 +42,12 @@ fn json_printing(site: &Site) -> Result<(), Error> { } } -fn filter_site(site: &Site, json: bool, seperator: &Option, group: &PrintGroup) -> Result<(), Error> { +fn filter_site( + site: &Site, + json: bool, + seperator: &Option, + group: &PrintGroup, +) -> Result<(), Error> { if json { json_printing(site)?; return Ok(()); @@ -69,7 +74,10 @@ pub(crate) fn print( 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 Err(Error { + status: ErrorStatus::NotFoundError, + data: "this url was not found in names.json".into(), + }); } return filter_site(&names[0], json, seperator, group); } @@ -95,6 +103,15 @@ pub(crate) fn add(path: &Path, url: &String, name: &Option) -> Result<() 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(), diff --git a/shared/src/errors.rs b/shared/src/errors.rs index dcb9eae..aa744fe 100644 --- a/shared/src/errors.rs +++ b/shared/src/errors.rs @@ -4,7 +4,8 @@ pub enum ErrorStatus { ParsingError, DirectoriesError, LoggerInitializationError, - NotFoundError + NotFoundError, + AlreadyExistsError } pub struct Error {