From 19353fc700ef11fb77dd86c84f0730ad4abab15f Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 25 Apr 2024 15:22:23 +0200 Subject: [PATCH] git: Split parsing of `Time` from `Author` This allows us to parse time values on their own. Signed-off-by: Fintan Halpenny X-Clacks-Overhead: GNU Terry Pratchett Signed-off-by: Fintan Halpenny X-Clacks-Overhead: GNU Terry Pratchett --- radicle-git-ext/src/author.rs | 69 ++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/radicle-git-ext/src/author.rs b/radicle-git-ext/src/author.rs index 7a38310..0faa7ba 100644 --- a/radicle-git-ext/src/author.rs +++ b/radicle-git-ext/src/author.rs @@ -43,6 +43,38 @@ impl Time { pub fn offset(&self) -> i32 { self.offset } + + fn from_components<'a>(cs: &mut impl Iterator) -> Result { + let offset = match cs.next() { + None => Err(ParseError::Missing("offset")), + Some(offset) => Self::parse_offset(offset).map_err(ParseError::Offset), + }?; + let time = match cs.next() { + None => return Err(ParseError::Missing("time")), + Some(time) => time.parse::().map_err(ParseError::Time)?, + }; + Ok(Self::new(time, offset)) + } + + fn parse_offset(offset: &str) -> Result { + // The offset is in the form of timezone offset, + // e.g. +0200, -0100. This needs to be converted into + // minutes. The first two digits in the offset are the + // number of hours in the offset, while the latter two + // digits are the number of minutes in the offset. + let tz_offset = offset.parse::()?; + let hours = tz_offset / 100; + let minutes = tz_offset % 100; + Ok(hours * 60 + minutes) + } +} + +impl FromStr for Time { + type Err = ParseError; + + fn from_str(s: &str) -> Result { + Self::from_components(&mut s.split(' ').rev()) + } } impl From