Skip to content

Commit

Permalink
WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
svetli-n committed Sep 11, 2022
1 parent 70fe59f commit 79d82d4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
32 changes: 17 additions & 15 deletions git-date/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,29 @@ mod relative {
}

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

fn duration(period: &str, multiplier: i64) -> Option<Duration> {
let period = period.strip_suffix('s').unwrap_or(period);
Some(match period {
"second" => Duration::seconds(multiplier),
"minute" => Duration::minutes(multiplier),
"hour" => Duration::hours(multiplier),
"day" => Duration::days(multiplier),
"week" => Duration::weeks(multiplier),
// TODO months & years
let seconds: i64 = match period {
"second" => 1,
"minute" => 60,
"hour" => 3_600,
"day" => 86_400,
"week" => 604_800,
// TODO months & years?
_ => return None,
})
};
Some(Duration::seconds(seconds.checked_mul(multiplier)?))
}

#[cfg(test)]
Expand Down
Binary file not shown.
1 change: 0 additions & 1 deletion git-date/tests/time/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ mod relative {
use time::{Duration, OffsetDateTime};

#[test]
#[should_panic] // TODO: fix
fn large_offsets_can_panic() {
git_date::parse("999999999999999 weeks ago", Some(std::time::UNIX_EPOCH)).ok();
}
Expand Down
4 changes: 2 additions & 2 deletions git-prompt/src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ pub(crate) mod imp {
};

use nix::sys::{termios, termios::Termios};
use parking_lot::{lock_api::MutexGuard, Mutex, RawMutex};
use parking_lot::{const_mutex, lock_api::MutexGuard, Mutex, RawMutex};

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

static TERM_STATE: Mutex<Option<Termios>> = Mutex::new(None);
static TERM_STATE: Mutex<Option<Termios>> = const_mutex(None);

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

0 comments on commit 79d82d4

Please # to comment.