1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#[cfg(not(target_arch = "wasm32"))]
mod native {
    use crate::node::Node;
    use std::{borrow::Cow, convert::TryFrom};

    #[derive(Clone)]
    pub struct TreeCursor<'a> {
        pub(crate) inner: tree_sitter::TreeCursor<'a>,
    }

    impl<'a> TreeCursor<'a> {
        #[inline]
        pub fn field_id(&self) -> Option<u16> {
            self.inner.field_id()
        }

        #[inline]
        pub fn field_name(&self) -> Option<Cow<str>> {
            self.inner.field_name().map(Into::into)
        }

        #[inline]
        pub fn goto_first_child(&mut self) -> bool {
            self.inner.goto_first_child()
        }

        #[inline]
        pub fn goto_first_child_for_byte(&mut self, index: u32) -> Option<u32> {
            let index = index as usize;
            self.inner
                .goto_first_child_for_byte(index)
                .map(|i| u32::try_from(i).unwrap())
        }

        #[inline]
        pub fn goto_next_sibling(&mut self) -> bool {
            self.inner.goto_next_sibling()
        }

        #[inline]
        pub fn goto_parent(&mut self) -> bool {
            self.inner.goto_parent()
        }

        #[inline]
        pub fn node(&self) -> Node<'a> {
            let inner = self.inner.node();
            Node { inner }
        }

        #[inline]
        pub fn reset(&mut self, node: Node<'a>) {
            self.inner.reset(node.inner);
        }
    }

    impl<'a> From<tree_sitter::TreeCursor<'a>> for TreeCursor<'a> {
        #[inline]
        fn from(inner: tree_sitter::TreeCursor<'a>) -> Self {
            Self { inner }
        }
    }

    impl<'a> std::panic::RefUnwindSafe for TreeCursor<'a> {
    }

    impl<'a> Unpin for TreeCursor<'a> {
    }

    impl<'a> std::panic::UnwindSafe for TreeCursor<'a> {
    }
}

#[cfg(not(target_arch = "wasm32"))]
pub use native::*;

#[cfg(target_arch = "wasm32")]
mod wasm {
    use crate::node::Node;
    use std::borrow::Cow;

    #[derive(Clone)]
    pub struct TreeCursor<'a> {
        pub(crate) inner: web_tree_sitter::TreeCursor,
        pub(crate) phantom: std::marker::PhantomData<&'a ()>,
    }

    impl<'a> TreeCursor<'a> {
        #[inline]
        pub fn field_id(&self) -> Option<u16> {
            self.inner.current_field_id()
        }

        #[inline]
        pub fn field_name(&self) -> Option<Cow<str>> {
            self.inner
                .current_field_name()
                .map(|name| From::<String>::from(name.into()))
        }

        #[inline]
        pub fn goto_first_child(&mut self) -> bool {
            self.inner.goto_first_child()
        }

        #[inline]
        pub fn goto_next_sibling(&mut self) -> bool {
            self.inner.goto_next_sibling()
        }

        #[inline]
        pub fn goto_parent(&mut self) -> bool {
            self.inner.goto_parent()
        }

        #[inline]
        pub fn node(&self) -> Node<'a> {
            let inner = self.inner.current_node();
            let phantom = std::marker::PhantomData;
            Node { inner, phantom }
        }

        #[inline]
        pub fn reset(&mut self, node: Node<'a>) {
            self.inner.reset(&node.inner);
        }
    }

    impl<'a> Drop for TreeCursor<'a> {
        fn drop(&mut self) {
            self.inner.delete();
        }
    }

    impl<'a> From<web_tree_sitter::TreeCursor> for TreeCursor<'a> {
        #[inline]
        fn from(inner: web_tree_sitter::TreeCursor) -> Self {
            let phantom = std::marker::PhantomData;
            Self { inner, phantom }
        }
    }

    impl<'a> std::panic::RefUnwindSafe for TreeCursor<'a> {
    }

    impl<'a> Unpin for TreeCursor<'a> {
    }

    impl<'a> std::panic::UnwindSafe for TreeCursor<'a> {
    }
}

#[cfg(target_arch = "wasm32")]
pub use wasm::*;