diff --git a/agb-image-converter/Cargo.lock b/agb-image-converter/Cargo.lock index fb990437..a0dfd7c4 100644 --- a/agb-image-converter/Cargo.lock +++ b/agb-image-converter/Cargo.lock @@ -13,6 +13,8 @@ name = "agb_image_converter" version = "0.4.0" dependencies = [ "image", + "proc-macro2", + "quote", "serde", "syn", "toml", diff --git a/agb-image-converter/Cargo.toml b/agb-image-converter/Cargo.toml index 95326b63..8add75bb 100644 --- a/agb-image-converter/Cargo.toml +++ b/agb-image-converter/Cargo.toml @@ -13,4 +13,6 @@ proc-macro = true image = { version = "0.23.14", default-features = false, features = [ "png", "bmp" ] } toml = "0.5.8" serde = { version = "1.0", features = ["derive"] } -syn = "1.0.73" \ No newline at end of file +syn = "1.0.73" +proc-macro2 = "1.0.27" +quote = "1.0.9" \ No newline at end of file diff --git a/agb-image-converter/src/lib.rs b/agb-image-converter/src/lib.rs index cad79bae..f7697b31 100644 --- a/agb-image-converter/src/lib.rs +++ b/agb-image-converter/src/lib.rs @@ -1,9 +1,10 @@ use proc_macro::TokenStream; use syn::parse_macro_input; -use std::fmt::Write; use std::path::Path; +use quote::{quote, format_ident}; + mod colour; mod config; mod image_loader; @@ -43,30 +44,23 @@ pub fn include_gfx(input: TokenStream) -> TokenStream { let config = config::parse(&path.to_string_lossy()); - let module_name = path.file_stem().expect("Expected a file stem"); + let module_name = format_ident!("{}", path.file_stem().expect("Expected a file stem").to_string_lossy()); + let include_path = path.to_string_lossy(); - let mut output = String::new(); + let images = config.images(); + let image_code = images + .iter() + .map(|(image_name, &image)| convert_image(image, parent, &image_name, &config.crate_prefix()).parse::().unwrap()); - writeln!(&mut output, "mod {} {{", module_name.to_string_lossy()).unwrap(); - writeln!( - &mut output, - "const _: &[u8] = include_bytes!(\"{}\");", - path.to_string_lossy() - ) - .unwrap(); + let module = quote! { + pub mod #module_name { + const _: &[u8] = include_bytes!(#include_path); - for (image_name, image) in config.images() { - writeln!( - &mut output, - "{}", - convert_image(image, parent, &image_name, &config.crate_prefix()) - ) - .unwrap(); - } + #(#image_code)* + } + }; - writeln!(&mut output, "}}").unwrap(); - - output.parse().expect("Failed to generate valid rust code") + TokenStream::from(module) } fn convert_image( diff --git a/agb/Cargo.lock b/agb/Cargo.lock index aa2dd606..67d021c9 100644 --- a/agb/Cargo.lock +++ b/agb/Cargo.lock @@ -21,6 +21,8 @@ name = "agb_image_converter" version = "0.4.0" dependencies = [ "image", + "proc-macro2", + "quote", "serde", "syn", "toml",