[rs] Use macros to specify days
This commit is contained in:
parent
ff0ebe393b
commit
07f272073a
1 changed files with 24 additions and 20 deletions
|
|
@ -6,8 +6,28 @@ use std::{fmt, fs};
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
enum Day {
|
macro_rules! days {
|
||||||
Y2022D01,
|
( $( $day:ident : $name:expr, )* ) => {
|
||||||
|
enum Day { $( $day, )* }
|
||||||
|
|
||||||
|
impl fmt::Display for Day{
|
||||||
|
fn fmt(&self, f:&mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
$( Self::$day => $name, )*
|
||||||
|
}.fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromStr for Day {
|
||||||
|
type Err = ();
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
Ok(match s {
|
||||||
|
$( $name => Self::$day, )*
|
||||||
|
_ => return Err(()),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Day {
|
impl Day {
|
||||||
|
|
@ -16,24 +36,8 @@ impl Day {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Day {
|
days! {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
Y2022D01: "2022_01",
|
||||||
match self {
|
|
||||||
Day::Y2022D01 => "2022_01",
|
|
||||||
}
|
|
||||||
.fmt(f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for Day {
|
|
||||||
type Err = ();
|
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
Ok(match s {
|
|
||||||
"2022_01" => Self::Y2022D01,
|
|
||||||
_ => return Err(()),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue