diff --git a/agb-image-converter/src/config.rs b/agb-image-converter/src/config.rs index 4d6b3bf4..1c2c62da 100644 --- a/agb-image-converter/src/config.rs +++ b/agb-image-converter/src/config.rs @@ -2,15 +2,19 @@ use serde::Deserialize; use std::collections::HashMap; use std::fs; -use crate::{TileSize, Colour}; +use crate::{Colour, TileSize}; pub(crate) fn parse(filename: &str) -> Box { - let config_toml = fs::read_to_string(filename).unwrap_or_else(|_| panic!("Failed to read file {}", filename)); - + let config_toml = + fs::read_to_string(filename).unwrap_or_else(|_| panic!("Failed to read file {}", filename)); + let config: ConfigV1 = toml::from_str(&config_toml).expect("Failed to parse file"); if config.version != "1.0" { - panic!("Expected version of {} to be 1.0, got {}", filename, config.version); + panic!( + "Expected version of {} to be 1.0, got {}", + filename, config.version + ); } Box::new(config) @@ -37,14 +41,16 @@ pub struct ConfigV1 { impl Config for ConfigV1 { fn crate_prefix(&self) -> String { - self.crate_prefix.clone().unwrap_or_else(|| "agb".to_owned()) + self.crate_prefix + .clone() + .unwrap_or_else(|| "agb".to_owned()) } fn images(&self) -> HashMap { - self.image.iter() - .map(|(filename, image)| ( - filename.clone(), image as &dyn Image - )).collect() + self.image + .iter() + .map(|(filename, image)| (filename.clone(), image as &dyn Image)) + .collect() } } diff --git a/agb-image-converter/src/lib.rs b/agb-image-converter/src/lib.rs index 97e74990..797df419 100644 --- a/agb-image-converter/src/lib.rs +++ b/agb-image-converter/src/lib.rs @@ -1,15 +1,15 @@ -use proc_macro::TokenStream; use litrs::StringLit; +use proc_macro::TokenStream; -use std::path::Path; use std::convert::TryFrom; use std::fmt::Write; +use std::path::Path; mod colour; +mod config; mod image_loader; mod palette16; mod rust_generator; -mod config; use image_loader::Image; @@ -41,7 +41,9 @@ pub fn include_gfx(input: TokenStream) -> TokenStream { let root = std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get cargo manifest dir"); let path = Path::new(&root).join(&*filename); - let parent = path.parent().expect("Expected a parent directory for the path"); + let parent = path + .parent() + .expect("Expected a parent directory for the path"); let config = config::parse(&path.to_string_lossy()); @@ -50,10 +52,20 @@ pub fn include_gfx(input: TokenStream) -> TokenStream { let mut output = String::new(); writeln!(&mut output, "mod {} {{", module_name.to_string_lossy()).unwrap(); - writeln!(&mut output, "const _: &[u8] = include_bytes!(\"{}\");", path.to_string_lossy()).unwrap(); + writeln!( + &mut output, + "const _: &[u8] = include_bytes!(\"{}\");", + path.to_string_lossy() + ) + .unwrap(); for (image_name, image) in config.images() { - writeln!(&mut output, "{}", convert_image(image, parent, &image_name, &config.crate_prefix())).unwrap(); + writeln!( + &mut output, + "{}", + convert_image(image, parent, &image_name, &config.crate_prefix()) + ) + .unwrap(); } writeln!(&mut output, "}}").unwrap(); @@ -61,7 +73,12 @@ pub fn include_gfx(input: TokenStream) -> TokenStream { output.parse().expect("Failed to generate valid rust code") } -fn convert_image(settings: &dyn config::Image, parent: &Path, variable_name: &str, crate_prefix: &str) -> String { +fn convert_image( + settings: &dyn config::Image, + parent: &Path, + variable_name: &str, + crate_prefix: &str, +) -> String { let image_filename = &parent.join(&settings.filename()); let image = Image::load_from_file(image_filename); diff --git a/agb-image-converter/src/rust_generator.rs b/agb-image-converter/src/rust_generator.rs index 34ab8f1b..d8f71100 100644 --- a/agb-image-converter/src/rust_generator.rs +++ b/agb-image-converter/src/rust_generator.rs @@ -14,22 +14,34 @@ pub(crate) fn generate_code( crate_prefix: String, ) { writeln!(output, "#[allow(non_upper_case_globals)]").unwrap(); - writeln!(output, "pub const {}: {}::display::tile_data::TileData = {{", output_variable_name, crate_prefix).unwrap(); + writeln!( + output, + "pub const {}: {}::display::tile_data::TileData = {{", + output_variable_name, crate_prefix + ) + .unwrap(); - writeln!(output, "const _: &[u8] = include_bytes!(\"{}\");", image_filename).unwrap(); + writeln!( + output, + "const _: &[u8] = include_bytes!(\"{}\");", + image_filename + ) + .unwrap(); writeln!( output, "const PALETTE_DATA: &[{}::display::palette16::Palette16] = &[", crate_prefix, - ).unwrap(); + ) + .unwrap(); for palette in &results.optimised_palettes { write!( output, " {}::display::palette16::Palette16::new([", crate_prefix - ).unwrap(); + ) + .unwrap(); for colour in palette.clone() { write!(output, "0x{:08x}, ", colour.to_rgb15()).unwrap(); @@ -60,7 +72,8 @@ pub(crate) fn generate_code( output, " /* {}, {} (palette index {}) */", x, y, palette_index - ).unwrap(); + ) + .unwrap(); for inner_y in 0..tile_size / 8 { write!(output, " ").unwrap(); @@ -99,5 +112,10 @@ pub(crate) fn generate_code( writeln!(output, "\n];").unwrap(); - writeln!(output, "{}::display::tile_data::TileData::new(PALETTE_DATA, TILE_DATA, PALETTE_ASSIGNMENT)\n}};", crate_prefix).unwrap(); + writeln!( + output, + "{}::display::tile_data::TileData::new(PALETTE_DATA, TILE_DATA, PALETTE_ASSIGNMENT)\n}};", + crate_prefix + ) + .unwrap(); }