diff --git a/src/glyph/mod.rs b/src/glyph/mod.rs index 1753c421..7e91e2f7 100644 --- a/src/glyph/mod.rs +++ b/src/glyph/mod.rs @@ -366,6 +366,34 @@ 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), + } + } +} + +/// 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))] 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), - } - } -} diff --git a/src/glyph/tests.rs b/src/glyph/tests.rs index 9650d564..e30eb908 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_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); + 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(); +}