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
// Copyright 2018 Evgeniy Reizner
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use simplecss;

use svgtypes;
use svgtypes::xmlparser::{
    self,
    ErrorPos,
};

// TODO: split to Dom errors and Parser errors

/// SVG DOM errors.
#[derive(Fail, Debug)]
pub enum Error {
    /// If you want to use referenced element inside link attribute,
    /// such element must have a non-empty ID.
    #[fail(display = "the element must have an id")]
    ElementMustHaveAnId,

    /// Linked nodes can't reference each other or itself.
    ///
    /// # Examples
    ///
    /// ```text
    /// <linearGradient id="lg1" xlink:href="#lg2"/>
    /// <linearGradient id="lg2" xlink:href="#lg1"/>
    /// ```
    ///
    /// or
    ///
    /// ```text
    /// <linearGradient id="lg1" xlink:href="#lg1"/>
    /// ```
    #[fail(display = "element crosslink")]
    ElementCrosslink,

    /// Parsed document must have an `svg` element.
    #[fail(display = "the document does not have an SVG element")]
    NoSvgElement,

    /// Parsed document must have at least one node.
    #[fail(display = "the document does not have any nodes")]
    EmptyDocument,

    /// *svgdom* didn't support most of the CSS2 spec.
    #[fail(display = "unsupported CSS at {}", _0)]
    UnsupportedCSS(ErrorPos),

    /// Error during parsing of the CSS2.
    #[fail(display = "invalid CSS at {}", _0)]
    InvalidCSS(ErrorPos),

    /// ENTITY with XML Element data is not supported.
    #[fail(display = "unsupported ENTITY data at {}", _0)]
    UnsupportedEntity(ErrorPos),

    /// We don't support a \<paint\> type with a fallback value and a valid FuncIRI.
    ///
    /// # Examples
    ///
    /// ```text
    /// <linearGradient id="lg1"/>
    /// <rect fill="url(#lg1) red"/>
    /// ```
    #[fail(display = "valid FuncIRI(#{}) with fallback value is not supported", _0)]
    UnsupportedPaintFallback(String),

    // TODO: only `use`?
    /// We don't support `use` elements with a broken filter attribute.
    #[fail(display = "the 'use' element with a broken filter attribute('#{}') is not supported", _0)]
    BrokenFuncIri(String),

    /// Failed to find an attribute, which must be set, during post-processing.
    #[fail(display = "attribute '{}' is missing in the '{}' element", _0, _1)]
    MissingAttribute(String, String),

    /// Error during attribute value parsing.
    #[fail(display = "{}", _0)]
    SvgTypesError(svgtypes::Error),

    /// An XML stream error.
    #[fail(display = "{}", _0)]
    XmlError(xmlparser::Error),

    /// simplecss errors.
    #[fail(display = "{:?}", _0)]
    CssError(simplecss::Error),
}

impl From<xmlparser::Error> for Error {
    fn from(value: xmlparser::Error) -> Error {
        Error::XmlError(value)
    }
}

impl From<svgtypes::Error> for Error {
    fn from(value: svgtypes::Error) -> Error {
        Error::SvgTypesError(value)
    }
}

impl From<simplecss::Error> for Error {
    fn from(value: simplecss::Error) -> Error {
        Error::CssError(value)
    }
}

/// A specialized `Result` type where the error is hard-wired to [`Error`].
///
/// [`Error`]: enum.Error.html
pub(crate) type Result<T> = ::std::result::Result<T, Error>;