From c95ff86e2c8fbdd8e0cf6550aadc7ffc676dcc16 Mon Sep 17 00:00:00 2001 From: seancarroll Date: Wed, 6 Jan 2021 21:11:57 -0600 Subject: trying to organize files and use declarations --- src/attributes/point.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/attributes/point.rs (limited to 'src/attributes/point.rs') diff --git a/src/attributes/point.rs b/src/attributes/point.rs new file mode 100644 index 0000000..819044e --- /dev/null +++ b/src/attributes/point.rs @@ -0,0 +1,55 @@ +use crate::dot::DotString; +use std::borrow::Cow; + +pub struct Point { + pub x: f32, + pub y: f32, + pub z: Option, + + /// specify that the node position should not change. + pub force_pos: bool, +} + +impl Point { + pub fn new_2d(x: f32, y: f32) -> Self { + Self::new(x, y, None, false) + } + + pub fn new_3d(x: f32, y: f32, z: f32) -> Self { + Self::new(x, y, Some(z), false) + } + + pub fn new(x: f32, y: f32, z: Option, force_pos: bool) -> Self { + Self { x, y, z, force_pos } + } +} + +impl<'a> DotString<'a> for Point { + fn dot_string(&self) -> Cow<'a, str> { + let mut slice = format!("{:.1},{:.1}", self.x, self.y); + if self.z.is_some() { + slice.push_str(format!(",{:.1}", self.z.unwrap()).as_str()); + } + if self.force_pos { + slice.push_str("!") + } + slice.into() + } +} + +#[cfg(test)] +mod test { + use crate::attributes::Point; + use crate::DotString; + + #[test] + fn dot_string() { + assert_eq!("1.0,2.0", Point::new_2d(1.0, 2.0).dot_string()); + assert_eq!("1.0,2.0,0.0", Point::new_3d(1.0, 2.0, 0.0).dot_string()); + assert_eq!("1.0,2.0!", Point::new(1.0, 2.0, None, true).dot_string()); + assert_eq!( + "1.0,2.0,0.0!", + Point::new(1.0, 2.0, Some(0.0), true).dot_string() + ); + } +} -- cgit debian/1.2.3+git2.25.1-1-2-gaceb0