topiary_tree_sitter_facade/
query_capture.rs

1#[cfg(not(target_arch = "wasm32"))]
2mod native {
3    use crate::node::Node;
4    use std::borrow::Cow;
5
6    #[derive(Clone)]
7    pub struct QueryCapture<'a> {
8        pub(crate) inner: tree_sitter::QueryCapture<'a>,
9    }
10
11    impl QueryCapture<'_> {
12        #[inline]
13        pub fn node(&self) -> Node<'_> {
14            self.inner.node.into()
15        }
16
17        #[inline]
18        pub fn name<'s>(&self, capture_names: &'s [&str]) -> Cow<'s, str> {
19            let index: usize = self.inner.index as usize;
20            Cow::Borrowed(capture_names[index])
21        }
22    }
23
24    impl std::fmt::Debug for QueryCapture<'_> {
25        fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
26            std::fmt::Debug::fmt(&self.inner, fmt)
27        }
28    }
29
30    impl<'a> From<&tree_sitter::QueryCapture<'a>> for QueryCapture<'a> {
31        #[inline]
32        fn from(inner: &tree_sitter::QueryCapture<'a>) -> Self {
33            Self { inner: *inner }
34        }
35    }
36
37    impl<'tree> From<tree_sitter::QueryCapture<'tree>> for QueryCapture<'tree> {
38        #[inline]
39        fn from(inner: tree_sitter::QueryCapture<'tree>) -> Self {
40            Self { inner }
41        }
42    }
43
44    impl std::panic::RefUnwindSafe for QueryCapture<'_> {}
45
46    impl Unpin for QueryCapture<'_> {}
47
48    impl std::panic::UnwindSafe for QueryCapture<'_> {}
49}
50
51#[cfg(not(target_arch = "wasm32"))]
52pub use native::*;
53
54#[cfg(target_arch = "wasm32")]
55mod wasm {
56    use crate::node::Node;
57    use std::borrow::Cow;
58
59    #[derive(Clone)]
60    pub struct QueryCapture<'a> {
61        pub(crate) inner: topiary_web_tree_sitter_sys::QueryCapture,
62        pub(crate) phantom: std::marker::PhantomData<&'a ()>,
63    }
64
65    impl<'a> QueryCapture<'a> {
66        #[inline]
67        pub fn node(&self) -> Node {
68            self.inner.node().into()
69        }
70
71        #[inline]
72        pub fn name(&self, _capture_names: &[&str]) -> Cow<str> {
73            Cow::Owned(self.inner.name().as_string().unwrap())
74        }
75    }
76
77    impl<'a> std::fmt::Debug for QueryCapture<'a> {
78        fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
79            std::fmt::Debug::fmt(&self.inner, fmt)
80        }
81    }
82
83    impl<'a> From<topiary_web_tree_sitter_sys::QueryCapture> for QueryCapture<'a> {
84        #[inline]
85        fn from(inner: topiary_web_tree_sitter_sys::QueryCapture) -> Self {
86            let phantom = std::marker::PhantomData;
87            Self { inner, phantom }
88        }
89    }
90
91    impl<'a> std::panic::RefUnwindSafe for QueryCapture<'a> {}
92
93    impl<'a> Unpin for QueryCapture<'a> {}
94
95    impl<'a> std::panic::UnwindSafe for QueryCapture<'a> {}
96}
97
98#[cfg(target_arch = "wasm32")]
99pub use wasm::*;