1
use std::fmt;
2

            
3
use crate::error::InternalRenderingError;
4

            
5
/// An enumeration of errors that can occur during filter primitive rendering.
6
#[derive(Debug, Clone)]
7
pub enum FilterError {
8
    /// The filter was passed invalid input (the `in` attribute).
9
    InvalidInput,
10
    /// The filter was passed an invalid parameter.
11
    InvalidParameter(String),
12
    /// The filter input surface has an unsuccessful status.
13
    BadInputSurfaceStatus(cairo::Error),
14
    /// A Cairo error.
15
    ///
16
    /// This means that either a failed intermediate surface creation or bad intermediate surface
17
    /// status.
18
    CairoError(cairo::Error),
19
    /// Error from the rendering backend.
20
    Rendering(InternalRenderingError),
21
    /// A lighting filter input surface is too small.
22
    LightingInputTooSmall,
23
}
24

            
25
/// Errors that can occur while resolving a `FilterSpec`.
26
#[derive(Debug)]
27
pub enum FilterResolveError {
28
    /// An `uri(#foo)` reference does not point to a `<filter>` element.
29
    ReferenceToNonFilterElement,
30
    /// A lighting filter has none or multiple light sources.
31
    InvalidLightSourceCount,
32
    /// Child node was in error.
33
    ChildNodeInError,
34
}
35

            
36
impl fmt::Display for FilterError {
37
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38
        match *self {
39
            FilterError::InvalidInput => write!(f, "invalid value of the `in` attribute"),
40
            FilterError::InvalidParameter(ref s) => write!(f, "invalid parameter value: {s}"),
41
            FilterError::BadInputSurfaceStatus(ref status) => {
42
                write!(f, "invalid status of the input surface: {status}")
43
            }
44
            FilterError::CairoError(ref status) => write!(f, "Cairo error: {status}"),
45
            FilterError::Rendering(ref e) => write!(f, "Rendering error: {e}"),
46
            FilterError::LightingInputTooSmall => write!(
47
                f,
48
                "lighting filter input surface is too small (less than 2×2 pixels)"
49
            ),
50
        }
51
    }
52
}
53

            
54
impl fmt::Display for FilterResolveError {
55
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56
        match *self {
57
            FilterResolveError::ReferenceToNonFilterElement => {
58
                write!(f, "reference to a non-filter element")
59
            }
60
            FilterResolveError::InvalidLightSourceCount => write!(f, "invalid light source count"),
61
            FilterResolveError::ChildNodeInError => write!(f, "child node was in error"),
62
        }
63
    }
64
}
65

            
66
impl From<cairo::Error> for FilterError {
67
    #[inline]
68
    fn from(x: cairo::Error) -> Self {
69
        FilterError::CairoError(x)
70
    }
71
}
72

            
73
impl From<InternalRenderingError> for FilterError {
74
    #[inline]
75
    fn from(e: InternalRenderingError) -> Self {
76
        FilterError::Rendering(e)
77
    }
78
}