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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/// The fill tessellator's result type.
pub type TessellationResult = Result<(), TessellationError>;

/// An error that can happen while generating geometry.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum GeometryBuilderError {
    InvalidVertex,
    TooManyVertices,
}

#[cfg(feature = "std")]
impl core::fmt::Display for GeometryBuilderError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            GeometryBuilderError::InvalidVertex => {
                std::write!(f, "Invalid vertex")
            },
            GeometryBuilderError::TooManyVertices => {
                std::write!(f, "Too many vertices")
            }
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for GeometryBuilderError {}

/// Describes an unexpected error happening during tessellation.
///
/// If you run into one of these, please consider
/// [filing an issue](https://github.com/nical/lyon/issues/new).
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum InternalError {
    IncorrectActiveEdgeOrder(i16),
    InsufficientNumberOfSpans,
    InsufficientNumberOfEdges,
    MergeVertexOutside,
    InvalidNumberOfEdgesBelowVertex,
    ErrorCode(i16),
}

#[cfg(feature = "std")]
impl core::fmt::Display for InternalError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            InternalError::IncorrectActiveEdgeOrder(i) => {
                std::write!(f, "Incorrect active edge order ({i})")
            },
            InternalError::InsufficientNumberOfSpans => {
                std::write!(f, "Insufficient number of spans")
            },
            InternalError::InsufficientNumberOfEdges => {
                std::write!(f, "Insufficient number of edges")
            },
            InternalError::MergeVertexOutside => {
                std::write!(f, "Merge vertex is outside of the shape")
            },
            InternalError::InvalidNumberOfEdgesBelowVertex => {
                std::write!(f, "Unexpected number of edges below a vertex")
            },
            InternalError::ErrorCode(i) => {
                std::write!(f, "Error code: #{i}")
            },
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for InternalError {}

/// The fill tessellator's error enumeration.
#[derive(Clone, Debug, PartialEq)]
pub enum TessellationError {
    // TODO Parameter typo
    UnsupportedParamater(UnsupportedParamater),
    GeometryBuilder(GeometryBuilderError),
    Internal(InternalError),
}

#[cfg(feature = "std")]
impl core::fmt::Display for TessellationError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            TessellationError::UnsupportedParamater(e) => {
                std::write!(f, "Unsupported parameter: {e}")
            },
            TessellationError::GeometryBuilder(e) => {
                std::write!(f, "Geometry builder error: {e}")
            },
            TessellationError::Internal(e) => {
                std::write!(f, "Internal error: {e}")
            },
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for TessellationError {}

impl core::convert::From<GeometryBuilderError> for TessellationError {
    fn from(value: GeometryBuilderError) -> Self {
        Self::GeometryBuilder(value)
    }
}

impl core::convert::From<InternalError> for TessellationError {
    fn from(value: InternalError) -> Self {
        Self::Internal(value)
    }
}

#[derive(Clone, Debug, PartialEq)]
pub enum UnsupportedParamater {
    PositionIsNaN,
    ToleranceIsNaN,
}

#[cfg(feature = "std")]
impl core::fmt::Display for UnsupportedParamater {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            UnsupportedParamater::PositionIsNaN => {
                std::write!(f, "Position is not a number")
            },
            UnsupportedParamater::ToleranceIsNaN => {
                std::write!(f, "Tolerance threshold is not a number")
            },
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for UnsupportedParamater {}