summaryrefslogtreecommitdiff
path: root/src/attributes/viewport.rs
blob: f5bfe92460d9624e2bb35e508894e364c7fda802 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use crate::attributes::Point;
use crate::DotString;
use std::borrow::Cow;

pub struct ViewPort {
    pub width: f32,
    pub height: f32,
    pub zoom: f32,
    pub focus: Option<FocusType>
}

impl ViewPort {
    pub fn new(width: f32, height: f32, zoom: Option<f32>, focus: Option<FocusType>) -> Self {
        Self {
            width,
            height,
            zoom: zoom.unwrap_or(1 as f32),
            focus
        }
    }

    pub fn new_point(width: f32, height: f32, zoom: Option<f32>, x: f32, y: f32) -> Self {
        Self {
            width,
            height,
            zoom: zoom.unwrap_or(1 as f32),
            focus: Some(FocusType::Point(Point::new_2d(x, y)))
        }
    }

    pub fn new_node(width: f32, height: f32, zoom: Option<f32>, node: String) -> Self {
        Self {
            width,
            height,
            zoom: zoom.unwrap_or(1 as f32),
            focus: Some(FocusType::Node(node))
        }
    }
}

impl<'a> DotString<'a> for ViewPort {
    fn dot_string(&self) -> Cow<'a, str> {
        let mut dot_string = String::from("");
        dot_string.push_str(
            format!("{:.1},{:.1},{:.1}",
                    self.width, self.height, self.zoom
        ).as_str());

        if let Some(focus) = &self.focus {
            match focus {
                FocusType::Point(p) => {
                    dot_string.push_str(format!(",{}", p.dot_string()).as_str());
                },
                FocusType::Node(n) => {
                    dot_string.push_str(format!(",'{}'", n).as_str());
                },
            }
        }

        dot_string.into()
    }
}

pub enum FocusType {
    Point(Point),
    Node(String)
}


#[cfg(test)]
mod test {
    use crate::attributes::{ViewPort};
    use crate::DotString;

    #[test]
    fn viewport_dot_string() {
        assert_eq!(
            "1.0,2.0,1.0",
            ViewPort::new(1.0, 2.0, None, None).dot_string()
        );
    }

    #[test]
    fn viewport_zoom_dot_string() {
        assert_eq!(
            "1.0,2.0,3.0",
            ViewPort::new(1.0, 2.0, Some(3.0), None).dot_string()
        );
    }

    #[test]
    fn viewport_point_focus_dot_string() {
        assert_eq!(
            "1.0,2.0,3.0,5.0,10.0",
            ViewPort::new_point(1.0, 2.0, Some(3.0), 5.0, 10.0).dot_string()
        );
    }

    #[test]
    fn viewport_node_focus_dot_string() {
        assert_eq!(
            "1.0,2.0,3.0,'2.8 BSD'",
            ViewPort::new_node(
                1.0, 2.0, Some(3.0), String::from("2.8 BSD")
            ).dot_string()
        );
    }
}