Skip to content

Commit

Permalink
Merge pull request #174 from chrissimpkins/pointtype-traits
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissimpkins committed Sep 4, 2021
2 parents bc9ec60 + dbd5b75 commit 9c594e9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
28 changes: 28 additions & 0 deletions src/glyph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Self::Err> {
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))]
Expand Down
14 changes: 0 additions & 14 deletions src/glyph/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,17 +550,3 @@ impl FromStr for GlifVersion {
}
}
}

impl FromStr for PointType {
type Err = ErrorKind;
fn from_str(s: &str) -> Result<Self, Self::Err> {
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),
}
}
}
25 changes: 25 additions & 0 deletions src/glyph/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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();
}

0 comments on commit 9c594e9

Please sign in to comment.