From d608fe6b5082150e0fffb48d612e711830b019ff Mon Sep 17 00:00:00 2001 From: Thomas Eppers Date: Wed, 27 Oct 2021 13:40:55 +0200 Subject: [PATCH] changed function next_page from Result to Option --- src/tags.rs | 59 +++++++++++++++++++++--------------------- src/widget/tag_list.rs | 8 +++--- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/tags.rs b/src/tags.rs index f872dc3..fb5a40f 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -24,6 +24,30 @@ pub struct Images { 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)] pub struct Tags { count: usize, @@ -40,7 +64,6 @@ pub enum Error { Converting(String), /// invalid repos show a valid json with 0 tags NoTagsFound, - NoNextPage, } impl fmt::Display for Error { @@ -48,7 +71,6 @@ impl fmt::Display for Error { match self { Error::Fetching(s) => write!(f, "Fetching 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?"), } } @@ -97,38 +119,17 @@ impl Tags { } /// returns tags of next page - pub fn next_page(&self) -> Result { + pub fn next_page(&self) -> Option { match &self.next_page { - Some(url) => Self::with_url(url), - None => Err(Error::NoNextPage), + Some(url) => match Self::with_url(url) { + 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 fn format_time_nice(time: chrono::Duration) -> String { if time.num_weeks() == 52 { diff --git a/src/widget/tag_list.rs b/src/widget/tag_list.rs index b14e936..656fd04 100644 --- a/src/widget/tag_list.rs +++ b/src/widget/tag_list.rs @@ -68,8 +68,8 @@ impl TagList { .collect(); match tags.next_page() { - Err(_) => (), - Ok(new_tags) => { + None => (), + Some(new_tags) => { lines.push(Line::NextPage(String::from("load more tags"))); tags = new_tags; } @@ -149,8 +149,8 @@ impl TagList { fn load_next_page(&mut self) { match &self.tags { Some(tags) => match tags.next_page() { - Err(_) => (), - Ok(new_tags) => { + None => (), + Some(new_tags) => { //load new tags object self.tags = Some(new_tags);