From c4ba6beeec228137127a0b1914e63d25ea631f3d Mon Sep 17 00:00:00 2001 From: thehappycheese Date: Sun, 6 Aug 2023 01:25:13 +0800 Subject: [PATCH] cargo fmt + clippy --- .../algorithm/line_split/line_split_trait.rs | 18 +-- .../line_split_trait_impl_for_line.rs | 127 +++++++++--------- .../line_split_trait_impl_for_linestring.rs | 32 +++-- .../line_split/line_split_twice_result.rs | 2 +- 4 files changed, 92 insertions(+), 87 deletions(-) diff --git a/geo/src/algorithm/line_split/line_split_trait.rs b/geo/src/algorithm/line_split/line_split_trait.rs index 63cbf12590..0b87e605a5 100644 --- a/geo/src/algorithm/line_split/line_split_trait.rs +++ b/geo/src/algorithm/line_split/line_split_trait.rs @@ -2,20 +2,22 @@ use geo_types::CoordFloat; use super::{LineSplitResult, LineSplitTwiceResult}; - -pub trait LineSplit where Self:Sized, Scalar: CoordFloat { - +pub trait LineSplit +where + Self: Sized, + Scalar: CoordFloat, +{ /// Note on choice of return type: - /// + /// /// You may wonder why this does not return `Option<(Option, Option)>`? /// It is because then the return type causes uncertainty; The user may expect to possibly /// receive `Some((None, None))` which is never possible, this would lead to clutter in match /// statements. - /// + /// /// To make it easier to 'just get the first' or 'just get the second' you can use /// `LineSplitResult::first()` and `LineSplitResult::second()` which return `Option` - /// - /// + /// + /// fn line_split(&self, fraction: Scalar) -> Option>; /// Note on choice of return type: @@ -57,7 +59,7 @@ pub trait LineSplit where Self:Sized, Scalar: CoordFloat { }, Some(LineSplitResult::First (line1)) => Some(First(line1)), Some(LineSplitResult::Second(line2)) => match line2.line_split(second_fraction) { - Some(LineSplitResult::FirstSecond(line2, line3)) => Some(SecondThird ( line2, line3)), + Some(LineSplitResult::FirstSecond(line2, line3)) => Some(SecondThird ( line2, line3)), Some(LineSplitResult::First (line2 )) => Some(Second ( line2 )), Some(LineSplitResult::Second ( line3)) => Some(Third ( line3)), None => None, diff --git a/geo/src/algorithm/line_split/line_split_trait_impl_for_line.rs b/geo/src/algorithm/line_split/line_split_trait_impl_for_line.rs index 5a9e7be861..49b37d6ce0 100644 --- a/geo/src/algorithm/line_split/line_split_trait_impl_for_line.rs +++ b/geo/src/algorithm/line_split/line_split_trait_impl_for_line.rs @@ -1,19 +1,19 @@ -use geo_types::{Line, CoordFloat}; +use geo_types::{CoordFloat, Line}; -use super::{ - LineSplitResult, - LineSplit, -}; +use super::{LineSplit, LineSplitResult}; -impl LineSplit for Line where Scalar: CoordFloat { +impl LineSplit for Line +where + Scalar: CoordFloat, +{ fn line_split(&self, fraction: Scalar) -> Option> { if fraction.is_nan() { - return None + return None; } if fraction <= Scalar::zero() { - Some(LineSplitResult::Second(self.clone())) + Some(LineSplitResult::Second(*self)) } else if fraction >= Scalar::one() { - Some(LineSplitResult::First(self.clone())) + Some(LineSplitResult::First(*self)) } else { let new_midpoint = self.start + self.delta() * fraction; Some(LineSplitResult::FirstSecond( @@ -25,10 +25,10 @@ impl LineSplit for Line where Scalar: CoordFloat { } #[cfg(test)] -mod test{ - use geo_types::coord; +mod test { use super::super::LineSplitTwiceResult; use super::*; + use geo_types::coord; // ============================================================================================= // Line::line_split() @@ -38,71 +38,71 @@ mod test{ fn test_line_split_first_second() { // simple x-axis aligned check let line = Line::new( - coord!{x: 0.0_f32, y:0.0_f32}, - coord!{x:10.0_f32, y:0.0_f32}, + coord! {x: 0.0_f32, y:0.0_f32}, + coord! {x:10.0_f32, y:0.0_f32}, ); let result = line.line_split(0.6); - assert_eq!(result, Some(LineSplitResult::FirstSecond( - Line::new( - coord!{x: 0.0_f32, y:0.0_f32}, - coord!{x: 6.0_f32, y:0.0_f32}, - ), - Line::new( - coord!{x: 6.0_f32, y:0.0_f32}, - coord!{x:10.0_f32, y:0.0_f32}, - ) - ))); + assert_eq!( + result, + Some(LineSplitResult::FirstSecond( + Line::new( + coord! {x: 0.0_f32, y:0.0_f32}, + coord! {x: 6.0_f32, y:0.0_f32}, + ), + Line::new( + coord! {x: 6.0_f32, y:0.0_f32}, + coord! {x:10.0_f32, y:0.0_f32}, + ) + )) + ); // simple y-axis aligned check let line = Line::new( - coord!{x:0.0_f32, y: 0.0_f32}, - coord!{x:0.0_f32, y:10.0_f32}, + coord! {x:0.0_f32, y: 0.0_f32}, + coord! {x:0.0_f32, y:10.0_f32}, ); let result = line.line_split(0.3); - assert_eq!(result, Some(LineSplitResult::FirstSecond( - Line::new( - coord!{x:0.0_f32, y:0.0_f32}, - coord!{x:0.0_f32, y:3.0_f32}, - ), - Line::new( - coord!{x:0.0_f32, y:3.0_f32}, - coord!{x:0.0_f32, y:10.0_f32}, - ) - ))); + assert_eq!( + result, + Some(LineSplitResult::FirstSecond( + Line::new(coord! {x:0.0_f32, y:0.0_f32}, coord! {x:0.0_f32, y:3.0_f32},), + Line::new( + coord! {x:0.0_f32, y:3.0_f32}, + coord! {x:0.0_f32, y:10.0_f32}, + ) + )) + ); // non_trivial check let line = Line::new( - coord!{x: 1.0_f32, y: 1.0_f32}, - coord!{x:10.0_f32, y:-10.0_f32}, + coord! {x: 1.0_f32, y: 1.0_f32}, + coord! {x:10.0_f32, y:-10.0_f32}, ); let split_point = line.start + line.delta() * 0.7; let result = line.line_split(0.7); - assert_eq!(result, Some(LineSplitResult::FirstSecond( - Line::new( - line.start, - split_point, - ), - Line::new( - split_point, - line.end, - ) - ))); + assert_eq!( + result, + Some(LineSplitResult::FirstSecond( + Line::new(line.start, split_point,), + Line::new(split_point, line.end,) + )) + ); } #[test] fn test_line_split_first() { // test one let line = Line::new( - coord!{x: 0.0_f32, y:0.0_f32}, - coord!{x:10.0_f32, y:0.0_f32}, + coord! {x: 0.0_f32, y:0.0_f32}, + coord! {x:10.0_f32, y:0.0_f32}, ); let result = line.line_split(1.0); assert_eq!(result, Some(LineSplitResult::First(line))); // Test numbers larger than one let line = Line::new( - coord!{x: 0.0_f32, y:0.0_f32}, - coord!{x:10.0_f32, y:0.0_f32}, + coord! {x: 0.0_f32, y:0.0_f32}, + coord! {x:10.0_f32, y:0.0_f32}, ); let result = line.line_split(2.0); assert_eq!(result, Some(LineSplitResult::First(line))); @@ -111,26 +111,25 @@ mod test{ fn test_line_split_second() { // test zero let line = Line::new( - coord!{x: 0.0_f32, y:0.0_f32}, - coord!{x:10.0_f32, y:0.0_f32}, + coord! {x: 0.0_f32, y:0.0_f32}, + coord! {x:10.0_f32, y:0.0_f32}, ); let result = line.line_split(0.0); assert_eq!(result, Some(LineSplitResult::Second(line))); // Test negative numbers let line = Line::new( - coord!{x: 0.0_f32, y:0.0_f32}, - coord!{x:10.0_f32, y:0.0_f32}, + coord! {x: 0.0_f32, y:0.0_f32}, + coord! {x:10.0_f32, y:0.0_f32}, ); let result = line.line_split(-2.0); assert_eq!(result, Some(LineSplitResult::Second(line))); } - // ============================================================================================= // Line::line_split_twice() // ============================================================================================= - + macro_rules! test_line_split_twice_helper{ ($a:expr, $b:expr, $enum_variant:ident, $(($x1:expr, $x2:expr)),*)=>{{ let line = Line::new( @@ -154,8 +153,15 @@ mod test{ } #[test] - fn test_line_split_twice(){ - test_line_split_twice_helper!(0.6, 0.8, FirstSecondThird, (0.0, 6.0), (6.0, 8.0), (8.0, 10.0)); + fn test_line_split_twice() { + test_line_split_twice_helper!( + 0.6, + 0.8, + FirstSecondThird, + (0.0, 6.0), + (6.0, 8.0), + (8.0, 10.0) + ); test_line_split_twice_helper!(0.6, 1.0, FirstSecond, (0.0, 6.0), (6.0, 10.0)); test_line_split_twice_helper!(0.6, 0.6, FirstThird, (0.0, 6.0), (6.0, 10.0)); test_line_split_twice_helper!(0.0, 0.6, SecondThird, (0.0, 6.0), (6.0, 10.0)); @@ -163,5 +169,4 @@ mod test{ test_line_split_twice_helper!(0.0, 1.0, Second, (0.0, 10.0)); test_line_split_twice_helper!(0.0, 0.0, Third, (0.0, 10.0)); } - -} \ No newline at end of file +} diff --git a/geo/src/algorithm/line_split/line_split_trait_impl_for_linestring.rs b/geo/src/algorithm/line_split/line_split_trait_impl_for_linestring.rs index 48b283e768..73581d064c 100644 --- a/geo/src/algorithm/line_split/line_split_trait_impl_for_linestring.rs +++ b/geo/src/algorithm/line_split/line_split_trait_impl_for_linestring.rs @@ -24,7 +24,7 @@ where let LineStringMeasurements { length_total, length_segments, - } = match measure_line_string(&self) { + } = match measure_line_string(self) { Some(x) => x, None => return None, }; @@ -88,7 +88,7 @@ where #[cfg(test)] mod test { - use geo_types::{line_string, coord}; + use geo_types::{coord, line_string}; use super::super::LineSplitTwiceResult; @@ -99,7 +99,7 @@ mod test { #[test] fn split() { - let line_string:LineString = line_string![ + let line_string: LineString = line_string![ (x:0.0, y:0.0), (x:1.0, y:0.0), (x:1.0, y:1.0), @@ -110,15 +110,15 @@ mod test { assert_eq!( line_string.line_split(0.5), Some(LineSplitResult::FirstSecond( - LineString::new(vec![line_string.0[0],line_string.0[1], slice_point]), - LineString::new(vec![slice_point, line_string.0[2],line_string.0[3]]) + LineString::new(vec![line_string.0[0], line_string.0[1], slice_point]), + LineString::new(vec![slice_point, line_string.0[2], line_string.0[3]]) )) ); } - + #[test] fn split_on_point() { - let line_string:LineString = line_string![ + let line_string: LineString = line_string![ (x:0.0, y:0.0), (x:1.0, y:0.0), (x:1.0, y:1.0), @@ -129,7 +129,7 @@ mod test { assert_eq!( line_string.line_split(0.5), Some(LineSplitResult::FirstSecond( - LineString::new(vec![line_string.0[0],line_string.0[1], slice_point]), + LineString::new(vec![line_string.0[0], line_string.0[1], slice_point]), LineString::new(vec![slice_point, line_string.0[3], line_string.0[4]]) )) ); @@ -137,7 +137,7 @@ mod test { #[test] fn split_half_way_through_last_segment() { - let line_string:LineString = line_string![ + let line_string: LineString = line_string![ (x:0.0, y:0.0), (x:1.0, y:0.0), (x:1.0, y:1.0), @@ -154,7 +154,7 @@ mod test { #[test] fn split_half_way_through_first_segment() { - let line_string:LineString = line_string![ + let line_string: LineString = line_string![ (x:0.0, y:0.0), (x:1.0, y:0.0), (x:1.0, y:1.0), @@ -171,7 +171,7 @@ mod test { #[test] fn split_first() { - let line_string:LineString = line_string![ + let line_string: LineString = line_string![ (x:0.0, y:0.0), (x:1.0, y:0.0), ]; @@ -183,7 +183,7 @@ mod test { #[test] fn split_second() { - let line_string:LineString = line_string![ + let line_string: LineString = line_string![ (x:0.0, y:0.0), (x:1.0, y:0.0), ]; @@ -193,13 +193,11 @@ mod test { ); } - - // ============================================================================================= // LineString::line_split_twice() // ============================================================================================= #[test] - fn split_twice_typical(){ + fn split_twice_typical() { // I think if we exhaustively check // - `Line::line_split_twice()` and // - `LineString::line_split()` @@ -207,7 +205,7 @@ mod test { // we don't need an exhaustive check for `LineString::line_split_twice()` // So I will just do a spot check for a typical case - let line_string:LineString = line_string![ + let line_string: LineString = line_string![ (x:0.0, y:0.0), (x:1.0, y:0.0), (x:1.0, y:1.0), @@ -234,4 +232,4 @@ mod test { ) ); } -} \ No newline at end of file +} diff --git a/geo/src/algorithm/line_split/line_split_twice_result.rs b/geo/src/algorithm/line_split/line_split_twice_result.rs index e42495cc41..0738a81a07 100644 --- a/geo/src/algorithm/line_split/line_split_twice_result.rs +++ b/geo/src/algorithm/line_split/line_split_twice_result.rs @@ -100,4 +100,4 @@ impl LineSplitTwiceResult { Self::FirstSecondThird(a, b, c) => (Some(a), Some(b), Some(c)), } } -} \ No newline at end of file +}