rspack_lightningcss/values/
resolution.rs1use super::length::serialize_dimension;
4use super::number::{CSSNumber, NumericRange};
5use crate::compat::Feature;
6use crate::error::{ParserError, PrinterError};
7use crate::traits::{Parse, ParseNumeric, ToCss};
8#[cfg(feature = "visitor")]
9use crate::visitor::Visit;
10use cssparser::*;
11
12#[derive(Debug, Clone, PartialEq)]
14#[cfg_attr(feature = "visitor", derive(Visit))]
15#[cfg_attr(feature = "visitor", visit(visit_resolution, RESOLUTIONS))]
16#[cfg_attr(
17 feature = "serde",
18 derive(serde::Serialize, serde::Deserialize),
19 serde(tag = "type", content = "value", rename_all = "kebab-case")
20)]
21#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
22#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
23pub enum Resolution {
24 Dpi(CSSNumber),
26 Dpcm(CSSNumber),
28 Dppx(CSSNumber),
30}
31
32impl<'i> Parse<'i> for Resolution {
33 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
34 Self::parse_with_range(input, NumericRange::All)
35 }
36}
37
38impl<'i> ParseNumeric<'i> for Resolution {
39 fn parse_with_range<'t>(
40 input: &mut Parser<'i, 't>,
41 range: NumericRange,
42 ) -> Result<Self, ParseError<'i, ParserError<'i>>> {
43 let location = input.current_source_location();
45 match *input.next()? {
46 Token::Dimension { value, ref unit, .. } => {
47 let value = range.check(value, location)?;
48 match_ignore_ascii_case! { unit,
49 "dpi" => Ok(Resolution::Dpi(value)),
50 "dpcm" => Ok(Resolution::Dpcm(value)),
51 "dppx" | "x" => Ok(Resolution::Dppx(value)),
52 _ => Err(location.new_unexpected_token_error(Token::Ident(unit.clone())))
53 }
54 }
55 ref t => Err(location.new_unexpected_token_error(t.clone())),
56 }
57 }
58}
59
60impl<'i> TryFrom<&Token<'i>> for Resolution {
61 type Error = ();
62
63 fn try_from(token: &Token) -> Result<Self, Self::Error> {
64 match token {
65 Token::Dimension { value, ref unit, .. } => match_ignore_ascii_case! { unit,
66 "dpi" => Ok(Resolution::Dpi(*value)),
67 "dpcm" => Ok(Resolution::Dpcm(*value)),
68 "dppx" | "x" => Ok(Resolution::Dppx(*value)),
69 _ => Err(()),
70 },
71 _ => Err(()),
72 }
73 }
74}
75
76impl ToCss for Resolution {
77 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
78 let (value, unit) = match self {
79 Resolution::Dpi(dpi) => (*dpi, "dpi"),
80 Resolution::Dpcm(dpcm) => (*dpcm, "dpcm"),
81 Resolution::Dppx(dppx) => {
82 if dest.state().targets.current.is_compatible(Feature::XResolutionUnit) {
83 (*dppx, "x")
84 } else {
85 (*dppx, "dppx")
86 }
87 }
88 };
89
90 serialize_dimension(value, unit, dest)
91 }
92}
93
94impl std::ops::Add<CSSNumber> for Resolution {
95 type Output = Self;
96
97 fn add(self, other: CSSNumber) -> Resolution {
98 match self {
99 Resolution::Dpi(dpi) => Resolution::Dpi(dpi + other),
100 Resolution::Dpcm(dpcm) => Resolution::Dpcm(dpcm + other),
101 Resolution::Dppx(dppx) => Resolution::Dppx(dppx + other),
102 }
103 }
104}