From c02f2af5d46e09176b1203b4cc96e5849c5a47c8 Mon Sep 17 00:00:00 2001 From: Chris Simpkins Date: Wed, 1 Sep 2021 11:39:53 -0400 Subject: [PATCH 1/4] refactor PointType FromStr trait implementation to mod.rs module --- src/glyph/mod.rs | 14 ++++++++++++++ src/glyph/parse.rs | 14 -------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/glyph/mod.rs b/src/glyph/mod.rs index 1753c421..c57b51f2 100644 --- a/src/glyph/mod.rs +++ b/src/glyph/mod.rs @@ -366,6 +366,20 @@ pub enum PointType { QCurve, } +/// FromStr trait implementation for [`PointType`]. +impl std::str::FromStr for PointType { + type Err = ErrorKind; + fn from_str(s: &str) -> Result { + match s { + "move" => Ok(PointType::Move), + "line" => Ok(PointType::Line), + "offcurve" => Ok(PointType::OffCurve), + "curve" => Ok(PointType::Curve), + "qcurve" => Ok(PointType::QCurve), + _other => Err(ErrorKind::UnknownPointType), + } + } +} /// A 2D affine transformation. #[derive(Debug, Clone, Copy, PartialEq)] #[cfg_attr(feature = "druid", derive(Data))] diff --git a/src/glyph/parse.rs b/src/glyph/parse.rs index d50264ab..932e2025 100644 --- a/src/glyph/parse.rs +++ b/src/glyph/parse.rs @@ -550,17 +550,3 @@ impl FromStr for GlifVersion { } } } - -impl FromStr for PointType { - type Err = ErrorKind; - fn from_str(s: &str) -> Result { - match s { - "move" => Ok(PointType::Move), - "line" => Ok(PointType::Line), - "offcurve" => Ok(PointType::OffCurve), - "curve" => Ok(PointType::Curve), - "qcurve" => Ok(PointType::QCurve), - _other => Err(ErrorKind::UnknownPointType), - } - } -} From 56752d7f0e7d99d73edbb15807b3cb33c8554ff7 Mon Sep 17 00:00:00 2001 From: Chris Simpkins Date: Wed, 1 Sep 2021 11:40:32 -0400 Subject: [PATCH 2/4] add Display trait implementation for PointType --- src/glyph/mod.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/glyph/mod.rs b/src/glyph/mod.rs index c57b51f2..7e91e2f7 100644 --- a/src/glyph/mod.rs +++ b/src/glyph/mod.rs @@ -380,6 +380,20 @@ impl std::str::FromStr for PointType { } } } + +/// Display trait implementation for [`PointType`]. +impl std::fmt::Display for PointType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + PointType::Move => write!(f, "move"), + PointType::Line => write!(f, "line"), + PointType::OffCurve => write!(f, "offcurve"), + PointType::Curve => write!(f, "curve"), + PointType::QCurve => write!(f, "qcurve"), + } + } +} + /// A 2D affine transformation. #[derive(Debug, Clone, Copy, PartialEq)] #[cfg_attr(feature = "druid", derive(Data))] From 2209858af230d8ae48dfb3a8f2649c080f0424c5 Mon Sep 17 00:00:00 2001 From: Chris Simpkins Date: Fri, 3 Sep 2021 11:53:01 -0400 Subject: [PATCH 3/4] [glyph::tests] add Display, from_str tests on PointType enum also tests the PartialEq trait support --- src/glyph/tests.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/glyph/tests.rs b/src/glyph/tests.rs index 9650d564..a03091d9 100644 --- a/src/glyph/tests.rs +++ b/src/glyph/tests.rs @@ -2,6 +2,7 @@ use super::parse::parse_glyph; use super::*; use crate::write::QuoteChar; use std::path::PathBuf; +use std::str::FromStr; #[test] #[allow(clippy::float_cmp)] @@ -572,3 +573,27 @@ fn empty_contours() { assert_eq!(test2.components, vec![]); assert_eq!(test2.contours, vec![]); } + +#[test] +fn pointtype_display_trait() { + assert_eq!(format!("{}", PointType::Move), "move"); + assert_eq!(format!("{}", PointType::Line), "line"); + assert_eq!(format!("{}", PointType::OffCurve), "offcurve"); + assert_eq!(format!("{}", PointType::Curve), "curve"); + assert_eq!(format!("{}", PointType::QCurve), "qcurve"); +} + +#[test] +fn pointtype_from_str() { + assert!(PointType::from_str("move").unwrap() == PointType::Move); + assert!(PointType::from_str("line").unwrap() == PointType::Line); + assert!(PointType::from_str("offcurve").unwrap() == PointType::OffCurve); + assert!(PointType::from_str("curve").unwrap() == PointType::Curve); + assert!(PointType::from_str("qcurve").unwrap() == PointType::QCurve); +} + +#[test] +#[should_panic(expected = "UnknownPointType")] +fn pointtype_from_str_unknown_type() { + PointType::from_str("bogus").unwrap(); +} From dbd5b75b167efdcae18d08883dc21cf60aacee47 Mon Sep 17 00:00:00 2001 From: Chris Simpkins Date: Fri, 3 Sep 2021 12:17:59 -0400 Subject: [PATCH 4/4] consistent test function names --- src/glyph/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/glyph/tests.rs b/src/glyph/tests.rs index a03091d9..e30eb908 100644 --- a/src/glyph/tests.rs +++ b/src/glyph/tests.rs @@ -584,7 +584,7 @@ fn pointtype_display_trait() { } #[test] -fn pointtype_from_str() { +fn pointtype_from_str_trait() { assert!(PointType::from_str("move").unwrap() == PointType::Move); assert!(PointType::from_str("line").unwrap() == PointType::Line); assert!(PointType::from_str("offcurve").unwrap() == PointType::OffCurve);