css_ast/functions/
leader_function.rs

1use css_parse::{Function, T, function_set, keyword_set};
2use csskit_derives::{Parse, Peek, ToCursors, ToSpan, Visitable};
3
4function_set!(pub struct LeaderFunctionName "leader");
5
6/// <https://drafts.csswg.org/css-content-3/#leader-function>
7///
8/// ```text,ignore
9/// leader() = leader( <leader-type> )
10/// <leader-type> = dotted | solid | space | <string>
11/// ```
12#[derive(Parse, Peek, ToCursors, ToSpan, Visitable, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
14#[visit(self)]
15pub struct LeaderFunction(Function<LeaderFunctionName, LeaderType>);
16
17keyword_set!(pub enum LeaderTypeKeywords { Dotted: "dotted", Solid: "solid", Space: "space" });
18
19// https://drafts.csswg.org/css-content-3/#typedef-leader-type
20// <leader-type> = dotted | solid | space | <string>
21#[derive(ToSpan, Parse, Peek, ToCursors, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
22#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
23pub enum LeaderType {
24	Keyword(LeaderTypeKeywords),
25	String(T![String]),
26}
27
28#[cfg(test)]
29mod tests {
30	use super::*;
31	use css_parse::{assert_parse, assert_parse_error};
32
33	#[test]
34	fn size_test() {
35		assert_eq!(std::mem::size_of::<LeaderFunction>(), 44);
36		assert_eq!(std::mem::size_of::<LeaderType>(), 16);
37	}
38
39	#[test]
40	fn test_writes() {
41		assert_parse!(LeaderType, "dotted");
42		assert_parse!(LeaderType, "'.'");
43		assert_parse!(LeaderType, "'abc'");
44		assert_parse!(LeaderFunction, "leader(dotted)");
45		assert_parse!(LeaderFunction, "leader('.')");
46	}
47
48	#[test]
49	fn test_errors() {
50		assert_parse_error!(LeaderType, "foo");
51		assert_parse_error!(LeaderFunction, "leader(foo)");
52	}
53}