implmeent macro for expiration from str
This commit is contained in:
parent
d6b2d53249
commit
0eb8adc20e
1 changed files with 33 additions and 13 deletions
|
@ -189,21 +189,41 @@ pub enum Expiration {
|
||||||
UnixTime(DateTime<Utc>),
|
UnixTime(DateTime<Utc>),
|
||||||
}
|
}
|
||||||
|
|
||||||
// This impl is used for the CLI
|
// This impl is used for the CLI. We use a macro here to ensure that possible
|
||||||
impl FromStr for Expiration {
|
// expressed by the CLI are the same supported by the server.
|
||||||
type Err = String;
|
macro_rules! expiration_from_str {
|
||||||
|
{
|
||||||
|
$($str_repr:literal => $duration:expr),* $(,)?
|
||||||
|
} => {
|
||||||
|
impl FromStr for Expiration {
|
||||||
|
type Err = String;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
match s {
|
match s {
|
||||||
"read" => Ok(Self::BurnAfterReading),
|
$($str_repr => Ok($duration),)*
|
||||||
"5m" => Ok(Self::UnixTime(Utc::now() + Duration::minutes(5))),
|
_ => Err(s.to_owned()),
|
||||||
"10m" => Ok(Self::UnixTime(Utc::now() + Duration::minutes(10))),
|
}
|
||||||
"1h" => Ok(Self::UnixTime(Utc::now() + Duration::hours(1))),
|
}
|
||||||
"1d" => Ok(Self::UnixTime(Utc::now() + Duration::days(1))),
|
|
||||||
// We disallow permanent pastes
|
|
||||||
_ => Err(s.to_owned()),
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
impl Expiration {
|
||||||
|
pub fn variants() -> &'static [&'static str] {
|
||||||
|
&[
|
||||||
|
$($str_repr,)*
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
expiration_from_str! {
|
||||||
|
"read" => Self::BurnAfterReading,
|
||||||
|
"5m" => Self::UnixTime(Utc::now() + Duration::minutes(5)),
|
||||||
|
"10m" => Self::UnixTime(Utc::now() + Duration::minutes(10)),
|
||||||
|
"1h" => Self::UnixTime(Utc::now() + Duration::hours(1)),
|
||||||
|
"1d" => Self::UnixTime(Utc::now() + Duration::days(1)),
|
||||||
|
"3d" => Self::UnixTime(Utc::now() + Duration::days(1)),
|
||||||
|
"1w" => Self::UnixTime(Utc::now() + Duration::weeks(1)),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Expiration {
|
impl Display for Expiration {
|
||||||
|
|
Loading…
Reference in a new issue