lewp_css/domain/units/
unit_from_str_error.rs

1// This file is part of css. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT. No part of predicator, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
2// Copyright © 2017 The developers of css. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT.
3
4use {
5    crate::domain::numbers::CssNumberConversionError,
6    std::{
7        error::Error,
8        fmt::{self, Display, Formatter},
9        num::ParseFloatError,
10    },
11};
12
13#[derive(Debug, Clone, Eq, PartialEq)]
14pub enum UnitFromStrError {
15    Float(ParseFloatError),
16    Conversion(CssNumberConversionError),
17    InvalidDimension,
18}
19
20impl Display for UnitFromStrError {
21    #[inline(always)]
22    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
23        use self::UnitFromStrError::*;
24
25        match *self
26        {
27            Float(ref error) => write!(f, "Could not parse string to CssFloat because parsing it as a float caused '{}'", error),
28            Conversion(ref error) => write!(f, "Could not parse string to CssFloat because converting it from a parsed float caused '{}'", error),
29            InvalidDimension => write!(f, "Could not parse string to CssFloat because it had an invalid dimension"),
30        }
31    }
32}
33
34impl Error for UnitFromStrError {
35    #[inline(always)]
36    fn description(&self) -> &str {
37        use self::UnitFromStrError::*;
38
39        match *self {
40            Float(_) => "float error",
41            Conversion(_) => "conversion error",
42            InvalidDimension => "invalid dimension",
43        }
44    }
45
46    #[inline(always)]
47    fn cause(&self) -> Option<&dyn Error> {
48        use self::UnitFromStrError::*;
49
50        match *self {
51            Float(ref error) => Some(error),
52            Conversion(ref error) => Some(error),
53            InvalidDimension => None,
54        }
55    }
56}
57
58impl From<ParseFloatError> for UnitFromStrError {
59    #[inline(always)]
60    fn from(error: ParseFloatError) -> Self {
61        UnitFromStrError::Float(error)
62    }
63}
64
65impl From<CssNumberConversionError> for UnitFromStrError {
66    #[inline(always)]
67    fn from(error: CssNumberConversionError) -> Self {
68        UnitFromStrError::Conversion(error)
69    }
70}