merged Error into 1; simplified some logic
This commit is contained in:
parent
8ab2690d17
commit
c98558c41d
26
src/error.rs
Normal file
26
src/error.rs
Normal file
@ -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),
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
mod common;
|
||||
mod error;
|
||||
mod repo;
|
||||
mod repository;
|
||||
mod ui;
|
||||
|
17
src/repo.rs
17
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 {
|
||||
|
@ -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<super::Repo, Error> {
|
||||
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::<Self>() {
|
||||
Ok(result) => result,
|
||||
Err(e) => return Err(Error::Converting(format!("invalid json: {}", e))),
|
||||
};
|
||||
|
||||
let tags = response.json::<Self>()?;
|
||||
if tags.results.is_empty() {
|
||||
return Err(Error::NoTagsFound);
|
||||
}
|
||||
|
@ -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<String>,
|
||||
@ -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<Self> {
|
||||
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<String, Error> {
|
||||
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))),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user