css_ast/functions/
symbols_function.rs

1use bumpalo::collections::Vec;
2use css_parse::{Function, T, function_set, keyword_set};
3use csskit_derives::{Parse, Peek, ToCursors, ToSpan, Visitable};
4
5use crate::types::Image;
6
7function_set!(pub struct SymbolsFunctionName "symbols");
8
9/// <https://drafts.csswg.org/css-counter-styles-3/#funcdef-symbols>
10///
11/// ```text,ignore
12/// symbols() = symbols( <symbols-type>? [ <string> | <image> ]+ )
13/// ```
14#[derive(Parse, Peek, ToCursors, ToSpan, Visitable, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
15#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
16#[visit]
17pub struct SymbolsFunction<'a>(Function<SymbolsFunctionName, (Option<SymbolsType>, Vec<'a, Symbol<'a>>)>);
18
19keyword_set!(
20	/// <https://drafts.csswg.org/css-counter-styles-3/#typedef-symbols-type>
21	///
22	/// ```text,ignore
23	/// <symbols-type> = cyclic | numeric | alphabetic | symbolic | fixed
24	/// ```
25	#[derive(Visitable)]
26	#[visit(skip)]
27	pub enum SymbolsType {
28		Cyclic: "cyclic",
29		Numeric: "numeric",
30		Alphabetic: "alphabetic",
31		Symbolic: "symbolic",
32		Fixed: "fixed",
33	}
34);
35
36/// <https://drafts.csswg.org/css-counter-styles-3/#funcdef-symbols>
37///
38/// A single Symbol from the `<symbols()>` syntax
39///
40/// ```text,ignore
41/// <string> | <image>
42/// ```
43#[derive(Parse, Peek, ToCursors, ToSpan, Visitable, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
44#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
45#[allow(clippy::large_enum_variant)] // TODO: Box or shrink Image
46#[visit(children)]
47pub enum Symbol<'a> {
48	#[visit(skip)]
49	String(T![String]),
50	Image(Image<'a>),
51}
52
53#[cfg(test)]
54mod tests {
55	use super::*;
56	use css_parse::assert_parse;
57
58	#[test]
59	fn size_test() {
60		assert_eq!(std::mem::size_of::<SymbolsFunction>(), 80);
61		assert_eq!(std::mem::size_of::<Symbol>(), 216);
62		assert_eq!(std::mem::size_of::<SymbolsType>(), 16);
63	}
64
65	#[test]
66	fn test_writes() {
67		assert_parse!(SymbolsFunction, "symbols(symbolic'+')");
68		assert_parse!(SymbolsFunction, "symbols(symbolic'*''†''‡')");
69	}
70}