topiary_tree_sitter_facade/
query_predicate.rs

1#[cfg(not(target_arch = "wasm32"))]
2mod native {
3    use std::borrow::Cow;
4
5    pub struct QueryPredicate<'query> {
6        pub(crate) inner: &'query tree_sitter::QueryPredicate,
7    }
8
9    impl QueryPredicate<'_> {
10        #[inline]
11        pub fn operator(&self) -> Cow<'_, str> {
12            Cow::Borrowed(&self.inner.operator)
13        }
14
15        #[inline]
16        pub fn args(&self) -> Vec<String> {
17            let args: Vec<_> = self
18                .inner
19                .args
20                .iter()
21                .map(|s| match s {
22                    tree_sitter::QueryPredicateArg::String(s) => s.to_string(),
23                    _ => {
24                        unimplemented!("Only string predicate arguments are currently implemented.")
25                    }
26                })
27                .collect();
28
29            args
30        }
31    }
32
33    impl std::fmt::Debug for QueryPredicate<'_> {
34        fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
35            std::fmt::Debug::fmt(&self.inner, fmt)
36        }
37    }
38
39    impl<'query> From<&'query tree_sitter::QueryPredicate> for QueryPredicate<'query> {
40        #[inline]
41        fn from(inner: &'query tree_sitter::QueryPredicate) -> Self {
42            Self { inner }
43        }
44    }
45
46    impl std::panic::RefUnwindSafe for QueryPredicate<'_> {}
47
48    impl Unpin for QueryPredicate<'_> {}
49
50    impl std::panic::UnwindSafe for QueryPredicate<'_> {}
51}
52
53#[cfg(not(target_arch = "wasm32"))]
54pub use native::*;
55
56#[cfg(target_arch = "wasm32")]
57mod wasm {
58    use std::borrow::Cow;
59    use wasm_bindgen::JsCast;
60
61    pub struct QueryPredicate {
62        pub(crate) inner: topiary_web_tree_sitter_sys::QueryPredicate,
63    }
64
65    impl QueryPredicate {
66        #[inline]
67        pub fn operator(&self) -> Cow<'_, str> {
68            Cow::Owned(self.inner.operator().as_string().unwrap())
69        }
70
71        #[inline]
72        pub fn args(&self) -> Vec<String> {
73            let args: Vec<_> = self
74                .inner
75                .operands()
76                .iter()
77                .cloned()
78                .map(|value| {
79                    let arg =
80                        value.unchecked_into::<topiary_web_tree_sitter_sys::QueryPredicateArg>();
81                    arg.value().as_string().unwrap()
82                })
83                .collect();
84
85            args
86        }
87    }
88
89    impl std::fmt::Debug for QueryPredicate {
90        fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
91            std::fmt::Debug::fmt(&self.inner, fmt)
92        }
93    }
94
95    impl From<topiary_web_tree_sitter_sys::QueryPredicate> for QueryPredicate {
96        #[inline]
97        fn from(inner: topiary_web_tree_sitter_sys::QueryPredicate) -> Self {
98            Self { inner }
99        }
100    }
101
102    impl std::panic::RefUnwindSafe for QueryPredicate {}
103
104    impl Unpin for QueryPredicate {}
105
106    impl std::panic::UnwindSafe for QueryPredicate {}
107}
108
109#[cfg(target_arch = "wasm32")]
110pub use wasm::*;