Skip to content

Commit

Permalink
feat: impl Default for points and paths
Browse files Browse the repository at this point in the history
  • Loading branch information
tirithen committed Jan 29, 2025
1 parent ea07713 commit cc77f7b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
27 changes: 26 additions & 1 deletion src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,33 @@ impl<P: PointScaler> From<Vec<[f64; 2]>> for Path<P> {

#[cfg(test)]
mod test {
use crate::Deci;

use super::*;

#[test]
fn test_default() {
let path: Path = Path::default();
assert_eq!(path.len(), 0);
}

#[test]
fn test_default_deci_precision() {
let path = Path::<Deci>::default();
assert_eq!(path.len(), 0);
}

#[test]
fn test_default_as_struct_field() {
#[derive(Default)]
struct Foo {
path: Path,
}

let path = Foo::default();
assert_eq!(path.path.len(), 0);
}

#[test]
fn test_negative_inflate_removing_imploded_paths() {
let path: Path = vec![(0.0, 0.0), (5.0, 0.0), (5.0, 6.0), (0.0, 6.0)].into();
Expand All @@ -405,7 +430,7 @@ mod test {

#[test]
fn test_from_custom_scaler() {
#[derive(Debug, Clone, Copy, PartialEq, Hash)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Hash)]
struct CustomScaler;

impl PointScaler for CustomScaler {
Expand Down
29 changes: 27 additions & 2 deletions src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
/// let paths_from_single_vec: Paths = vec![(0.0, 0.0), (5.0, 0.0), (5.0, 6.0), (0.0, 6.0)].into();
/// let paths_from_vec_of_vecs: Paths = vec![vec![(0.0, 0.0), (5.0, 0.0), (5.0, 6.0), (0.0, 6.0)]].into();
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down Expand Up @@ -386,8 +386,33 @@ impl<P: PointScaler> From<Vec<Path<P>>> for Paths<P> {

#[cfg(test)]
mod test {
use crate::Deci;

use super::*;

#[test]
fn test_default() {
let paths: Paths = Paths::default();
assert_eq!(paths.len(), 0);
}

#[test]
fn test_default_deci_precision() {
let paths = Paths::<Deci>::default();
assert_eq!(paths.len(), 0);
}

#[test]
fn test_default_as_struct_field() {
#[derive(Default)]
struct Foo {
paths: Paths,
}

let paths = Foo::default();
assert_eq!(paths.paths.len(), 0);
}

#[test]
fn test_from() {
let paths = Paths::<Centi>::from(vec![(0.4, 0.0), (5.0, 1.0)]);
Expand All @@ -405,7 +430,7 @@ mod test {

#[test]
fn test_from_custom_scaler() {
#[derive(Debug, Clone, Copy, PartialEq, Hash)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Hash)]
struct CustomScaler;

impl PointScaler for CustomScaler {
Expand Down
12 changes: 6 additions & 6 deletions src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use clipper2c_sys::ClipperPoint64;
/// The default multiplier is `Centi`, and others are provided by the library,
/// but if needed the user can create a custom scaler struct that implements
/// `PointScaler`.
pub trait PointScaler: Clone + Copy + PartialEq + std::hash::Hash {
pub trait PointScaler: Default + Clone + Copy + PartialEq + std::hash::Hash {
/// The point multiplier. This is set to a custom value when implementing
/// the `PointScaler` trait.
const MULTIPLIER: f64;
Expand All @@ -25,31 +25,31 @@ pub trait PointScaler: Clone + Copy + PartialEq + std::hash::Hash {
}

/// No scaling.
#[derive(Debug, Copy, Clone, PartialEq, Hash)]
#[derive(Debug, Default, Copy, Clone, PartialEq, Hash)]
pub struct One;

impl PointScaler for One {
const MULTIPLIER: f64 = 1.0;
}

/// Scale by 10.
#[derive(Debug, Copy, Clone, PartialEq, Hash)]
#[derive(Debug, Default, Copy, Clone, PartialEq, Hash)]
pub struct Deci;

impl PointScaler for Deci {
const MULTIPLIER: f64 = 10.0;
}

/// Scale by 100. This is the default.
#[derive(Debug, Copy, Clone, PartialEq, Hash)]
#[derive(Debug, Default, Copy, Clone, PartialEq, Hash)]
pub struct Centi;

impl PointScaler for Centi {
const MULTIPLIER: f64 = 100.0;
}

/// Scale by 1000.
#[derive(Debug, Copy, Clone, PartialEq, Hash)]
#[derive(Debug, Default, Copy, Clone, PartialEq, Hash)]
pub struct Milli;

impl PointScaler for Milli {
Expand Down Expand Up @@ -229,7 +229,7 @@ mod test {

#[test]
fn test_point_custom_scaler() {
#[derive(Debug, Clone, Copy, PartialEq, Hash)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Hash)]
struct CustomScaler;

impl PointScaler for CustomScaler {
Expand Down

0 comments on commit cc77f7b

Please sign in to comment.