From c5a5ccd47351f4536d2a2c3cb4d93c71545d0b14 Mon Sep 17 00:00:00 2001 From: Sebastian Tobie Date: Tue, 3 Dec 2024 09:50:28 +0100 Subject: [PATCH] updated include_example to reflect the include_data macro and removed dead code --- macros/src/lib.rs | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 54f98ba..a085a6b 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -49,24 +49,6 @@ fn get_text(item: T) -> Result { } } -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 { fn parse(input: ParseStream) -> Result { let mut data = IncludeData::default(); @@ -138,7 +120,7 @@ pub fn include_data(data: TokenStream) -> TokenStream { } } else { comment = format!( - "Failed to get the year({:?}) or day({:?})", + "Failed to get the year({:?}) or day({:?}) falling back to default", input.year, input.day ); } @@ -149,22 +131,36 @@ pub fn include_data(data: TokenStream) -> TokenStream { .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] pub fn include_example(data: TokenStream) -> TokenStream { - let input = parse::(data).unwrap_or_default(); + let input = match parse::(data) { + Ok(include) => include, + Err(error) => return error.into_compile_error().into(), + }; let ident = format_ident!("{}", input.var); + let mut comment: String; if input.day.is_some() && input.year.is_some() { + comment = "Data from the Example dir".into(); match canonicalize(EXAMPLE_DIR, input.clone()) { Some(p) => { return quote! { + #[doc = #comment] static #ident: &str = include_str!(#p); } .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! { + #[doc = #comment] static #ident: &str = ""; } .into()