Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 13, 2022
1 parent f84e8f5 commit bd64387
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 48 deletions.
27 changes: 27 additions & 0 deletions git-date/src/time/format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::Time;
use time::format_description::FormatItem;
use time::formatting::Formattable;
use time::macros::format_description;

/// E.g. `2018-12-24`
pub const SHORT: &[FormatItem<'_>] = format_description!("[year]-[month]-[day]");

/// Formatting
impl Time {
/// Format this instance according to the given `format`.
///
/// Use the [`format_description`] macro to create and validate formats at compile time, courtesy of the [`time`] crate.
pub fn format(&self, format: &(impl Formattable + ?Sized)) -> String {
self.to_time()
.format(&format)
.expect("well-known format into memory never fails")
}
}

impl Time {
fn to_time(&self) -> time::OffsetDateTime {
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"))
}
}
2 changes: 2 additions & 0 deletions git-date/src/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub enum Sign {
Minus,
}

///
pub mod format;
mod init;
mod write;

Expand Down
26 changes: 0 additions & 26 deletions git-date/src/time/write.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use crate::time::Sign;
use crate::Time;
use bstr::BString;
use time::format_description::FormatItem;
use time::formatting::Formattable;
use time::macros::format_description;

const DEFAULT_FORMAT: &[FormatItem<'_>] = format_description!("[year]-[month]-[day]");

/// Serialization with standard `git` format
impl Time {
Expand Down Expand Up @@ -72,24 +67,3 @@ impl Time {
}) + 2 /*space + sign*/ + 2 /*hours*/ + 2 /*minutes*/
}
}

/// Formatting
impl Time {
/// Format this instance as string using the default format -- `[year]-[month]-[day]`.
pub fn format(&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(&DEFAULT_FORMAT)
.expect("well-known format into memory never fails")
}

/// Format this instance as string using the format.
pub fn to_formatted_string(&self, format: &(impl Formattable + ?Sized)) -> 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(&format)
.expect("well-known format into memory never fails")
}
}
50 changes: 29 additions & 21 deletions git-date/tests/time/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use bstr::ByteSlice;
use git_date::{time::Sign, Time};
use time::macros::format_description;

#[test]
fn is_set() {
Expand Down Expand Up @@ -47,25 +46,34 @@ fn write_to() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

#[test]
fn format() {
let time = Time {
seconds_since_unix_epoch: 500,
offset_in_seconds: 9000,
sign: Sign::Plus,
};
assert_eq!("1970-01-01".to_string(), time.format());
}
mod format {
use git_date::time::Sign;
use git_date::Time;
use time::macros::format_description;

#[test]
fn to_formatted_string() {
let time = Time {
seconds_since_unix_epoch: 500,
offset_in_seconds: 9000,
sign: Sign::Plus,
};
assert_eq!(
"1970-01-01 00:08:20".to_string(),
time.to_formatted_string(&format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"))
);
#[test]
fn year_month_day() {
assert_eq!(time().format(git_date::time::format::SHORT), "1970-01-01");
}

#[test]
fn custom_string() {
assert_eq!(time().format("[year]-[month]-[day]"), "1970-01-01");
}

#[test]
fn custom_compile_time() {
assert_eq!(
time().format(&format_description!("[year]-[month]-[day] [hour]:[minute]:[second]")),
"1970-01-01 00:08:20",
);
}

fn time() -> Time {
Time {
seconds_since_unix_epoch: 500,
offset_in_seconds: 9000,
sign: Sign::Plus,
}
}
}
4 changes: 3 additions & 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,9 @@ 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.format(), title),
CandidateInfo::Commit { date, title } => {
write!(f, "commit {} {:?}", date.format(git_date::time::format::SHORT), title)
}
}
}
}
Expand Down

0 comments on commit bd64387

Please sign in to comment.