Run rustfmt

This commit is contained in:
Gwilym Kuiper 2021-07-21 22:46:22 +01:00
parent bc46764e2f
commit 061b8be368
3 changed files with 63 additions and 22 deletions

View file

@ -2,15 +2,19 @@ use serde::Deserialize;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs; use std::fs;
use crate::{TileSize, Colour}; use crate::{Colour, TileSize};
pub(crate) fn parse(filename: &str) -> Box<dyn Config> { pub(crate) fn parse(filename: &str) -> Box<dyn Config> {
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"); let config: ConfigV1 = toml::from_str(&config_toml).expect("Failed to parse file");
if config.version != "1.0" { 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) Box::new(config)
@ -37,14 +41,16 @@ pub struct ConfigV1 {
impl Config for ConfigV1 { impl Config for ConfigV1 {
fn crate_prefix(&self) -> String { 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<String, &dyn Image> { fn images(&self) -> HashMap<String, &dyn Image> {
self.image.iter() self.image
.map(|(filename, image)| ( .iter()
filename.clone(), image as &dyn Image .map(|(filename, image)| (filename.clone(), image as &dyn Image))
)).collect() .collect()
} }
} }

View file

@ -1,15 +1,15 @@
use proc_macro::TokenStream;
use litrs::StringLit; use litrs::StringLit;
use proc_macro::TokenStream;
use std::path::Path;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::fmt::Write; use std::fmt::Write;
use std::path::Path;
mod colour; mod colour;
mod config;
mod image_loader; mod image_loader;
mod palette16; mod palette16;
mod rust_generator; mod rust_generator;
mod config;
use image_loader::Image; 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 root = std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get cargo manifest dir");
let path = Path::new(&root).join(&*filename); 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()); let config = config::parse(&path.to_string_lossy());
@ -50,10 +52,20 @@ pub fn include_gfx(input: TokenStream) -> TokenStream {
let mut output = String::new(); let mut output = String::new();
writeln!(&mut output, "mod {} {{", module_name.to_string_lossy()).unwrap(); 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() { 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(); writeln!(&mut output, "}}").unwrap();
@ -61,7 +73,12 @@ pub fn include_gfx(input: TokenStream) -> TokenStream {
output.parse().expect("Failed to generate valid rust code") 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_filename = &parent.join(&settings.filename());
let image = Image::load_from_file(image_filename); let image = Image::load_from_file(image_filename);

View file

@ -14,22 +14,34 @@ pub(crate) fn generate_code(
crate_prefix: String, crate_prefix: String,
) { ) {
writeln!(output, "#[allow(non_upper_case_globals)]").unwrap(); 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!( writeln!(
output, output,
"const PALETTE_DATA: &[{}::display::palette16::Palette16] = &[", "const PALETTE_DATA: &[{}::display::palette16::Palette16] = &[",
crate_prefix, crate_prefix,
).unwrap(); )
.unwrap();
for palette in &results.optimised_palettes { for palette in &results.optimised_palettes {
write!( write!(
output, output,
" {}::display::palette16::Palette16::new([", " {}::display::palette16::Palette16::new([",
crate_prefix crate_prefix
).unwrap(); )
.unwrap();
for colour in palette.clone() { for colour in palette.clone() {
write!(output, "0x{:08x}, ", colour.to_rgb15()).unwrap(); write!(output, "0x{:08x}, ", colour.to_rgb15()).unwrap();
@ -60,7 +72,8 @@ pub(crate) fn generate_code(
output, output,
" /* {}, {} (palette index {}) */", " /* {}, {} (palette index {}) */",
x, y, palette_index x, y, palette_index
).unwrap(); )
.unwrap();
for inner_y in 0..tile_size / 8 { for inner_y in 0..tile_size / 8 {
write!(output, " ").unwrap(); write!(output, " ").unwrap();
@ -99,5 +112,10 @@ pub(crate) fn generate_code(
writeln!(output, "\n];").unwrap(); 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();
} }