Skip to content

Commit 1ca44c9

Browse files
Add helper functions to PathSegment (#47)
Hi Here his a tiny PR to add to helpers functions for `PathSegment`
1 parent 26c9a26 commit 1ca44c9

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

src/path.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,88 @@ pub enum PathSegment {
7272
},
7373
}
7474

75+
impl PathSegment {
76+
/// Returns `true` if the segment is absolute.
77+
///
78+
/// # Examples
79+
///
80+
/// ```
81+
/// use svgtypes::{PathParser, PathSegment};
82+
///
83+
/// let mut segments = Vec::new();
84+
/// for segment in PathParser::from("M10-20l30.1.5.1-20z") {
85+
/// segments.push(segment.unwrap());
86+
/// }
87+
///
88+
/// assert_eq!(segments[0].is_abs(), true);
89+
/// assert_eq!(segments[1].is_abs(), false);
90+
/// assert_eq!(segments[2].is_abs(), false);
91+
/// assert_eq!(segments[3].is_abs(), false);
92+
/// ```
93+
///
94+
pub fn is_abs(&self) -> bool {
95+
match *self {
96+
Self::MoveTo { abs, .. } => abs,
97+
Self::LineTo { abs, .. } => abs,
98+
Self::HorizontalLineTo { abs, .. } => abs,
99+
Self::VerticalLineTo { abs, .. } => abs,
100+
Self::CurveTo { abs, .. } => abs,
101+
Self::SmoothCurveTo { abs, .. } => abs,
102+
Self::Quadratic { abs, .. } => abs,
103+
Self::SmoothQuadratic { abs, .. } => abs,
104+
Self::EllipticalArc { abs, .. } => abs,
105+
Self::ClosePath { abs } => abs,
106+
}
107+
}
108+
109+
/// Returns the segment letter.
110+
///
111+
/// # Examples
112+
///
113+
/// ```
114+
/// use svgtypes::{PathParser, PathSegment};
115+
///
116+
/// let mut segments = Vec::new();
117+
/// for segment in PathParser::from("M10-20l30.1.5.1-20z") {
118+
/// segments.push(segment.unwrap());
119+
/// }
120+
///
121+
/// assert_eq!(segments[0].command(), b'M');
122+
/// assert_eq!(segments[1].command(), b'l');
123+
/// assert_eq!(segments[2].command(), b'l');
124+
/// assert_eq!(segments[3].command(), b'z');
125+
/// ```
126+
///
127+
pub fn command(&self) -> u8 {
128+
match self.is_abs() {
129+
true => match *self {
130+
Self::MoveTo { .. } => b'M',
131+
Self::LineTo { .. } => b'L',
132+
Self::HorizontalLineTo { .. } => b'H',
133+
Self::VerticalLineTo { .. } => b'V',
134+
Self::CurveTo { .. } => b'C',
135+
Self::SmoothCurveTo { .. } => b'S',
136+
Self::Quadratic { .. } => b'Q',
137+
Self::SmoothQuadratic { .. } => b'T',
138+
Self::EllipticalArc { .. } => b'A',
139+
Self::ClosePath { .. } => b'Z',
140+
},
141+
false => match *self {
142+
Self::MoveTo { .. } => b'm',
143+
Self::LineTo { .. } => b'l',
144+
Self::HorizontalLineTo { .. } => b'h',
145+
Self::VerticalLineTo { .. } => b'v',
146+
Self::CurveTo { .. } => b'c',
147+
Self::SmoothCurveTo { .. } => b's',
148+
Self::Quadratic { .. } => b'q',
149+
Self::SmoothQuadratic { .. } => b't',
150+
Self::EllipticalArc { .. } => b'a',
151+
Self::ClosePath { .. } => b'z',
152+
},
153+
}
154+
}
155+
}
156+
75157
/// A pull-based [path data] parser.
76158
///
77159
/// # Errors
@@ -574,6 +656,13 @@ mod tests {
574656
PathSegment::ClosePath { abs: true },
575657
PathSegment::HorizontalLineTo { abs: true, x: 10.0 }
576658
);
659+
660+
#[test]
661+
fn test_helper_functions(){
662+
assert!(PathSegment::MoveTo { abs: true, x: 0.0, y: 0.0 }.is_abs());
663+
assert!(!PathSegment::MoveTo { abs: false, x: 0.0, y: 0.0 }.is_abs()); // asserting false
664+
assert_eq!(PathSegment::MoveTo { abs: true, x: 0.0, y: 0.0 }.command(), b'M');
665+
}
577666
}
578667

579668
/// Representation of a simple path segment.

0 commit comments

Comments
 (0)