css_ast/functions/
string_function.rs

1use css_parse::{Function, T, function_set, keyword_set};
2use csskit_derives::{Parse, Peek, ToCursors, ToSpan, Visitable};
3
4function_set!(pub struct StringFunctionName "string");
5
6/// <https://drafts.csswg.org/css-content-3/#string-function>
7///
8/// ```text,ignore
9/// string() = string( <custom-ident> , [ first | start | last | first-except ]? )
10/// ```
11#[derive(Parse, Peek, ToCursors, ToSpan, Visitable, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
13#[visit(self)]
14pub struct StringFunction(Function<StringFunctionName, (T![Ident], Option<T![,]>, Option<StringKeywords>)>);
15
16keyword_set!(
17	pub enum StringKeywords {
18		First: "first",
19		Start: "start",
20		Last: "last",
21		FirstExcept: "first-except"
22	}
23);
24
25#[cfg(test)]
26mod tests {
27	use super::*;
28	use css_parse::{assert_parse, assert_parse_error};
29
30	#[test]
31	fn size_test() {
32		assert_eq!(std::mem::size_of::<StringFunction>(), 72);
33		assert_eq!(std::mem::size_of::<StringKeywords>(), 16);
34	}
35
36	#[test]
37	fn test_writes() {
38		assert_parse!(StringFunction, "string(foo)");
39		assert_parse!(StringFunction, "string(foo,first)");
40	}
41
42	#[test]
43	fn test_errors() {
44		assert_parse_error!(StringFunction, "string(foo bar)");
45	}
46}