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
use super::length::serialize_dimension;
use super::number::CSSNumber;
use crate::compat::Feature;
use crate::error::{ParserError, PrinterError};
use crate::printer::Printer;
use crate::traits::{Parse, ToCss};
use cssparser::*;
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(tag = "type", content = "value", rename_all = "kebab-case")
)]
pub enum Resolution {
Dpi(CSSNumber),
Dpcm(CSSNumber),
Dppx(CSSNumber),
}
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) => {
if let Some(targets) = dest.targets {
if Feature::XResolutionUnit.is_compatible(targets) {
(*dppx, "x")
} else {
(*dppx, "dppx")
}
} else {
(*dppx, "x")
}
}
};
serialize_dimension(value, unit, dest)
}
}
impl std::ops::Add<CSSNumber> for Resolution {
type Output = Self;
fn add(self, other: CSSNumber) -> Resolution {
match self {
Resolution::Dpi(dpi) => Resolution::Dpi(dpi + other),
Resolution::Dpcm(dpcm) => Resolution::Dpcm(dpcm + other),
Resolution::Dppx(dppx) => Resolution::Dppx(dppx + other),
}
}
}