Skip to main content

bromlife/
lib.rs

1// Copyright (C) 2026  gemmaro
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
16use bromlife_sys::{
17    oml_event, oml_event_is_body, oml_event_is_empty, oml_event_is_ignored, oml_event_is_label,
18    oml_event_is_left, oml_event_is_normal, oml_event_is_null, oml_event_is_right,
19    oml_event_is_vocab, oml_event_is_word, oml_event_label_len, oml_event_label_offset,
20    oml_event_len, oml_event_offset, oml_next, oml_parser,
21};
22
23pub struct Parser<'a> {
24    parser: oml_parser,
25    input: &'a str,
26}
27
28impl<'a> Parser<'a> {
29    pub fn new(input: &'a str) -> Self {
30        let len = input.len();
31        let bytes = input.as_ptr();
32        Self {
33            parser: unsafe { oml_parser(bytes, len) },
34            input,
35        }
36    }
37}
38
39impl<'a> Iterator for Parser<'a> {
40    type Item = Event<'a>;
41
42    fn next(&mut self) -> Option<Self::Item> {
43        let &mut Parser { mut parser, input } = self;
44        let event = unsafe { oml_next(&mut parser) };
45        if unsafe { oml_event_is_empty(event) } {
46            return None;
47        }
48        Some(Event { event, input })
49    }
50}
51
52pub struct Event<'a> {
53    event: oml_event,
54    input: &'a str,
55}
56
57impl<'a> Event<'a> {
58    pub fn len(&self) -> usize {
59        let Event { event, input: _ } = self;
60        unsafe { oml_event_len(*event) }
61    }
62
63    pub fn is_empty(&self) -> bool {
64        self.len() == 0
65    }
66
67    pub fn offset(&self) -> usize {
68        let Event { event, input: _ } = self;
69        unsafe { oml_event_offset(*event) }
70    }
71
72    pub fn as_str(&self) -> &str {
73        let Event { event: _, input } = self;
74        let offset = self.offset();
75        &input[offset..offset + self.len()]
76    }
77
78    pub fn is_null(&self) -> bool {
79        let Event { event, input: _ } = self;
80        unsafe { oml_event_is_null(*event) }
81    }
82
83    pub fn is_left(&self) -> bool {
84        let Event { event, input: _ } = self;
85        unsafe { oml_event_is_left(*event) }
86    }
87
88    pub fn is_right(&self) -> bool {
89        let Event { event, input: _ } = self;
90        unsafe { oml_event_is_right(*event) }
91    }
92
93    pub fn is_body(&self) -> bool {
94        let Event { event, input: _ } = self;
95        unsafe { oml_event_is_body(*event) }
96    }
97
98    pub fn is_normal(&self) -> bool {
99        let Event { event, input: _ } = self;
100        unsafe { oml_event_is_normal(*event) }
101    }
102
103    pub fn is_ignored(&self) -> bool {
104        let Event { event, input: _ } = self;
105        unsafe { oml_event_is_ignored(*event) }
106    }
107
108    pub fn is_vocab(&self) -> bool {
109        let Event { event, input: _ } = self;
110        unsafe { oml_event_is_vocab(*event) }
111    }
112
113    pub fn is_word(&self) -> bool {
114        let Event { event, input: _ } = self;
115        unsafe { oml_event_is_word(*event) }
116    }
117
118    pub fn is_label(&self) -> bool {
119        let Event { event, input: _ } = self;
120        unsafe { oml_event_is_label(*event) }
121    }
122
123    fn _label_len(&self) -> usize {
124        let Event { event, input: _ } = self;
125        unsafe { oml_event_label_len(*event) }
126    }
127
128    pub fn label_len(&self) -> Option<usize> {
129        if !self.is_left() || !self.is_normal() {
130            return None;
131        }
132        Some(self._label_len())
133    }
134
135    fn _label_offset(&self) -> usize {
136        let Event { event, input: _ } = self;
137        unsafe { oml_event_label_offset(*event) }
138    }
139
140    pub fn label_offset(&self) -> Option<usize> {
141        if !self.is_left() || !self.is_normal() {
142            return None;
143        }
144        Some(self._label_offset())
145    }
146
147    pub fn label(&self) -> Option<&str> {
148        if !self.is_left() || !self.is_normal() {
149            return None;
150        }
151        let Event { event: _, input } = self;
152        let offset = self._label_offset();
153        Some(&input[offset..offset + self._label_len()])
154    }
155}