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
pub mod contrib;
mod drawing;
mod macros;
mod position;
mod shapes;
pub mod style;

pub type Size = Vec2;

pub use crate::drawing::Drawing;
pub use crate::position::Rect;
pub use crate::shapes::{Shape, ShapeType};
pub use algebr::{vec2, Angle, Vec2};

pub mod shape {
    pub use crate::shapes::circle::Circle;
    pub use crate::shapes::embedded::EmbeddedDrawing;
    pub use crate::shapes::image::{Image, ImageFormat};
    pub use crate::shapes::line::{Line, LineBuilder};
    pub use crate::shapes::path::{Keypoint, Keypoints, Path};
    pub use crate::shapes::text::Text;
    pub use crate::style::{Color, Fill, Stroke, Style};
}

pub trait ShapeInteraction<T> {
    fn children_of(self, parent: T) -> Drawing;
    fn parent_of(self, children: T) -> Drawing;
}

impl<T, U> ShapeInteraction<U> for T
where
    T: Into<Shape>,
    U: Into<Shape>,
{
    fn children_of(self, parent: U) -> Drawing {
        let mut parent = Drawing::new(parent);
        parent.add(self);
        parent
    }

    fn parent_of(self, children: U) -> Drawing {
        let mut parent = Drawing::new(self);
        parent.add(children);
        parent
    }
}

impl<T> Into<Shape> for Vec<T>
where
    T: Into<Shape>,
{
    fn into(self) -> Shape {
        let shapes: Vec<Shape> = self.into_iter().map(Into::into).collect();

        let pos = shapes.iter().fold(Rect::new(), |r, s| r.union(s.pos));

        Shape {
            pos,
            style: None,
            shape_type: ShapeType::Drawing(shapes),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use shape::{Circle, Text};

    #[test]
    fn children() {
        let c = Circle::new();
        let t = Text::new("".to_owned());

        let drawing = c.parent_of(t);

        let ts = vec![Text::new("".to_owned()), Text::new("".to_owned())];
        drawing.parent_of(ts);
    }
}