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
use crate::geometry::{LineSegment, Point, Region, Shapelike, ShapelikeError};

#[derive(Clone, Debug, PartialEq)]
pub enum Shape {
    Point(Point),
    Region(Region),
    LineSegment(LineSegment),
}

// TODO: write a derive macro to write out this boilerplate
impl Shapelike for Shape {
    fn get_center(&self) -> Point {
        match self {
            Shape::Point(point) => point.get_center(),
            Shape::LineSegment(line) => line.get_center(),
            Shape::Region(region) => region.get_center(),
        }
    }

    fn get_dimension(&self) -> usize {
        match self {
            Shape::Point(point) => point.get_dimension(),
            Shape::LineSegment(line) => line.get_dimension(),
            Shape::Region(region) => region.get_dimension(),
        }
    }

    fn get_min_bounding_region(&self) -> Region {
        match self {
            Shape::Point(point) => point.get_min_bounding_region(),
            Shape::LineSegment(line) => line.get_min_bounding_region(),
            Shape::Region(region) => region.get_min_bounding_region(),
        }
    }

    fn get_area(&self) -> f64 {
        match self {
            Shape::Point(point) => point.get_area(),
            Shape::LineSegment(line) => line.get_area(),
            Shape::Region(region) => region.get_area(),
        }
    }

    fn get_min_distance(&self, other: &Shape) -> Result<f64, ShapelikeError> {
        match self {
            Shape::Point(point) => point.get_min_distance(other),
            Shape::LineSegment(line) => line.get_min_distance(other),
            Shape::Region(region) => region.get_min_distance(other),
        }
    }
}

impl Into<Shape> for Point {
    fn into(self) -> Shape {
        Shape::Point(self)
    }
}

impl Into<Shape> for LineSegment {
    fn into(self) -> Shape {
        Shape::LineSegment(self)
    }
}

impl Into<Shape> for Region {
    fn into(self) -> Shape {
        Shape::Region(self)
    }
}