Start switching to the quote crate for generating the code

This commit is contained in:
Gwilym Kuiper 2021-07-22 19:03:49 +01:00
parent 19e0a0527b
commit d7a24d9659
4 changed files with 22 additions and 22 deletions

View file

@ -13,6 +13,8 @@ name = "agb_image_converter"
version = "0.4.0"
dependencies = [
"image",
"proc-macro2",
"quote",
"serde",
"syn",
"toml",

View file

@ -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"
syn = "1.0.73"
proc-macro2 = "1.0.27"
quote = "1.0.9"

View file

@ -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::<proc_macro2::TokenStream>().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(

2
agb/Cargo.lock generated
View file

@ -21,6 +21,8 @@ name = "agb_image_converter"
version = "0.4.0"
dependencies = [
"image",
"proc-macro2",
"quote",
"serde",
"syn",
"toml",