diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..c286c10 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,26 @@ +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum Error { + #[error("Expected a tag")] + NoTagFound, + + #[error("Unexpected input")] + MisformedInput, + + /// a serde error + #[error("Converting error: {0}")] + Converting(String), + + /// invalid repos show a valid json with 0 tags + #[error("Given Repo does not exists or has 0 tags.")] + NoTagsFound, + + /// converting serde error + #[error("Serde error: {0}")] + Serde(#[from] serde_json::Error), + + /// error while handling requests + #[error("reqwest error: {0}")] + Reqwest(#[from] reqwest::Error), +} diff --git a/src/main.rs b/src/main.rs index 982fd04..69be117 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ mod common; +mod error; mod repo; mod repository; mod ui; diff --git a/src/repo.rs b/src/repo.rs index 99ea687..25565d1 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -1,21 +1,6 @@ -use std::fmt; - use regex::Regex; -#[derive(Debug, PartialEq)] -pub enum Error { - NoTagFound, - MisformedInput, -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Error::NoTagFound => write!(f, "Expected a tag"), - Error::MisformedInput => write!(f, "Unexpected input"), - } - } -} +use crate::error::Error; #[derive(Debug, PartialEq)] pub enum Repo { diff --git a/src/repository/dockerhub.rs b/src/repository/dockerhub.rs index ed3b587..890fde5 100644 --- a/src/repository/dockerhub.rs +++ b/src/repository/dockerhub.rs @@ -1,6 +1,6 @@ use serde::Deserialize; -use crate::repository::Error; +use crate::error::Error; #[derive(Deserialize, Debug, Clone)] struct ImageDetails { @@ -53,17 +53,10 @@ impl DockerHub { /// fetches tag information from a url pub fn with_url(url: &str) -> Result { - let response = match reqwest::blocking::get(url) { - Ok(result) => result, - Err(e) => return Err(Error::Fetching(format!("reqwest error: {}", e))), - }; + let response = reqwest::blocking::get(url)?; //convert it to json - let tags = match response.json::() { - Ok(result) => result, - Err(e) => return Err(Error::Converting(format!("invalid json: {}", e))), - }; - + let tags = response.json::()?; if tags.results.is_empty() { return Err(Error::NoTagsFound); } diff --git a/src/repository/mod.rs b/src/repository/mod.rs index 766d92a..b862944 100644 --- a/src/repository/mod.rs +++ b/src/repository/mod.rs @@ -1,24 +1,11 @@ mod dockerhub; use chrono::DateTime; -use thiserror::Error; use crate::common::display_duration_ext::DisplayDurationExt; +use crate::error::Error; use crate::repo; -#[derive(Debug, PartialEq, Error)] -pub enum Error { - /// couldn't fetch json with reqwest - #[error("Fetching error: {0}")] - Fetching(String), - /// a serde error - #[error("Converting error: {0}")] - Converting(String), - /// invalid repos show a valid json with 0 tags - #[error("Given Repo does not exists or has 0 tags.")] - NoTagsFound, -} - #[derive(Clone, PartialEq)] pub struct TagDetails { pub arch: Option, @@ -77,7 +64,9 @@ impl Repo { if registry.unwrap_or_default().is_empty() { dockerhub::DockerHub::create_repo(&repo) } else { - Err(Error::Converting("This registry is not supported".into())) + Err(Error::Converting( + "This registry is not supported yet".into(), + )) } } @@ -91,26 +80,23 @@ impl Repo { } pub fn next_page(&self) -> Option { - match &self.next_page { - Some(url) => match Self::with_url(url) { - Ok(tags) => Some(tags), - Err(_) => None, - }, - None => None, + if let Some(url) = &self.next_page { + match Self::with_url(url) { + Ok(tags) => return Some(tags), + Err(e) => println!("Encountered error: {e}"), + } } + None } } /// checks the repo name and may add a prefix for official images pub fn check_repo(name: &str) -> Result { - let repo = match repo::split_tag_from_repo(name) { - Err(e) => return Err(Error::Converting(format!("{}", e))), - Ok((name, _)) => name, - }; + let repo = repo::split_tag_from_repo(name)?; match repo::split_repo_without_tag(name) { Ok(repo::Repo::Project(s)) => Ok(format!("library/{}", s)), - Ok(_) => Ok(repo.to_string()), + Ok(_) => Ok(repo.0.to_string()), Err(e) => Err(Error::Converting(format!("{}", e))), } }