Skip to content

Commit 79d82d4

Browse files
committed
WIP.
1 parent 70fe59f commit 79d82d4

File tree

4 files changed

+19
-18
lines changed

4 files changed

+19
-18
lines changed

git-date/src/parse.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,27 +103,29 @@ mod relative {
103103
}
104104

105105
pub(crate) fn parse(input: &str, now: Option<SystemTime>) -> Option<Result<OffsetDateTime, Error>> {
106-
parse_inner(input).map(|offset| {
107-
let offset = std::time::Duration::from_secs(offset.whole_seconds().try_into().expect("positive value"));
108-
now.ok_or(Error::MissingCurrentTime).map(|now| {
109-
now.checked_sub(offset)
110-
.expect("BUG: values can't be large enough to cause underflow")
111-
.into()
112-
})
106+
let offset = parse_inner(input).map(|offset| {
107+
let secs = offset.whole_seconds().try_into().expect("positive value");
108+
return std::time::Duration::from_secs(secs);
109+
})?;
110+
now.ok_or(Error::MissingCurrentTime).map(|now| {
111+
now.checked_sub(offset)
112+
.expect("BUG: values can't be large enough to cause underflow")
113+
.into()
113114
})
114115
}
115116

116117
fn duration(period: &str, multiplier: i64) -> Option<Duration> {
117118
let period = period.strip_suffix('s').unwrap_or(period);
118-
Some(match period {
119-
"second" => Duration::seconds(multiplier),
120-
"minute" => Duration::minutes(multiplier),
121-
"hour" => Duration::hours(multiplier),
122-
"day" => Duration::days(multiplier),
123-
"week" => Duration::weeks(multiplier),
124-
// TODO months & years
119+
let seconds: i64 = match period {
120+
"second" => 1,
121+
"minute" => 60,
122+
"hour" => 3_600,
123+
"day" => 86_400,
124+
"week" => 604_800,
125+
// TODO months & years?
125126
_ => return None,
126-
})
127+
};
128+
Some(Duration::seconds(seconds.checked_mul(multiplier)?))
127129
}
128130

129131
#[cfg(test)]
Binary file not shown.

git-date/tests/time/parse.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ mod relative {
9999
use time::{Duration, OffsetDateTime};
100100

101101
#[test]
102-
#[should_panic] // TODO: fix
103102
fn large_offsets_can_panic() {
104103
git_date::parse("999999999999999 weeks ago", Some(std::time::UNIX_EPOCH)).ok();
105104
}

git-prompt/src/unix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ pub(crate) mod imp {
99
};
1010

1111
use nix::sys::{termios, termios::Termios};
12-
use parking_lot::{lock_api::MutexGuard, Mutex, RawMutex};
12+
use parking_lot::{const_mutex, lock_api::MutexGuard, Mutex, RawMutex};
1313

1414
use crate::{unix::TTY_PATH, Error, Mode, Options};
1515

16-
static TERM_STATE: Mutex<Option<Termios>> = Mutex::new(None);
16+
static TERM_STATE: Mutex<Option<Termios>> = const_mutex(None);
1717

1818
/// Ask the user given a `prompt`, returning the result.
1919
pub(crate) fn ask(prompt: &str, Options { mode, .. }: &Options<'_>) -> Result<String, Error> {

0 commit comments

Comments
 (0)