Allow exporting included background

(cherry picked from commit 0f6548cc8cb4fabec868a234488f064c12961cd8)
This commit is contained in:
kouta 2024-07-06 21:11:44 -03:00
parent 7833561be1
commit 98ec2c5629

View file

@ -109,6 +109,7 @@ impl Parse for BackgroundGfxOption {
struct IncludeBackgroundGfxInput {
module_name: syn::Ident,
as_pub: bool,
crate_prefix: String,
transparent_colour: Colour,
background_gfx_options: Vec<BackgroundGfxOption>,
@ -126,6 +127,15 @@ impl Parse for IncludeBackgroundGfxInput {
format_ident!("agb")
};
let lookahead = input.lookahead1();
let as_pub = if lookahead.peek(Token![pub]) {
let _: Token![pub] = input.parse()?;
true
} else {
false
};
let module_name: syn::Ident = input.parse()?;
let _: Token![,] = input.parse()?;
@ -146,6 +156,7 @@ impl Parse for IncludeBackgroundGfxInput {
Ok(Self {
module_name,
as_pub,
crate_prefix: crate_prefix.to_string(),
transparent_colour,
background_gfx_options: background_gfx_options.into_iter().collect(),
@ -186,11 +197,13 @@ pub fn include_background_gfx(input: TokenStream) -> TokenStream {
let root = std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get cargo manifest dir");
let module_name = config.module_name.clone();
include_gfx_from_config(config, module_name, Path::new(&root))
let as_pub = config.as_pub.clone();
include_gfx_from_config(config, as_pub, module_name, Path::new(&root))
}
fn include_gfx_from_config(
config: Box<dyn config::Config>,
as_pub: bool,
module_name: syn::Ident,
parent: &Path,
) -> TokenStream {
@ -255,12 +268,22 @@ fn include_gfx_from_config(
let palette_code =
rust_generator::generate_palette_code(&optimisation_results, &config.crate_prefix());
let module = quote! {
let module = if as_pub {
quote! {
pub mod #module_name {
#palette_code
#(#image_code)*
}
}
} else {
quote! {
mod #module_name {
#palette_code
#(#image_code)*
}
}
};
TokenStream::from(module)