| |
}
|
| |
}
|
| |
|
| + |
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)
|
| |
|
| |
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"))
|
| |
}
|
| |
}
|