selma 0.0.7-x64-mingw-ucrt → 0.1.0-x64-mingw-ucrt

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,112 +0,0 @@
1
- use magnus::{exception, function, scan_args, Error, Module, Object, RModule, Value};
2
-
3
- #[derive(Clone, Debug)]
4
- #[magnus::wrap(class = "Selma::Selector")]
5
- pub struct SelmaSelector {
6
- match_element: Option<String>,
7
- match_text_within: Option<String>,
8
- ignore_text_within: Option<Vec<String>>,
9
- }
10
-
11
- impl SelmaSelector {
12
- fn new(args: &[Value]) -> Result<Self, Error> {
13
- let (match_element, match_text_within, rb_ignore_text_within) =
14
- Self::scan_parse_args(args)?;
15
-
16
- if match_element.is_none() && match_text_within.is_none() {
17
- return Err(Error::new(
18
- exception::arg_error(),
19
- "Neither `match_element` nor `match_text_within` option given",
20
- ));
21
- }
22
-
23
- // FIXME: not excited about this double parse work (`element!` does it too),
24
- // but at least we can bail ASAP if the CSS is invalid
25
- if match_element.is_some() {
26
- let css = match_element.as_ref().unwrap();
27
- if css.parse::<lol_html::Selector>().is_err() {
28
- return Err(Error::new(
29
- exception::arg_error(),
30
- format!("Could not parse `match_element` (`{css:?}`) as valid CSS"),
31
- ));
32
- }
33
- }
34
-
35
- if match_text_within.is_some() {
36
- let css = match_text_within.as_ref().unwrap();
37
- if css.parse::<lol_html::Selector>().is_err() {
38
- return Err(Error::new(
39
- exception::arg_error(),
40
- format!("Could not parse `match_text_within` (`{css:?}`) as valid CSS",),
41
- ));
42
- }
43
- }
44
-
45
- let ignore_text_within = match rb_ignore_text_within {
46
- None => None,
47
- Some(rb_ignore_text_within) => {
48
- let mut ignore_text_within = vec![];
49
- rb_ignore_text_within.iter().for_each(|i| {
50
- // TODO: test this against malice
51
- let ignore_text_within_tag_name = i.to_string();
52
- ignore_text_within.push(ignore_text_within_tag_name);
53
- });
54
- Some(ignore_text_within)
55
- }
56
- };
57
-
58
- Ok(Self {
59
- match_element,
60
- match_text_within,
61
- ignore_text_within,
62
- })
63
- }
64
-
65
- #[allow(clippy::let_unit_value)]
66
- fn scan_parse_args(
67
- args: &[Value],
68
- ) -> Result<(Option<String>, Option<String>, Option<Vec<String>>), Error> {
69
- let args = scan_args::scan_args(args)?;
70
- let _: () = args.required;
71
- let _: () = args.optional;
72
- let _: () = args.splat;
73
- let _: () = args.trailing;
74
- let _: () = args.block;
75
-
76
- let kw = scan_args::get_kwargs::<
77
- _,
78
- (),
79
- (Option<String>, Option<String>, Option<Vec<String>>),
80
- (),
81
- >(
82
- args.keywords,
83
- &[],
84
- &["match_element", "match_text_within", "ignore_text_within"],
85
- )?;
86
- let (match_element, match_text_within, rb_ignore_text_within) = kw.optional;
87
-
88
- Ok((match_element, match_text_within, rb_ignore_text_within))
89
- }
90
-
91
- pub fn match_element(&self) -> Option<String> {
92
- self.match_element.clone()
93
- }
94
-
95
- pub fn match_text_within(&self) -> Option<String> {
96
- self.match_text_within.clone()
97
- }
98
-
99
- pub fn ignore_text_within(&self) -> Option<Vec<String>> {
100
- self.ignore_text_within.clone()
101
- }
102
- }
103
-
104
- pub fn init(m_selma: RModule) -> Result<(), Error> {
105
- let c_selector = m_selma
106
- .define_class("Selector", Default::default())
107
- .expect("cannot define class Selma::Selector");
108
-
109
- c_selector.define_singleton_method("new", function!(SelmaSelector::new, -1))?;
110
-
111
- Ok(())
112
- }