css_ast/rules/
font_face.rs

1use crate::{Computed, StyleValue};
2use css_parse::{
3	AtRule, Cursor, DeclarationList, DeclarationValue, NoPreludeAllowed, Parser, Peek, Result as ParserResult,
4	atkeyword_set, keyword_set,
5};
6use csskit_derives::{Parse, Peek, ToCursors, ToSpan, Visitable};
7
8atkeyword_set!(pub struct AtFontFaceKeyword "font-face");
9
10// https://drafts.csswg.org/css-fonts/#font-face-rule
11#[derive(Parse, Peek, ToSpan, ToCursors, Visitable, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
13#[cfg_attr(feature = "css_feature_data", derive(::csskit_derives::ToCSSFeature), css_feature("css.at-rules.font-face"))]
14#[visit]
15pub struct FontFaceRule<'a>(pub AtRule<AtFontFaceKeyword, NoPreludeAllowed, FontFaceRuleBlock<'a>>);
16
17#[derive(Parse, Peek, ToSpan, ToCursors, Visitable, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
18#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
19#[visit(children)]
20pub struct FontFaceRuleBlock<'a>(DeclarationList<'a, FontFaceRuleStyleValue<'a>>);
21
22keyword_set!(pub enum FontFaceRulePropertyId {
23	AscentOverride: "ascent-override",
24	DescentOverride: "descent-override",
25	FontDisplay: "font-display",
26	FontFamily: "font-family",
27	FontFeatureSettings: "font-feature-settings",
28	FontLanguageOverride: "font-language-override",
29	FontNamedInstance: "font-named-instance",
30	FontStyle: "font-style",
31	FontVariationSettings: "font-variation-settings",
32	FontWeight: "font-weight",
33	FontWidth: "font-width",
34	LineGapOverride: "line-gap-override",
35	Src: "src",
36	UnicodeRange: "unicode-range",
37});
38
39#[derive(ToSpan, ToCursors, Visitable, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
40#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
41#[visit(children)]
42struct FontFaceRuleStyleValue<'a>(StyleValue<'a>);
43
44impl<'a> DeclarationValue<'a> for FontFaceRuleStyleValue<'a> {
45	type ComputedValue = Computed<'a>;
46
47	fn valid_declaration_name(p: &Parser, c: Cursor) -> bool {
48		FontFaceRulePropertyId::peek(p, c)
49	}
50
51	fn is_unknown(&self) -> bool {
52		self.0.is_unknown()
53	}
54
55	fn is_initial(&self) -> bool {
56		self.0.is_initial()
57	}
58
59	fn is_inherit(&self) -> bool {
60		self.0.is_inherit()
61	}
62
63	fn is_unset(&self) -> bool {
64		self.0.is_unset()
65	}
66
67	fn is_revert(&self) -> bool {
68		self.0.is_revert()
69	}
70
71	fn is_revert_layer(&self) -> bool {
72		self.0.is_revert_layer()
73	}
74
75	fn needs_computing(&self) -> bool {
76		self.0.needs_computing()
77	}
78
79	fn parse_declaration_value(p: &mut Parser<'a>, name: Cursor) -> ParserResult<Self> {
80		Ok(Self(StyleValue::parse_declaration_value(p, name)?))
81	}
82}
83
84#[cfg(test)]
85mod tests {
86	use super::*;
87
88	#[test]
89	fn size_test() {
90		assert_eq!(std::mem::size_of::<FontFaceRule>(), 96);
91		assert_eq!(std::mem::size_of::<FontFaceRuleStyleValue>(), 296);
92		assert_eq!(std::mem::size_of::<FontFaceRuleBlock>(), 64);
93	}
94
95	#[test]
96	fn test_writes() {
97		//assert_parse!(FontFaceRule, "@font-face {}");
98	}
99}