Radish alpha
r
rad:z6cFWeWpnZNHh9rUW8phgA3b5yGt
Git libraries for Radicle
Radicle
Git
git: Split parsing of `Time` from `Author`
Merged did:key:z6MksFqX...wzpT opened 2 years ago

This allows us to parse time values on their own.

1 file changed +40 -28 1525ef65 19353fc7
modified radicle-git-ext/src/author.rs
@@ -45,6 +45,33 @@ impl Time {
    }
}

+
impl FromStr for Time {
+
    type Err = ParseError;
+

+
    fn from_str(s: &str) -> Result<Self, Self::Err> {
+
        let mut components = s.split(' ');
+
        let offset = match components.next_back() {
+
            None => return Err(ParseError::Missing("offset")),
+
            Some(offset) => {
+
                // 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::<i32>().map_err(ParseError::Offset)?;
+
                let hours = tz_offset / 100;
+
                let minutes = tz_offset % 100;
+
                hours * 60 + minutes
+
            }
+
        };
+
        let time = match components.next_back() {
+
            None => return Err(ParseError::Missing("time")),
+
            Some(time) => time.parse::<i64>().map_err(ParseError::Time)?,
+
        };
+
        Ok(Time::new(time, offset))
+
    }
+
}
+

impl From<Time> for git2::Time {
    fn from(t: Time) -> Self {
        Self::new(t.seconds, t.offset)
@@ -101,8 +128,6 @@ pub enum ParseError {
    Offset(#[source] ParseIntError),
    #[error("time was incorrect format while parsing person signature")]
    Time(#[source] ParseIntError),
-
    #[error("time offset is expected to be '+'/'-' for a person siganture")]
-
    UnknownOffset,
}

impl FromStr for Author {
@@ -110,32 +135,19 @@ impl FromStr for Author {

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut components = s.split(' ');
-
        let offset = match components.next_back() {
-
            None => return Err(ParseError::Missing("offset")),
-
            Some(offset) => {
-
                // 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::<i32>().map_err(ParseError::Offset)?;
-
                let hours = tz_offset / 100;
-
                let minutes = tz_offset % 100;
-
                hours * 60 + minutes
+
        let mut name = Vec::new();
+

+
        while let Some(next) = components.next() {
+
            if let Some(email) = next.strip_prefix('<').and_then(|n| n.strip_suffix('>')) {
+
                let time = Time::from_str(components.collect::<Vec<_>>().join(" ").as_str())?;
+
                return Ok(Self {
+
                    name: name.join(" "),
+
                    email: email.to_owned(),
+
                    time,
+
                });
            }
-
        };
-
        let time = match components.next_back() {
-
            None => return Err(ParseError::Missing("time")),
-
            Some(time) => time.parse::<i64>().map_err(ParseError::Time)?,
-
        };
-
        let time = Time::new(time, offset);
-

-
        let email = components
-
            .next_back()
-
            .ok_or(ParseError::Missing("email"))?
-
            .trim_matches(|c| c == '<' || c == '>')
-
            .to_owned();
-
        let name = components.collect::<Vec<_>>().join(" ");
-
        Ok(Self { name, email, time })
+
            name.push(next);
+
        }
+
        Err(ParseError::Missing("email"))
    }
}