Skip to content

Commit

Permalink
update rust toolchain to 1.79.0
Browse files Browse the repository at this point in the history
  • Loading branch information
TitanNano committed Jun 17, 2024
1 parent b4fc663 commit 54b9d36
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 81 deletions.
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[toolchain]
channel = "1.67.1"
channel = "1.79.0"
17 changes: 13 additions & 4 deletions src/open_city_2k/bit_flags.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use super::sc_util::{int_to_bitstring, parse_bitstring};
use serde::Serialize;

Expand Down Expand Up @@ -25,6 +27,7 @@ pub struct BitFlags {
}

/// Returns the integer corresponding to the flags.
#[allow(dead_code)]
impl BitFlags {
fn to_u32(&self) -> u32 {
parse_bitstring(&self.to_string())
Expand Down Expand Up @@ -68,8 +71,8 @@ impl From<u8> for BitFlags {
}

/// Returns a binary string representing the bitflags.
impl ToString for BitFlags {
fn to_string(&self) -> String {
impl Display for BitFlags {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let attrs = [
self.powerable,
self.powered,
Expand All @@ -81,8 +84,14 @@ impl ToString for BitFlags {
self.salt,
];

let bit_string: String = attrs.iter().map(|x| if *x { '1' } else { '0' }).collect();
for attr in attrs {
if attr {
write!(f, "1")?;
} else {
write!(f, "0")?;
}
}

bit_string
Ok(())
}
}
24 changes: 1 addition & 23 deletions src/open_city_2k/budget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,32 +141,10 @@ struct BudgetItems {
}

trait EnumMap<K, V> {
fn get(&self, key: K) -> &V;
fn get_mut(&mut self, key: K) -> &mut V;
}

impl EnumMap<SubBudgetIndices, SubBudget> for BudgetItems {
fn get(&self, key: SubBudgetIndices) -> &SubBudget {
match key {
SubBudgetIndices::Residential => &self.residential,
SubBudgetIndices::Commercial => &self.commercial,
SubBudgetIndices::Industrial => &self.industrial,
SubBudgetIndices::Ordinances => &self.ordinances,
SubBudgetIndices::Bonds => &self.bonds,
SubBudgetIndices::Police => &self.police,
SubBudgetIndices::Fire => &self.fire,
SubBudgetIndices::Health => &self.health,
SubBudgetIndices::Schools => &self.schools,
SubBudgetIndices::Colleges => &self.colleges,
SubBudgetIndices::Road => &self.road,
SubBudgetIndices::Hiway => &self.hiway,
SubBudgetIndices::Bridge => &self.bridge,
SubBudgetIndices::Rail => &self.rail,
SubBudgetIndices::Subway => &self.subway,
SubBudgetIndices::Tunnel => &self.tunnel,
}
}

fn get_mut(&mut self, key: SubBudgetIndices) -> &mut SubBudget {
match key {
SubBudgetIndices::Residential => &mut self.residential,
Expand Down Expand Up @@ -255,7 +233,7 @@ impl Budget {
}
}

fn serialize_array<S: Serializer, T>(value: T, serializer: S) -> Result<S::Ok, S::Error>
fn serialize_array<S, T>(value: T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: IntoIterator,
Expand Down
9 changes: 6 additions & 3 deletions src/open_city_2k/building.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use super::buildings;
use serde::Serialize;

Expand Down Expand Up @@ -36,9 +38,10 @@ impl Building {
}
}

impl ToString for Building {
fn to_string(&self) -> String {
format!(
impl Display for Building {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Building: {} {:#04x} at {}, {}",
self.name, self.building_id, self.tile_coords.0, self.tile_coords.1
)
Expand Down
27 changes: 13 additions & 14 deletions src/open_city_2k/buildings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use std::ops::Range;
* microsim: What microsim applies to this tile (if any?)
* Possible Values: city_hall, hospital, police, fire, museum, park, school, stadium, prison, college, zoo, statue, library, bus, rail, wind, hydro, marina, subway, plymouth, forest, darco, launch, dome, mansion.
*/
#[allow(dead_code)]
struct TileAttributes {
name: &'static str,
size: usize,
Expand Down Expand Up @@ -55,6 +56,7 @@ enum TileZone {
Special,
}

#[allow(dead_code)]
enum Microsim {
None,
CityHall,
Expand Down Expand Up @@ -4481,6 +4483,7 @@ pub const NETWORK_IDS: Range<u8> = 0x0E..(0x6B + 1);
pub const HIGHWAY_2X2_IDS: Range<u8> = 0x61..(0x6B + 1);

/// Tiles that can have a train sprite drawn on them:
#[allow(dead_code)]
pub fn train_tiles() -> Vec<u8> {
TILE_DATA
.entries()
Expand All @@ -4491,27 +4494,23 @@ pub fn train_tiles() -> Vec<u8> {

// Section with nice functions to access the data contained here.

/**
* Gets the size of a building given the building's ID.
* Args:
* building_id (int): id of the building.
* Returns:
* Either 1, 2, 3 or 4 depending on how large the building is.
*/
/// Gets the size of a building given the building's ID.
/// Args:
/// building_id (int): id of the building.
/// Returns:
/// Either 1, 2, 3 or 4 depending on how large the building is.
pub fn get_size(building_id: &u8) -> Result<usize> {
match TILE_DATA.get(building_id) {
Some(building) => Ok(building.size),
None => Err(anyhow!("invalid building id {:#04x}", building_id)),
}
}

/**
* Gets the name of a building given the building's ID.
* Args:
* building_id (int): id of the building.
* Returns:
* String containing the building's name.
*/
/// Gets the name of a building given the building's ID.
/// Args:
/// building_id (int): id of the building.
/// Returns:
/// String containing the building's name.
pub fn get_name(building_id: &u8) -> Result<&'static str> {
match &TILE_DATA.get(building_id) {
Some(building) => Ok(building.name),
Expand Down
9 changes: 6 additions & 3 deletions src/open_city_2k/graph.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use super::sc_util::parse_int32;
use serde::Serialize;

Expand Down Expand Up @@ -54,9 +56,10 @@ impl Graph {
}
}

impl ToString for Graph {
fn to_string(&self) -> String {
format!(
impl Display for Graph {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Year:\n\t{:?}.\n10 Years:\n\t{:?}.\n100 Years:\n\t{:?}.\n",
self.one_year, self.ten_years, self.hundred_years
)
Expand Down
32 changes: 19 additions & 13 deletions src/open_city_2k/minimap.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use serde::Serialize;
use std::collections::HashMap;
use std::{collections::HashMap, fmt::Display};

// Couldn't think of a better name, but this stores minimap info/simulation variables stores in:
// XTRF, XPLT, XVAL, XCRM, XPLC, XFIR, XPOP, XROG.

#[allow(dead_code)]
const X64: [&str; 4] = ["XTRF", "XPLT", "XVAL", "XCRM"];
#[allow(dead_code)]
const X32: [&str; 4] = ["XPLC", "XFIR", "XPOP", "XROG"];

#[derive(Clone, Debug, Serialize)]
Expand Down Expand Up @@ -35,31 +36,36 @@ impl Minimap {
&self.data[&new_key]
}

#[allow(dead_code)]
pub fn set_scaled(&mut self, key: (usize, usize), item: u8) {
let new_key = self.convert_xy(key);

self.data.insert(new_key, item);
}

#[allow(dead_code)]
pub fn set_item(&mut self, key: (usize, usize), value: u8) {
self.data.insert(key, value);
}

#[allow(dead_code)]
fn get_item(&self, key: (usize, usize)) -> &u8 {
&self.data[&key]
}
}

impl ToString for Minimap {
fn to_string(&self) -> String {
[self.name.clone()]
.into_iter()
.chain((0..self.size).map(|_| {
let items: Vec<_> = (0..self.size).into_iter().map(|y| y.to_string()).collect();

items.join("")
}))
.collect::<Vec<_>>()
.join("\n")
impl Display for Minimap {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{}", self.name)?;

for _ in 0..self.size {
for y in 0..self.size {
write!(f, "{y}")?;
}

writeln!(f)?;
}

Ok(())
}
}
28 changes: 15 additions & 13 deletions src/open_city_2k/sc2_iff_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::sc_util;
use anyhow::{anyhow, Result};
use std::collections::HashMap;

#[allow(dead_code)]
struct Sc2FileHeader {
type_id: String,
data_size: usize,
Expand Down Expand Up @@ -76,7 +77,7 @@ impl ChunkList {
};
}

fn iter_compressed(self) -> CompressedIterator {
fn iter_compressed(&self) -> CompressedIterator {
CompressedIterator {
list: self,
cursor: 0,
Expand All @@ -103,6 +104,7 @@ impl ChunkList {
&self.pict
}

#[allow(dead_code)]
pub fn tile(&self) -> &[u8] {
&self.tile
}
Expand Down Expand Up @@ -184,12 +186,12 @@ impl ChunkList {
}
}

struct CompressedIterator {
list: ChunkList,
struct CompressedIterator<'a> {
list: &'a ChunkList,
cursor: usize,
}

impl Iterator for CompressedIterator {
impl<'a> Iterator for CompressedIterator<'a> {
type Item = (String, Vec<u8>);

fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -482,12 +484,6 @@ pub fn sc2_uncompress_input(input_file: ChunkList, input_type: &str) -> ChunkLis

match input_type {
"sc2" => {
uncompressed_chunk_list.text = input_file.text.clone();
uncompressed_chunk_list.cnam = input_file.cnam.clone();
uncompressed_chunk_list.altm = input_file.altm.clone();
uncompressed_chunk_list.scen = input_file.scen.clone();
uncompressed_chunk_list.pict = input_file.pict.clone();

input_file
.iter_compressed()
.map(|(key, slice)| {
Expand All @@ -496,15 +492,21 @@ pub fn sc2_uncompress_input(input_file: ChunkList, input_type: &str) -> ChunkLis
(key, uncompress_rle(&slice))
})
.for_each(|(key, value)| uncompressed_chunk_list.set(&key, &value));

uncompressed_chunk_list.text = input_file.text;
uncompressed_chunk_list.cnam = input_file.cnam;
uncompressed_chunk_list.altm = input_file.altm;
uncompressed_chunk_list.scen = input_file.scen;
uncompressed_chunk_list.pict = input_file.pict;
}

"mif" => {
uncompressed_chunk_list.tile = input_file.tile.clone();

input_file
.iter_compressed()
.map(|(key, slice)| (key, uncompress_rle(&slice)))
.for_each(|(key, value)| uncompressed_chunk_list.set(&key, &value));

uncompressed_chunk_list.tile = input_file.tile;
}

_ => panic!("unknown input type: {}", input_type),
Expand Down Expand Up @@ -542,7 +544,7 @@ fn uncompress_rle(encoded_data: &[u8]) -> Vec<u8> {
match (byte, byte_count, next_byte_repeat) {
(0..=0x7F, 0, _) => {
// In this case, byte is a count of the number of data bytes that follow.
byte_count = byte.to_owned();
byte.clone_into(&mut byte_count);
next_byte_repeat = false;
log::debug!("new byte count: {}", byte_count);
}
Expand Down
9 changes: 6 additions & 3 deletions src/open_city_2k/thing.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use serde::Serialize;

/// Class to represent a thing stored in the XTHG segment.
Expand Down Expand Up @@ -38,9 +40,10 @@ impl Thing {
}
}

impl ToString for Thing {
fn to_string(&self) -> String {
format!(
impl Display for Thing {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Thing with ID: {} at ({}, {}), rotations: {}, {}, data: {:?}",
self.thing_id, self.x, self.y, self.rotation_1, self.rotation_2, self.data
)
Expand Down
11 changes: 7 additions & 4 deletions src/open_city_2k/tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::building::Building;
use super::minimap::Minimap;
use super::sc_util::int_to_bitstring;
use serde::Serialize;
use std::fmt::Display;
use std::sync::Arc;

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -199,9 +200,10 @@ impl Tile {
}

fn get_text(&self) -> &str {
&self._label[(self.text_pointer as usize)]
&self._label[self.text_pointer as usize]
}

#[allow(dead_code)]
pub fn building(&self) -> &Option<Arc<Building>> {
&self.building
}
Expand All @@ -211,8 +213,8 @@ impl Tile {
}
}

impl ToString for Tile {
fn to_string(&self) -> String {
impl Display for Tile {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let terr = int_to_bitstring(self.terrain as u32, 0);
let b_id = if let Some(building) = &self.building {
format!("{:#04x}", building.building_id)
Expand All @@ -226,7 +228,8 @@ impl ToString for Tile {
String::from("")
};

format!(
write!(
f,
r#"Tile at {:?}
Altitude:
tunnel: {}, water: {}, unknown: {}, altitude: {}
Expand Down

0 comments on commit 54b9d36

Please sign in to comment.