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
use cssparser::*;
use crate::traits::{Parse, ToCss};
use crate::printer::Printer;
use super::length::serialize_dimension;
use crate::error::{ParserError, PrinterError};
#[derive(Debug, Clone, PartialEq)]
pub enum Resolution {
Dpi(f32),
Dpcm(f32),
Dppx(f32)
}
impl<'i> Parse<'i> for Resolution {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let location = input.current_source_location();
match *input.next()? {
Token::Dimension { value, ref unit, .. } => {
match_ignore_ascii_case! { unit,
"dpi" => Ok(Resolution::Dpi(value)),
"dpcm" => Ok(Resolution::Dpcm(value)),
"dppx" | "x" => Ok(Resolution::Dppx(value)),
_ => Err(location.new_unexpected_token_error(Token::Ident(unit.clone())))
}
}
ref t => Err(location.new_unexpected_token_error(t.clone())),
}
}
}
impl ToCss for Resolution {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> where W: std::fmt::Write {
let (value, unit) = match self {
Resolution::Dpi(dpi) => (*dpi, "dpi"),
Resolution::Dpcm(dpcm) => (*dpcm, "dpcm"),
Resolution::Dppx(dppx) => (*dppx, "x")
};
serialize_dimension(value, unit, dest)
}
}
impl std::ops::Add<f32> for Resolution {
type Output = Self;
fn add(self, other: f32) -> Resolution {
match self {
Resolution::Dpi(dpi) => Resolution::Dpi(dpi + other),
Resolution::Dpcm(dpcm) => Resolution::Dpcm(dpcm + other),
Resolution::Dppx(dppx) => Resolution::Dppx(dppx + other)
}
}
}