Skip to content

Commit

Permalink
chore: clippy cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
francisdb committed Apr 2, 2024
1 parent bf11dd4 commit 7d75f1a
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 165 deletions.
4 changes: 2 additions & 2 deletions src/directb2s/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ pub struct DirectB2SData {

impl DirectB2SData {
/// Replaces image data with "\[stripped\]" for the whole structure
pub fn strip_images(self: &mut Self) {
pub fn strip_images(&mut self) {
self.images.backglass_image.iter_mut().for_each(|i| {
i.value = "[stripped]".to_string();
});
Expand Down Expand Up @@ -583,7 +583,7 @@ impl DirectB2SData {
});
}

fn strip_reels_images(images: &mut Vec<ReelsImage>) {
fn strip_reels_images(images: &mut [ReelsImage]) {
images.iter_mut().for_each(|i| {
i.image = "[stripped]".to_string();
i.intermediate_image1 = Some("[stripped]".to_string());
Expand Down
125 changes: 61 additions & 64 deletions src/vpx/expanded.rs

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions src/vpx/gamedata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ impl ViewSetup {
mode: VIEW_LAYOUT_MODE_LEGACY,
}
}
}

impl Default for ViewSetup {
fn default() -> Self {
ViewSetup::new()
Self::new()
}
}

Expand Down Expand Up @@ -566,11 +568,8 @@ impl GameDataJson {
}

pub fn from_game_data(game_data: &GameData) -> GameDataJson {
let custom_colors: [ColorJson; 16] = game_data
.custom_colors
.map(|c| ColorJson::from_color(&c))
.try_into()
.unwrap();
let custom_colors: [ColorJson; 16] =
game_data.custom_colors.map(|c| ColorJson::from_color(&c));
GameDataJson {
left: game_data.left,
top: game_data.top,
Expand Down
52 changes: 27 additions & 25 deletions src/vpx/gameitem/bumper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,31 +156,33 @@ impl<'de> Deserialize<'de> for Bumper {
D: serde::Deserializer<'de>,
{
let bumper_json = BumperJson::deserialize(deserializer)?;
let mut bumper = Bumper::default();
bumper.center = bumper_json.center;
bumper.radius = bumper_json.radius;
bumper.is_timer_enabled = bumper_json.is_timer_enabled;
bumper.timer_interval = bumper_json.timer_interval;
bumper.threshold = bumper_json.threshold;
bumper.force = bumper_json.force;
bumper.scatter = bumper_json.scatter;
bumper.height_scale = bumper_json.height_scale;
bumper.ring_speed = bumper_json.ring_speed;
bumper.orientation = bumper_json.orientation;
bumper.ring_drop_offset = bumper_json.ring_drop_offset.map(f32::from);
bumper.cap_material = bumper_json.cap_material;
bumper.base_material = bumper_json.base_material;
bumper.socket_material = bumper_json.socket_material;
bumper.ring_material = bumper_json.ring_material;
bumper.surface = bumper_json.surface;
bumper.name = bumper_json.name;
bumper.is_cap_visible = bumper_json.is_cap_visible;
bumper.is_base_visible = bumper_json.is_base_visible;
bumper.is_ring_visible = bumper_json.is_ring_visible;
bumper.is_socket_visible = bumper_json.is_socket_visible;
bumper.hit_event = bumper_json.hit_event;
bumper.is_collidable = bumper_json.is_collidable;
bumper.is_reflection_enabled = bumper_json.is_reflection_enabled;
let bumper = Bumper {
center: bumper_json.center,
radius: bumper_json.radius,
is_timer_enabled: bumper_json.is_timer_enabled,
timer_interval: bumper_json.timer_interval,
threshold: bumper_json.threshold,
force: bumper_json.force,
scatter: bumper_json.scatter,
height_scale: bumper_json.height_scale,
ring_speed: bumper_json.ring_speed,
orientation: bumper_json.orientation,
ring_drop_offset: bumper_json.ring_drop_offset.map(f32::from),
cap_material: bumper_json.cap_material,
base_material: bumper_json.base_material,
socket_material: bumper_json.socket_material,
ring_material: bumper_json.ring_material,
surface: bumper_json.surface,
name: bumper_json.name,
is_cap_visible: bumper_json.is_cap_visible,
is_base_visible: bumper_json.is_base_visible,
is_ring_visible: bumper_json.is_ring_visible,
is_socket_visible: bumper_json.is_socket_visible,
hit_event: bumper_json.hit_event,
is_collidable: bumper_json.is_collidable,
is_reflection_enabled: bumper_json.is_reflection_enabled,
..Default::default()
};
Ok(bumper)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/vpx/gameitem/flipper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub(crate) struct FlipperJson {
impl FlipperJson {
pub fn from_flipper(flipper: &Flipper) -> Self {
Self {
center: flipper.center.clone(),
center: flipper.center,
base_radius: flipper.base_radius,
end_radius: flipper.end_radius,
flipper_radius_max: flipper.flipper_radius_max,
Expand Down Expand Up @@ -135,7 +135,7 @@ impl FlipperJson {

pub fn to_flipper(&self) -> Flipper {
Flipper {
center: self.center.clone(),
center: self.center,
base_radius: self.base_radius,
end_radius: self.end_radius,
flipper_radius_max: self.flipper_radius_max,
Expand Down
12 changes: 6 additions & 6 deletions src/vpx/gameitem/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ pub enum FontStyle {
impl FontStyle {
fn to_u8(flag: &FontStyle) -> u8 {
match flag {
&FontStyle::Normal => 1 << 0,
&FontStyle::Bold => 1 << 1,
&FontStyle::Italic => 1 << 2,
&FontStyle::Underline => 1 << 3,
&FontStyle::Strikethrough => 1 << 4,
FontStyle::Normal => 1 << 0,
FontStyle::Bold => 1 << 1,
FontStyle::Italic => 1 << 2,
FontStyle::Underline => 1 << 3,
FontStyle::Strikethrough => 1 << 4,
}
}
pub fn flags_to_styles(style: u8) -> HashSet<Self> {
Expand Down Expand Up @@ -79,7 +79,7 @@ impl FontStyle {
for flag in flags {
bitflags |= Self::to_u8(flag);
}
return bitflags;
bitflags
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/vpx/gameitem/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl PrimitiveJson {
add_blend: self.add_blend,
use_depth_mask: self.use_depth_mask,
alpha: self.alpha,
color: self.color.as_ref().map(|c| ColorJson::to_color(c)),
color: self.color.as_ref().map(ColorJson::to_color),
light_map: self.light_map.clone(),
reflection_probe: self.reflection_probe.clone(),
reflection_strength: self.reflection_strength,
Expand Down
2 changes: 1 addition & 1 deletion src/vpx/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl ImageDataJson {
jpeg: image_data
.jpeg
.as_ref()
.map(|jpeg| ImageDataJpegJson::from_image_data_jpeg(jpeg)),
.map(ImageDataJpegJson::from_image_data_jpeg),
bits: image_data.bits.is_some(),
name_dedup: None,
}
Expand Down
16 changes: 6 additions & 10 deletions src/vpx/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ where
"inf" => Ok(f32::INFINITY),
"-inf" => Ok(f32::NEG_INFINITY),
other => {
if other.starts_with("nan|") {
let hex_string = &other[4..];
if let Some(hex_string) = other.strip_prefix("nan|") {
let bytes = hex::decode(hex_string)
.map_err(|e| serde::de::Error::custom(e.to_string()))?;
let mut array = [0; 4];
Expand Down Expand Up @@ -116,14 +115,11 @@ mod tests {
let json_nan = json!("NaN|0000c07f");
let json_other_nan = json!("NaN|ffffffff");

assert_eq!(serde_json::to_value(&f32_num).unwrap(), json_num);
assert_eq!(serde_json::to_value(&f32_inf).unwrap(), json_inf);
assert_eq!(serde_json::to_value(&f32_n_inf).unwrap(), json_n_inf);
assert_eq!(serde_json::to_value(&f32_nan).unwrap(), json_nan);
assert_eq!(
serde_json::to_value(&f32_other_nan).unwrap(),
json_other_nan
);
assert_eq!(serde_json::to_value(f32_num).unwrap(), json_num);
assert_eq!(serde_json::to_value(f32_inf).unwrap(), json_inf);
assert_eq!(serde_json::to_value(f32_n_inf).unwrap(), json_n_inf);
assert_eq!(serde_json::to_value(f32_nan).unwrap(), json_nan);
assert_eq!(serde_json::to_value(f32_other_nan).unwrap(), json_other_nan);

assert_eq!(
serde_json::from_value::<F32WithNanInf>(json_num).unwrap(),
Expand Down
6 changes: 2 additions & 4 deletions src/vpx/jsonmodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ pub fn info_to_json(
properties: table_info.properties.clone(),
properties_order: custom_info_tags.clone(),
};
let json = to_value(info_json).unwrap();
json
to_value(info_json).unwrap()
}

pub fn json_to_info(
Expand Down Expand Up @@ -96,8 +95,7 @@ pub fn collections_json(collections: &[Collection]) -> serde_json::Value {
};
collections_json.push(collection_json);
}
let json = to_value(collections_json).unwrap();
json
to_value(collections_json).unwrap()
}

pub fn json_to_collections(json: serde_json::Value) -> Result<Vec<Collection>, serde_json::Error> {
Expand Down
45 changes: 22 additions & 23 deletions src/vpx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use std::io::{self, Read, Seek, Write};
use std::path::MAIN_SEPARATOR_STR;
use std::{
fs,
fs::File,
path::{Path, PathBuf},
};
Expand Down Expand Up @@ -86,15 +85,15 @@ pub(crate) mod wav;
#[derive(Debug, PartialEq)]
pub struct VPX {
/// This is mainly here to have an ordering for custom info tags
pub custominfotags: custominfotags::CustomInfoTags, // this is a bit redundant
pub custominfotags: CustomInfoTags, // this is a bit redundant
pub info: TableInfo,
pub version: Version,
pub gamedata: GameData,
pub gameitems: Vec<gameitem::GameItemEnum>,
pub images: Vec<image::ImageData>,
pub sounds: Vec<sound::SoundData>,
pub fonts: Vec<font::FontData>,
pub collections: Vec<collection::Collection>,
pub gameitems: Vec<GameItemEnum>,
pub images: Vec<ImageData>,
pub sounds: Vec<SoundData>,
pub fonts: Vec<FontData>,
pub collections: Vec<Collection>,
}

pub enum ExtractResult {
Expand Down Expand Up @@ -145,7 +144,7 @@ impl<F: Read + Seek + Write> VpxFile<F> {
}

pub fn read_tableinfo(&mut self) -> io::Result<TableInfo> {
tableinfo::read_tableinfo(&mut self.compound_file)
read_tableinfo(&mut self.compound_file)
}

pub fn read_gamedata(&mut self) -> io::Result<GameData> {
Expand Down Expand Up @@ -185,8 +184,8 @@ impl<F: Read + Seek + Write> VpxFile<F> {
}

/// Opens a handle to an existing VPX file
pub fn open<P: AsRef<Path>>(path: P) -> io::Result<VpxFile<fs::File>> {
VpxFile::open(fs::File::open(path)?)
pub fn open<P: AsRef<Path>>(path: P) -> io::Result<VpxFile<File>> {
VpxFile::open(File::open(path)?)
}

/// Reads a VPX file from disk to memory
Expand Down Expand Up @@ -214,6 +213,7 @@ pub fn write(path: &PathBuf, vpx: &VPX) -> io::Result<()> {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(path)?;
let mut comp = CompoundFile::create(file)?;
write_vpx(&mut comp, vpx)
Expand Down Expand Up @@ -263,12 +263,13 @@ pub fn new_minimal_vpx<P: AsRef<Path>>(vpx_file_path: P) -> io::Result<()> {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(&vpx_file_path)?;
let mut comp = CompoundFile::create(file)?;
write_minimal_vpx(&mut comp)
}

fn write_minimal_vpx<F: Read + Write + Seek>(comp: &mut CompoundFile<F>) -> std::io::Result<()> {
fn write_minimal_vpx<F: Read + Write + Seek>(comp: &mut CompoundFile<F>) -> io::Result<()> {
let table_info = TableInfo::new();
write_tableinfo(comp, &table_info)?;
create_game_storage(comp)?;
Expand Down Expand Up @@ -316,7 +317,7 @@ pub fn extractvbs(
/// Imports the sidecar script into the provided `vpx` file.
///
/// see also [extractvbs]
pub fn importvbs(vpx_file_path: &PathBuf, extension: Option<&str>) -> std::io::Result<PathBuf> {
pub fn importvbs(vpx_file_path: &PathBuf, extension: Option<&str>) -> io::Result<PathBuf> {
let script_path = match extension {
Some(ext) => path_for(vpx_file_path, ext),
None => vbs_path_for(vpx_file_path),
Expand Down Expand Up @@ -371,7 +372,7 @@ fn path_for(vpx_file_path: &PathBuf, extension: &str) -> PathBuf {
PathBuf::from(vpx_file_path).with_extension(extension)
}

fn read_mac<F: Read + Write + Seek>(comp: &mut cfb::CompoundFile<F>) -> std::io::Result<Vec<u8>> {
fn read_mac<F: Read + Write + Seek>(comp: &mut CompoundFile<F>) -> io::Result<Vec<u8>> {
let mac_path = Path::new(MAIN_SEPARATOR_STR).join("GameStg").join("MAC");
if !comp.exists(&mac_path) {
// fail
Expand All @@ -386,10 +387,7 @@ fn read_mac<F: Read + Write + Seek>(comp: &mut cfb::CompoundFile<F>) -> std::io:
Ok(mac)
}

fn write_mac<F: Read + Write + Seek>(
comp: &mut CompoundFile<F>,
mac: &[u8],
) -> std::io::Result<()> {
fn write_mac<F: Read + Write + Seek>(comp: &mut CompoundFile<F>, mac: &[u8]) -> io::Result<()> {
let mac_path = Path::new(MAIN_SEPARATOR_STR).join("GameStg").join("MAC");
let mut mac_stream = comp.create_stream(mac_path)?;
mac_stream.write_all(mac)
Expand Down Expand Up @@ -491,11 +489,11 @@ fn generate_mac<F: Read + Seek>(comp: &mut CompoundFile<F>) -> Result<Vec<u8>, i
continue;
}
match item.file_type {
FileType::UnstructuredBytes => {
UnstructuredBytes => {
let bytes = read_bytes_at(&item.path, comp)?;
hasher.update(&bytes);
}
FileType::Biff => {
Biff => {
// println!("reading biff: {:?}", item.path);
let bytes = read_bytes_at(&item.path, comp)?;
let mut biff = BiffReader::new(&bytes);
Expand Down Expand Up @@ -580,7 +578,7 @@ pub fn extract_script<P: AsRef<Path>>(gamedata: &GameData, vbs_path: &P) -> Resu
fn read_gamedata<F: Seek + Read>(
comp: &mut CompoundFile<F>,
version: &Version,
) -> std::io::Result<GameData> {
) -> io::Result<GameData> {
let mut game_data_vec = Vec::new();
let game_data_path = Path::new(MAIN_SEPARATOR_STR)
.join("GameStg")
Expand Down Expand Up @@ -642,7 +640,7 @@ fn read_sounds<F: Read + Seek>(
comp: &mut CompoundFile<F>,
gamedata: &GameData,
file_version: &Version,
) -> std::io::Result<Vec<SoundData>> {
) -> io::Result<Vec<SoundData>> {
(0..gamedata.sounds_size)
.map(|index| {
let path = Path::new(MAIN_SEPARATOR_STR)
Expand Down Expand Up @@ -813,7 +811,7 @@ mod tests {
write_minimal_vpx(&mut comp)?;

let version = read_version(&mut comp)?;
let tableinfo = tableinfo::read_tableinfo(&mut comp)?;
let tableinfo = read_tableinfo(&mut comp)?;
let game_data = read_gamedata(&mut comp, &version)?;

assert_eq!(tableinfo, TableInfo::new());
Expand Down Expand Up @@ -903,6 +901,7 @@ mod tests {
expected_info.table_name = Some(String::from("Visual Pinball Demo Table"));
expected_info.table_save_rev = Some(String::from("10"));
expected_info.table_version = Some(String::from("1.2"));

expected_info.author_website = Some(String::from("http://www.vpforums.org/"));
expected_info.table_save_date = Some(String::from("Tue Jul 11 15:48:49 2023"));
expected_info.table_description = Some(String::from(
Expand All @@ -928,7 +927,7 @@ mod tests {
fn create_minimal_vpx_and_read() -> io::Result<()> {
let dir: PathBuf = testdir!();
let test_vpx_path = dir.join("test.vpx");
let mut comp = cfb::create(&test_vpx_path)?;
let mut comp = cfb::create(test_vpx_path)?;
write_minimal_vpx(&mut comp)?;
comp.flush()?;
let vpx = read_vpx(&mut comp)?;
Expand Down
Loading

0 comments on commit 7d75f1a

Please sign in to comment.