Compare commits

..

3 Commits

Author SHA1 Message Date
Thomas Eppers
74006af796 added a check to not show an error when load next page of tags 2021-10-27 14:10:07 +02:00
Thomas Eppers
d608fe6b50 changed function next_page from Result to Option 2021-10-27 13:40:55 +02:00
Thomas Eppers
3cfbc2a656 removed unused code 2021-10-27 13:40:41 +02:00
4 changed files with 35 additions and 42 deletions

View File

@ -2,24 +2,16 @@ use std::fmt;
use regex::Regex; use regex::Regex;
// use crate::common;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Error { pub enum Error {
// Conversion,
// Empty,
NoTagFound, NoTagFound,
// InvalidChar,
MisformedInput, MisformedInput,
} }
impl fmt::Display for Error { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
// Error::Conversion => write!(f, "Conversion error"),
// Error::Empty => write!(f, "Input is empty"),
Error::NoTagFound => write!(f, "Expected a tag"), Error::NoTagFound => write!(f, "Expected a tag"),
// Error::InvalidChar => write!(f, "Invalid character found"),
Error::MisformedInput => write!(f, "Unexpected input"), Error::MisformedInput => write!(f, "Unexpected input"),
} }
} }

View File

@ -7,7 +7,6 @@ use serde::Deserialize;
#[derive(Deserialize, Debug, Clone)] #[derive(Deserialize, Debug, Clone)]
struct ImageDetails { struct ImageDetails {
architecture: String, architecture: String,
// os: String,
size: usize, size: usize,
} }
@ -25,6 +24,30 @@ pub struct Images {
last_updated: String, last_updated: String,
} }
impl fmt::Display for Images {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
//architecture infos
let mut arch = String::new();
for image in self.images.iter().take(1) {
arch.push_str(&format!("{}", image));
}
for image in self.images.iter().skip(1) {
arch.push_str(&format!(", {}", image));
}
let now = chrono::Utc::now();
let rfc3339 = DateTime::parse_from_rfc3339(&self.last_updated).unwrap();
let dif = now - rfc3339.with_timezone(&chrono::Utc);
write!(
f,
"{} vor {} [{}]",
self.tag_name,
format_time_nice(dif),
arch
)
}
}
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Tags { pub struct Tags {
count: usize, count: usize,
@ -41,7 +64,6 @@ pub enum Error {
Converting(String), Converting(String),
/// invalid repos show a valid json with 0 tags /// invalid repos show a valid json with 0 tags
NoTagsFound, NoTagsFound,
NoNextPage,
} }
impl fmt::Display for Error { impl fmt::Display for Error {
@ -49,7 +71,6 @@ impl fmt::Display for Error {
match self { match self {
Error::Fetching(s) => write!(f, "Fetching error: {}", s), Error::Fetching(s) => write!(f, "Fetching error: {}", s),
Error::Converting(s) => write!(f, "Converting error: {}", s), Error::Converting(s) => write!(f, "Converting error: {}", s),
Error::NoNextPage => write!(f, "No next page available"),
Error::NoTagsFound => write!(f, "Given Repo has 0 tags. Is it valid?"), Error::NoTagsFound => write!(f, "Given Repo has 0 tags. Is it valid?"),
} }
} }
@ -98,38 +119,17 @@ impl Tags {
} }
/// returns tags of next page /// returns tags of next page
pub fn next_page(&self) -> Result<Self, Error> { pub fn next_page(&self) -> Option<Self> {
match &self.next_page { match &self.next_page {
Some(url) => Self::with_url(url), Some(url) => match Self::with_url(url) {
None => Err(Error::NoNextPage), Ok(tags) => Some(tags),
Err(_) => None,
},
None => None,
} }
} }
} }
impl fmt::Display for Images {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
//architecture infos
let mut arch = String::new();
for image in self.images.iter().take(1) {
arch.push_str(&format!("{}", image));
}
for image in self.images.iter().skip(1) {
arch.push_str(&format!(", {}", image));
}
let now = chrono::Utc::now();
let rfc3339 = DateTime::parse_from_rfc3339(&self.last_updated).unwrap();
let dif = now - rfc3339.with_timezone(&chrono::Utc);
write!(
f,
"{} vor {} [{}]",
self.tag_name,
format_time_nice(dif),
arch
)
}
}
/// converts a given duration to a readable string /// converts a given duration to a readable string
fn format_time_nice(time: chrono::Duration) -> String { fn format_time_nice(time: chrono::Duration) -> String {
if time.num_weeks() == 52 { if time.num_weeks() == 52 {

View File

@ -122,6 +122,7 @@ impl Ui {
State::SelectTag => { State::SelectTag => {
let mut repo = ui.repo.get(); let mut repo = ui.repo.get();
let tag = match ui.tags.get_selected() { let tag = match ui.tags.get_selected() {
Err(tag_list::Error::NextPageSelected) => continue,
Err(e) => { Err(e) => {
ui.info.set_info(&format!("{}", e)); ui.info.set_info(&format!("{}", e));
continue; continue;

View File

@ -68,8 +68,8 @@ impl TagList {
.collect(); .collect();
match tags.next_page() { match tags.next_page() {
Err(_) => (), None => (),
Ok(new_tags) => { Some(new_tags) => {
lines.push(Line::NextPage(String::from("load more tags"))); lines.push(Line::NextPage(String::from("load more tags")));
tags = new_tags; tags = new_tags;
} }
@ -149,8 +149,8 @@ impl TagList {
fn load_next_page(&mut self) { fn load_next_page(&mut self) {
match &self.tags { match &self.tags {
Some(tags) => match tags.next_page() { Some(tags) => match tags.next_page() {
Err(_) => (), None => (),
Ok(new_tags) => { Some(new_tags) => {
//load new tags object //load new tags object
self.tags = Some(new_tags); self.tags = Some(new_tags);