Skip to content

Commit

Permalink
Merge pull request #80 from scd31/clippy_fixes
Browse files Browse the repository at this point in the history
Clippy fixes
  • Loading branch information
louis-e authored Nov 2, 2024
2 parents 25e4b17 + 31fe15b commit 7ef7950
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 81 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ jobs:
with:
toolchain: stable
target: ${{ matrix.os == 'windows-latest' && 'x86_64-pc-windows-msvc' || 'x86_64-unknown-linux-gnu' || 'x86_64-apple-darwin' }}
components: clippy

- name: Check formatting
run: cargo fmt -- --check

- name: Check clippy lints
run: cargo clippy --all-targets --all-features -- -D warnings

- name: Install dependencies
run: cargo fetch
Expand Down
4 changes: 2 additions & 2 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ fn validate_bounding_box(bbox: &str) -> bool {
let max_lng: f64 = parts[2].parse().ok().unwrap_or(0.0);
let max_lat: f64 = parts[3].parse().ok().unwrap_or(0.0);

if !(min_lng >= -180.0 && min_lng <= 180.0) || !(max_lng >= -180.0 && max_lng <= 180.0) {
if !(-180.0..=180.0).contains(&min_lng) || !(-180.0..=180.0).contains(&max_lng) {
return false;
}

if !(min_lat >= -90.0 && min_lat <= 90.0) || !(max_lat >= -90.0 && max_lat <= 90.0) {
if !(-90.0..=90.0).contains(&min_lat) || !(-90.0..=90.0).contains(&max_lat) {
return false;
}

Expand Down
9 changes: 8 additions & 1 deletion src/bresenham.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/// Generates the coordinates for a line between two points using the Bresenham algorithm.
/// The result is a vector of 3D coordinates (x, y, z).
pub fn bresenham_line(x1: i32, y1: i32, z1: i32, x2: i32, y2: i32, z2: i32) -> Vec<(i32, i32, i32)> {
pub fn bresenham_line(
x1: i32,
y1: i32,
z1: i32,
x2: i32,
y2: i32,
z2: i32,
) -> Vec<(i32, i32, i32)> {
let mut points: Vec<(i32, i32, i32)> = Vec::new();

let dx: i32 = (x2 - x1).abs();
Expand Down
18 changes: 11 additions & 7 deletions src/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ pub type RGBTuple = (u8, u8, u8);

pub fn color_text_to_rgb_tuple(text: &str) -> Option<RGBTuple> {
if let Some(rgb) = full_hex_color_to_rgb_tuple(text) {
Some(rgb)
} else if let Some(rgb) = short_hex_color_to_rgb_tuple(text) {
Some(rgb)
} else if let Some(rgb) = color_name_to_rgb_tuple(text) {
Some(rgb)
} else {
None
return Some(rgb);
}

if let Some(rgb) = short_hex_color_to_rgb_tuple(text) {
return Some(rgb);
}

if let Some(rgb) = color_name_to_rgb_tuple(text) {
return Some(rgb);
}

None
}

fn full_hex_color_to_rgb_tuple(text: &str) -> Option<RGBTuple> {
Expand Down
9 changes: 4 additions & 5 deletions src/data_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn generate_world(
scale_factor_x: f64,
scale_factor_z: f64,
) {
println!("{} {}", "[3/5]".bold(), "Processing data...");
println!("{} Processing data...", "[3/5]".bold());

let region_template_path: &str = "region.template";
let region_dir: String = format!("{}/region", args.path);
Expand All @@ -35,7 +35,7 @@ pub fn generate_world(
&region_dir,
scale_factor_x,
scale_factor_z,
&args,
args,
);

// Process data
Expand Down Expand Up @@ -163,7 +163,7 @@ pub fn generate_world(

let mut block_counter: u64 = 0;

println!("{} {}", "[4/5]".bold(), "Generating ground layer...");
println!("Generating ground layer... {}", "[4/5]".bold());
let ground_pb: ProgressBar = ProgressBar::new(total_blocks);
ground_pb.set_style(
ProgressStyle::default_bar()
Expand All @@ -176,8 +176,7 @@ pub fn generate_world(
for z in 0..=(scale_factor_z as i32) {
// Use the smaller of [current block y, ground level y]
let max_y = (MIN_Y..MAX_Y)
.filter(|y| editor.block_at(x, *y, z))
.next()
.find(|y| editor.block_at(x, *y, z))
.unwrap_or(MAX_Y)
.min(GROUND_LEVEL);

Expand Down
4 changes: 1 addition & 3 deletions src/element_processing/amenities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ pub fn generate_amenities(
if let Some((x, z)) = first_node {
editor.set_block(CAULDRON, x, ground_level + 1, z, None, None);
}
return;
}
"vending_machine" | "atm" => {
if let Some((x, z)) = first_node {
editor.set_block(IRON_BLOCK, x, ground_level + 1, z, None, None);
editor.set_block(IRON_BLOCK, x, ground_level + 2, z, None, None);
}
return;
}
"bicycle_parking" => {
let ground_block = OAK_PLANKS;
Expand Down Expand Up @@ -146,7 +144,7 @@ pub fn generate_amenities(

// Flood-fill the interior area for parking or fountains
if corner_addup.2 > 0 {
let polygon_coords: Vec<(i32, i32)> = current_amenity.iter().copied().collect();
let polygon_coords: Vec<(i32, i32)> = current_amenity.to_vec();
let flood_area: Vec<(i32, i32)> =
flood_fill_area(&polygon_coords, floodfill_timeout);

Expand Down
6 changes: 2 additions & 4 deletions src/element_processing/buildings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,20 @@ pub fn generate_buildings(
let wall_block = element
.tags
.get("building:colour")
.map(|building_colour| {
.and_then(|building_colour| {
color_text_to_rgb_tuple(building_colour)
.map(|rgb| find_nearest_block_in_color_map(&rgb, building_wall_color_map()))
})
.flatten()
.flatten()
.unwrap_or_else(|| building_wall_variations()[variation_index_wall]);
let floor_block = element
.tags
.get("roof:colour")
.map(|roof_colour| {
.and_then(|roof_colour| {
color_text_to_rgb_tuple(roof_colour)
.map(|rgb| find_nearest_block_in_color_map(&rgb, building_floor_color_map()))
})
.flatten()
.flatten()
.unwrap_or_else(|| building_floor_variations()[variation_index_floor]);
let window_block = WHITE_STAINED_GLASS;

Expand Down
16 changes: 4 additions & 12 deletions src/element_processing/tourisms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,11 @@ pub fn generate_tourisms(editor: &mut WorldEditor, element: &ProcessedNode, grou
let x = element.x;
let z = element.z;

match tourism_type.as_str() {
"information" => {
if let Some(information_type) = element.tags.get("information") {
match information_type.as_str() {
"board" => {
// TODO draw a sign
editor.set_block(OAK_PLANKS, x, ground_level + 1, z, None, None);
}
_ => {}
}
}
if tourism_type == "information" {
if let Some("board") = element.tags.get("information").map(|x| x.as_str()) {
// TODO draw a sign
editor.set_block(OAK_PLANKS, x, ground_level + 1, z, None, None);
}
_ => {}
}
}
}
1 change: 1 addition & 0 deletions src/element_processing/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub fn create_tree(editor: &mut WorldEditor, x: i32, y: i32, z: i32, typetree: u
match typetree {
1 => {
// Oak tree

editor.fill_blocks(OAK_LOG, x, y, z, x, y + 8, z, None, None);
editor.fill_blocks(OAK_LEAVES, x - 1, y + 3, z, x - 1, y + 9, z, None, None);
editor.fill_blocks(OAK_LEAVES, x + 1, y + 3, z, x + 1, y + 9, z, None, None);
Expand Down
59 changes: 21 additions & 38 deletions src/element_processing/water_areas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,8 @@ fn inverse_floodfill(
.collect();

inverse_floodfill_recursive(
min_x,
max_x,
min_z,
max_z,
(min_x, min_z),
(max_x, max_z),
ground_level,
&outers,
&inners,
Expand All @@ -176,43 +174,32 @@ fn inverse_floodfill(
}

fn inverse_floodfill_recursive(
min_x: i32,
max_x: i32,
min_z: i32,
max_z: i32,
min: (i32, i32),
max: (i32, i32),
ground_level: i32,
outers: &[Polygon],
inners: &[Polygon],
editor: &mut WorldEditor,
) {
const ITERATIVE_THRES: i32 = 10_000;

if min_x > max_x || min_z > max_z {
if min.0 > max.0 || min.1 > max.1 {
return;
}

if (max_x - min_x) * (max_z - min_z) < ITERATIVE_THRES {
inverse_floodfill_iterative(
min_x,
max_x,
min_z,
max_z,
ground_level,
outers,
inners,
editor,
);
if (max.0 - min.0) * (max.1 - min.1) < ITERATIVE_THRES {
inverse_floodfill_iterative(min, max, ground_level, outers, inners, editor);

return;
}

let center_x = (min_x + max_x) / 2;
let center_z = (min_z + max_z) / 2;
let center_x = (min.0 + max.0) / 2;
let center_z = (min.1 + max.1) / 2;
let quadrants = [
(min_x, center_x, min_z, center_z),
(center_x, max_x, min_z, center_z),
(min_x, center_x, center_z, max_z),
(center_x, max_x, center_z, max_z),
(min.0, center_x, min.1, center_z),
(center_x, max.0, min.1, center_z),
(min.0, center_x, center_z, max.1),
(center_x, max.0, center_z, max.1),
];

for (min_x, max_x, min_z, max_z) in quadrants {
Expand All @@ -236,26 +223,24 @@ fn inverse_floodfill_recursive(
// This saves on processing time
let outers_intersects: Vec<_> = outers
.iter()
.cloned()
.filter(|poly| poly.intersects(&rect))
.cloned()
.collect();

// Moving this inside the below `if` statement makes it slower for some reason.
// I assume it changes how the compiler is able to optimize it
let inners_intersects: Vec<_> = inners
.iter()
.cloned()
.filter(|poly| poly.intersects(&rect))
.cloned()
.collect();

if !outers_intersects.is_empty() {
// recurse

inverse_floodfill_recursive(
min_x,
max_x,
min_z,
max_z,
(min_x, min_z),
(max_x, max_z),
ground_level,
&outers_intersects,
&inners_intersects,
Expand All @@ -267,17 +252,15 @@ fn inverse_floodfill_recursive(

// once we "zoom in" enough, it's more efficient to switch to iteration
fn inverse_floodfill_iterative(
min_x: i32,
max_x: i32,
min_z: i32,
max_z: i32,
min: (i32, i32),
max: (i32, i32),
ground_level: i32,
outers: &[Polygon],
inners: &[Polygon],
editor: &mut WorldEditor,
) {
for x in min_x..max_x {
for z in min_z..max_z {
for x in min.0..max.0 {
for z in min.1..max.1 {
let p = Point::new(x as f64, z as f64);

if outers.iter().any(|poly| poly.contains(&p))
Expand Down
8 changes: 4 additions & 4 deletions src/osm_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub fn parse_osm_data(
bbox: (f64, f64, f64, f64),
args: &Args,
) -> (Vec<ProcessedElement>, f64, f64) {
println!("{} {}", "[2/5]".bold(), "Parsing data...");
println!("{} Parsing data...", "[2/5]".bold());

// Deserialize the JSON data into the OSMData structure
let data: OsmData =
Expand Down Expand Up @@ -167,7 +167,7 @@ pub fn parse_osm_data(

let processed = ProcessedNode {
id: element.id,
tags: element.tags.as_ref().map(|x| x.clone()).unwrap_or_default(),
tags: element.tags.clone().unwrap_or_default(),
x,
z,
};
Expand Down Expand Up @@ -295,7 +295,7 @@ fn geo_distance(lat1: f64, lat2: f64, lon1: f64, lon2: f64) -> (f64, f64) {
// Haversine but optimized for a latitude delta of 0
// returns meters
fn lon_distance(lat: f64, lon1: f64, lon2: f64) -> f64 {
const R: f64 = 6371_000.0;
const R: f64 = 6_371_000.0;
let d_lon = (lon2 - lon1).to_radians();
let a =
lat.to_radians().cos() * lat.to_radians().cos() * (d_lon / 2.0).sin() * (d_lon / 2.0).sin();
Expand All @@ -307,7 +307,7 @@ fn lon_distance(lat: f64, lon1: f64, lon2: f64) -> f64 {
// Haversine but optimized for a longitude delta of 0
// returns meters
fn lat_distance(lat1: f64, lat2: f64) -> f64 {
const R: f64 = 6371_000.0;
const R: f64 = 6_371_000.0;
let d_lat = (lat2 - lat1).to_radians();
let a = (d_lat / 2.0).sin() * (d_lat / 2.0).sin();
let c = 2.0 * a.sqrt().atan2((1.0 - a).sqrt());
Expand Down
2 changes: 1 addition & 1 deletion src/retrieve_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub fn fetch_data(
debug: bool,
download_method: &str,
) -> Result<Value, Box<dyn std::error::Error>> {
println!("{} {}", "[1/5]".bold(), "Fetching data...");
println!("{} Fetching data...", "[1/5]".bold());

// List of Overpass API servers
let api_servers: Vec<&str> = vec![
Expand Down
7 changes: 4 additions & 3 deletions src/version_check.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use colored::Colorize;
use reqwest::{Error as ReqwestError, StatusCode};
use reqwest::blocking::Client;
use reqwest::{Error as ReqwestError, StatusCode};
use semver::Version;
use std::error::Error;

/// URL to the remote Cargo.toml file to check for the latest version
const REMOTE_CARGO_TOML_URL: &str = "https://raw.githubusercontent.com/louis-e/arnis/main/Cargo.toml";
const REMOTE_CARGO_TOML_URL: &str =
"https://raw.githubusercontent.com/louis-e/arnis/main/Cargo.toml";

/// Fetches the latest version from the remote Cargo.toml file and compares it with the local version.
/// If a newer version is available, prints a message.
pub fn check_for_updates() -> Result<(), Box<dyn Error>> {
let client: Client = Client::new();

// Fetch the remote Cargo.toml file with a User-Agent header
let response: Result<reqwest::blocking::Response, ReqwestError> = client
.get(REMOTE_CARGO_TOML_URL)
Expand Down
3 changes: 2 additions & 1 deletion src/world_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ impl<'a> WorldEditor<'a> {
}

/// Fills a cuboid area with the specified block between two coordinates.
#[allow(clippy::too_many_arguments)]
pub fn fill_blocks(
&mut self,
block: Block,
Expand Down Expand Up @@ -370,7 +371,7 @@ impl<'a> WorldEditor<'a> {

/// Saves all changes made to the world by writing modified chunks to the appropriate region files.
pub fn save(&mut self) {
println!("{} {}", "[5/5]".bold(), "Saving world...");
println!("{} Saving world...", "[5/5]".bold());

let _debug: bool = self.args.debug;
let total_regions: u64 = self.world.regions.len() as u64;
Expand Down

0 comments on commit 7ef7950

Please sign in to comment.