1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::dot::DotString;
use std::borrow::Cow;

pub struct Point {
    pub x: f32,
    pub y: f32,
    pub z: Option<f32>,

    /// 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<f32>, 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()
        );
    }
}