-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#414: testgen: add time command; make timestamps abstract
- Loading branch information
1 parent
da11fc1
commit 8ff0f93
Showing
7 changed files
with
97 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use crate::{helpers::*, Generator}; | ||
use gumdrop::Options; | ||
use serde::Deserialize; | ||
use simple_error::*; | ||
|
||
#[derive(Debug, Options, Deserialize, Clone)] | ||
pub struct Time { | ||
#[options(help = "seconds passed since UNIX EPOCH (required; can be passed via STDIN)")] | ||
pub secs: Option<u64>, | ||
} | ||
|
||
impl Time { | ||
pub fn new(secs: u64) -> Self { | ||
Time { | ||
secs: Some(secs), | ||
} | ||
} | ||
set_option!(secs, u64); | ||
} | ||
|
||
impl std::str::FromStr for Time { | ||
type Err = SimpleError; | ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
let time = match parse_as::<Time>(s) { | ||
Ok(input) => input, | ||
Err(_) => Time::new(try_with!(u64::from_str(s), "failed to parse time")), | ||
}; | ||
Ok(time) | ||
} | ||
} | ||
|
||
impl Generator<tendermint::Time> for Time { | ||
fn merge_with_default(self, default: Self) -> Self { | ||
Time { | ||
secs: self.secs.or(default.secs), | ||
} | ||
} | ||
|
||
fn generate(&self) -> Result<tendermint::Time, SimpleError> { | ||
let time = match &self.secs { | ||
None => bail!("time is missing"), | ||
Some(secs) => *secs, | ||
}; | ||
Ok(get_time(time)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_time() { | ||
|
||
let time = Time::new(0); | ||
assert_eq!(time.generate().unwrap(), tendermint::Time::unix_epoch()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters