spooky_season/
lib.rs

1#![feature(proc_macro_span)]
2
3use std::env;
4use std::fs;
5use std::str::FromStr;
6use std::os::unix::fs::MetadataExt;
7
8use time::{OffsetDateTime, Month};
9use proc_macro::Span;
10
11#[proc_macro]
12pub fn spooky(code: proc_macro::TokenStream) -> proc_macro::TokenStream {
13    match real_spooky(code) {
14        Ok(code) => code,
15        Err(err) => panic!("{}", err)
16    }
17}
18
19fn real_spooky(code: proc_macro::TokenStream) -> Result<proc_macro::TokenStream, &'static str> {
20    let here = Span::call_site();
21    let this_file = here.source_file();
22    let path = this_file.path();
23    let cwd = env::current_dir().map_err(|_| "👻 where am i? 👻")?;
24    let full_path = cwd.join(path);
25
26    let metadata = fs::metadata(full_path).map_err(|_| "👻 that wasn't candy 👻")?;
27    let mtime = OffsetDateTime::from_unix_timestamp(metadata.mtime()).map_err(|_| "🙀 not spooky enough 😿")?;
28    let month = mtime.month();
29
30    if month == Month::October {
31        let code = code.to_string();
32        let code = format!("unsafe {{ {} }}", code);
33        Ok(proc_macro::TokenStream::from_str(&code).map_err(|_| "🎃 trick! 🎃")?)
34    } else {
35        Err("🙀 it's not the spooky season yet. 😿")
36    }
37}