[rs] Use macros to specify days

This commit is contained in:
Joscha 2022-12-02 12:56:27 +01:00
parent ff0ebe393b
commit 07f272073a

View file

@ -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)]