updated include_example to reflect the include_data macro and removed dead code

Dieser Commit ist enthalten in:
Sebastian Tobie 2024-12-03 09:50:28 +01:00
Ursprung b626b224b4
Commit c5a5ccd473

Datei anzeigen

@ -49,24 +49,6 @@ fn get_text<T: Spanned>(item: T) -> Result<String> {
} }
} }
fn get_literal<'a, 'b>(cursor: StepCursor<'a, 'b>) -> Result<(String, Cursor<'a>)> {
let (lit, c) = match cursor.literal() {
Some(literal) => literal,
None => return Err(Error::new(cursor.span(), "Failed to get literal")),
};
let text = get_text(lit)?;
Ok((text, c))
}
fn get_ident<'a, 'b>(cursor: StepCursor<'a, 'b>) -> Result<(String, Cursor<'a>)> {
let (lit, c) = match cursor.ident() {
Some(literal) => literal,
None => return Err(Error::new(cursor.span(), "Failed to get literal")),
};
let text = get_text(lit)?;
Ok((text, c))
}
impl Parse for IncludeData { impl Parse for IncludeData {
fn parse(input: ParseStream) -> Result<Self> { fn parse(input: ParseStream) -> Result<Self> {
let mut data = IncludeData::default(); let mut data = IncludeData::default();
@ -138,7 +120,7 @@ pub fn include_data(data: TokenStream) -> TokenStream {
} }
} else { } else {
comment = format!( comment = format!(
"Failed to get the year({:?}) or day({:?})", "Failed to get the year({:?}) or day({:?}) falling back to default",
input.year, input.day input.year, input.day
); );
} }
@ -149,22 +131,36 @@ pub fn include_data(data: TokenStream) -> TokenStream {
.into() .into()
} }
/// Same as include_data!, but it only gets data from the example directory.
/// This is useful for testing the examples, even when Data from the AOC is available
#[proc_macro] #[proc_macro]
pub fn include_example(data: TokenStream) -> TokenStream { pub fn include_example(data: TokenStream) -> TokenStream {
let input = parse::<IncludeData>(data).unwrap_or_default(); let input = match parse::<IncludeData>(data) {
Ok(include) => include,
Err(error) => return error.into_compile_error().into(),
};
let ident = format_ident!("{}", input.var); let ident = format_ident!("{}", input.var);
let mut comment: String;
if input.day.is_some() && input.year.is_some() { if input.day.is_some() && input.year.is_some() {
comment = "Data from the Example dir".into();
match canonicalize(EXAMPLE_DIR, input.clone()) { match canonicalize(EXAMPLE_DIR, input.clone()) {
Some(p) => { Some(p) => {
return quote! { return quote! {
#[doc = #comment]
static #ident: &str = include_str!(#p); static #ident: &str = include_str!(#p);
} }
.into() .into()
} }
None => {} None => comment = "failed to get data from the path".into(),
} }
} else {
comment = format!(
"Failed to get the year({:?}) or day({:?}) falling back to default",
input.year, input.day
);
} }
quote! { quote! {
#[doc = #comment]
static #ident: &str = ""; static #ident: &str = "";
} }
.into() .into()