Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
- no need to force Strings into BStrings. We actually prefer String if
  we know it's valid UTF-8.
- Let's avoid exposing anything from the `time` crate directly. Instead
  consider adding support for a format string.
  • Loading branch information
Byron committed Aug 8, 2022
1 parent 5bbcbcd commit 556dd8c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
35 changes: 17 additions & 18 deletions git-date/src/time/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,14 @@ use crate::time::Sign;
use crate::Time;
use bstr::BString;
use time::format_description::well_known::Iso8601;
use time::formatting::Formattable;

/// Serialization with standard `git` format
impl Time {
/// Serialize this instance into a Iso8601 BString.
/// Serialize this instance into memory, similar to what [`write_to()`][Self::write_to()] would do with arbitrary `Write` implementations.
pub fn to_bstring(&self) -> BString {
time::OffsetDateTime::from_unix_timestamp(self.seconds_since_unix_epoch as i64)
.expect("always valid unix time")
.replace_offset(time::UtcOffset::from_whole_seconds(self.offset_in_seconds).expect("valid offset"))
.format(&Iso8601::DEFAULT)
.unwrap()
.into()
}

/// Serialize this instance into a BString, formatting it using the provided `time::format_description`
pub fn to_bstring_with_formatter(&self, formatter: &(impl Formattable + ?Sized)) -> BString {
time::OffsetDateTime::from_unix_timestamp(self.seconds_since_unix_epoch as i64)
.expect("always valid unix time")
.replace_offset(time::UtcOffset::from_whole_seconds(self.offset_in_seconds).expect("valid offset"))
.format(formatter)
.unwrap()
.into()
let mut buf = Vec::with_capacity(64);
self.write_to(&mut buf).expect("write to memory cannot fail");
buf.into()
}

/// Serialize this instance to `out` in a format suitable for use in header fields of serialized git commits or tags.
Expand Down Expand Up @@ -81,3 +68,15 @@ impl Time {
}) + 2 /*space + sign*/ + 2 /*hours*/ + 2 /*minutes*/
}
}

/// Formatting
impl Time {
/// Format this instance as Iso8601 string.
pub fn to_iso_8601(&self) -> String {
time::OffsetDateTime::from_unix_timestamp(self.seconds_since_unix_epoch as i64)
.expect("always valid unix time")
.replace_offset(time::UtcOffset::from_whole_seconds(self.offset_in_seconds).expect("valid offset"))
.format(&Iso8601::DEFAULT)
.expect("well-known format into memory never fails")
}
}
2 changes: 1 addition & 1 deletion git-repository/src/revision/spec/parse/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl std::fmt::Display for CandidateInfo {
CandidateInfo::FindError { source } => write!(f, "lookup error: {}", source),
CandidateInfo::Tag { name } => write!(f, "tag {:?}", name),
CandidateInfo::Object { kind } => std::fmt::Display::fmt(kind, f),
CandidateInfo::Commit { date, title } => write!(f, "commit {} {:?}", date.to_bstring(), title),
CandidateInfo::Commit { date, title } => write!(f, "commit {} {:?}", date.to_iso_8601(), title),
}
}
}
Expand Down

0 comments on commit 556dd8c

Please sign in to comment.