summaryrefslogtreecommitdiff
path: root/src/attributes/spline_type.rs
diff options
context:
space:
mode:
authorseancarroll <seanc28@gmail.com>2021-01-06 21:11:57 -0600
committerseancarroll <seanc28@gmail.com>2021-01-06 21:11:57 -0600
commitc95ff86e2c8fbdd8e0cf6550aadc7ffc676dcc16 (patch)
treecfc0db9248e14f2ea675cf3b6dc67bf7243d1701 /src/attributes/spline_type.rs
parent0c8eb45449e578cae1f27e93df3f9cc92ba68219 (diff)
downloaddotavious-c95ff86e2c8fbdd8e0cf6550aadc7ffc676dcc16.zip
trying to organize files and use declarations
Diffstat (limited to 'src/attributes/spline_type.rs')
-rw-r--r--src/attributes/spline_type.rs110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/attributes/spline_type.rs b/src/attributes/spline_type.rs
new file mode 100644
index 0000000..259496a
--- /dev/null
+++ b/src/attributes/spline_type.rs
@@ -0,0 +1,110 @@
+use crate::attributes::point::Point;
+use crate::dot::DotString;
+use std::borrow::Cow;
+
+/// The number of points in the list must be equivalent to 1 mod 3; note that this is not checked.
+/// TODO: should we check?
+pub struct SplineType {
+ pub start: Option<Point>,
+ pub end: Option<Point>,
+ pub spline_points: Vec<Point>,
+}
+
+impl<'a> DotString<'a> for SplineType {
+ fn dot_string(&self) -> Cow<'a, str> {
+ let mut dot_string = String::from("");
+
+ if let Some(end) = &self.end {
+ dot_string.push_str(format!("e,{:.1},{:.1} ", end.x, end.y).as_str());
+ }
+
+ if let Some(start) = &self.start {
+ dot_string.push_str(format!("s,{:.1},{:.1} ", start.x, start.y).as_str());
+ }
+
+ let mut iter = self.spline_points.iter();
+ let first = iter.next().unwrap();
+ dot_string.push_str(format!("{}", first.dot_string()).as_str());
+ for point in iter {
+ dot_string.push_str(" ");
+ dot_string.push_str(format!("{}", point.dot_string()).as_str());
+ }
+
+ dot_string.into()
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use crate::attributes::{Point, SplineType};
+ use crate::DotString;
+
+ #[test]
+ fn spline_type() {
+ let spline_type = SplineType {
+ end: None,
+ start: None,
+ spline_points: vec![
+ Point::new_2d(0.0, 0.0),
+ Point::new_2d(1.0, 1.0),
+ Point::new_2d(1.0, -1.0),
+ ],
+ };
+
+ assert_eq!("0.0,0.0 1.0,1.0 1.0,-1.0", spline_type.dot_string());
+ }
+
+ #[test]
+ fn spline_type_end() {
+ let spline_type = SplineType {
+ end: Some(Point::new_2d(2.0, 0.0)),
+ start: None,
+ spline_points: vec![
+ Point::new_2d(0.0, 0.0),
+ Point::new_2d(1.0, 1.0),
+ Point::new_2d(1.0, -1.0),
+ ],
+ };
+
+ assert_eq!(
+ "e,2.0,0.0 0.0,0.0 1.0,1.0 1.0,-1.0",
+ spline_type.dot_string()
+ );
+ }
+
+ #[test]
+ fn spline_type_start() {
+ let spline_type = SplineType {
+ end: None,
+ start: Some(Point::new_2d(-1.0, 0.0)),
+ spline_points: vec![
+ Point::new_2d(0.0, 0.0),
+ Point::new_2d(1.0, 1.0),
+ Point::new_2d(1.0, -1.0),
+ ],
+ };
+
+ assert_eq!(
+ "s,-1.0,0.0 0.0,0.0 1.0,1.0 1.0,-1.0",
+ spline_type.dot_string()
+ );
+ }
+
+ #[test]
+ fn spline_type_complete() {
+ let spline_type = SplineType {
+ end: Some(Point::new_2d(2.0, 0.0)),
+ start: Some(Point::new_2d(-1.0, 0.0)),
+ spline_points: vec![
+ Point::new_2d(0.0, 0.0),
+ Point::new_2d(1.0, 1.0),
+ Point::new_2d(1.0, -1.0),
+ ],
+ };
+
+ assert_eq!(
+ "e,2.0,0.0 s,-1.0,0.0 0.0,0.0 1.0,1.0 1.0,-1.0",
+ spline_type.dot_string()
+ );
+ }
+}