Skip to content

Commit

Permalink
Simplify code generation further
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Jul 17, 2016
1 parent dbf4810 commit 2dc7f41
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 52 deletions.
77 changes: 41 additions & 36 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
use std::{env, fs, path};
use std::{env, fmt, fs, path};
use std::io::Write;

fn main() {
let path = path::Path::new(&env::var("OUT_DIR").unwrap()).join("generated_data.rs");
let mut f = fs::File::create(&path).unwrap();
macro_rules! w {
($($tt: tt)*) => {{ writeln!(f, $($tt)*).unwrap(); }}
}

// The total number of days in the year up to the current month, inclusive,
// for common (non-leap) years and leap years.
let mut running_sum_common = 0;
let mut running_sum_leap = 0;

w!("macro_rules! with_month_data {{");
w!(" ($macro_name: ident) => {{");
w!(" $macro_name! {{");
for (i, &(name, length_common, length_leap)) in [
let month_data = [
// Name of the month with its length (number of days) in common years and leap years.
("January", 31, 31),
("February", 28, 29),
("March", 31, 31),
Expand All @@ -27,39 +20,51 @@ fn main() {
("October", 31, 31),
("November", 30, 30),
("December", 31, 31),
].iter().enumerate() {
w!("{} {{", name);
w!(" number = {},", i + 1); // i starts at 0
w!(" common_years = {{ first_day = {}, last_day = {}, }},",
running_sum_common,
running_sum_common + length_common - 1);
w!(" leap_years = {{ first_day = {}, last_day = {}, }},",
running_sum_leap,
running_sum_leap + length_leap - 1);
w!("}},");
].iter().enumerate().map(|(i, &(name, length_common, length_leap))| {
running_sum_common += length_common;
running_sum_leap += length_leap;
}
w!(" }}");
w!(" }}");
w!("}}");
(
Ident(name),
/* number = */ i + 1, // i starts at 0
/* first_day_in_common_years = */ running_sum_common - length_common,
/* last_day_in_common_years = */ running_sum_common - 1,
/* first_day_in_leap_years = */ running_sum_leap - length_leap,
/* last_day_in_leap_years = */ running_sum_leap - 1,
)
}).collect::<Vec<_>>();

w!("");
w!("macro_rules! with_day_of_the_week_data {{");
w!(" ($macro_name: ident) => {{");
w!(" $macro_name! {{");
for (i, name) in [
let day_of_the_week_data = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
].iter().enumerate() {
w!("{} = {},", name, i + 1); // i starts at 0
].iter().enumerate().map(|(i, &name)| (Ident(name), i + 1)).collect::<Vec<_>>();

let path = path::Path::new(&env::var("OUT_DIR").unwrap()).join("generated_data.rs");
let mut file = fs::File::create(&path).unwrap();

macro_rules! with {
($variable: ident) => {
writeln!(
file,
"macro_rules! with_{} {{ ($macro_name: ident) => {{ $macro_name!({:?}); }} }}",
stringify!($variable),
$variable
).unwrap()
}
}
with!(month_data);
with!(day_of_the_week_data);
}

/// Wrap a string to format without quotes.
struct Ident(&'static str);

impl fmt::Debug for Ident {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
w!(" }}");
w!(" }}");
w!("}}");
}
28 changes: 12 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,14 @@ impl From<i32> for YearKind {
}

macro_rules! declare_month {
(
$(
$name: ident {
number = $number: expr,
common_years = {
first_day = $first_day_in_common_years: expr,
last_day = $last_day_in_common_years: expr,
},
leap_years = {
first_day = $first_day_in_leap_years: expr,
last_day = $last_day_in_leap_years: expr,
},
},
)+
) => {
([ $((
$name: ident,
$number: expr,
$first_day_in_common_years: expr,
$last_day_in_common_years: expr,
$first_day_in_leap_years: expr,
$last_day_in_leap_years: expr
)),+ ]) => {
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum Month {
$(
Expand Down Expand Up @@ -224,7 +217,10 @@ macro_rules! declare_month {
}

macro_rules! declare_day_of_the_week {
( $( $name: ident = $number: expr, )+ ) => {
([ $((
$name: ident,
$number: expr
)),+ ]) => {
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum DayOfTheWeek {
$(
Expand Down

0 comments on commit 2dc7f41

Please sign in to comment.