Skip to content

Commit d28df1f

Browse files
committed
Handle RBSKeyword types (#60)
1 parent 6d0174a commit d28df1f

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

rust/ruby-rbs/build.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
171171
)?;
172172
writeln!(file, " }}")?;
173173
}
174+
"rbs_keyword" => {
175+
writeln!(file, " pub fn {}(&self) -> RBSKeyword {{", field.name)?;
176+
writeln!(
177+
file,
178+
" RBSKeyword::new(self.parser, unsafe {{ (*self.pointer).{} }})",
179+
field.name
180+
)?;
181+
writeln!(file, " }}")?;
182+
}
174183
_ => eprintln!("Unknown field type: {}", field.c_type),
175184
}
176185
}

rust/ruby-rbs/src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,32 @@ impl RBSSymbol {
183183
}
184184
}
185185

186+
pub struct RBSKeyword {
187+
parser: *mut rbs_parser_t,
188+
pointer: *const rbs_keyword,
189+
}
190+
191+
impl RBSKeyword {
192+
pub fn new(parser: *mut rbs_parser_t, pointer: *const rbs_keyword) -> Self {
193+
Self { parser, pointer }
194+
}
195+
196+
pub fn name(&self) -> &[u8] {
197+
unsafe {
198+
let constant_ptr = rbs_constant_pool_id_to_constant(
199+
&(*self.parser).constant_pool,
200+
(*self.pointer).constant_id,
201+
);
202+
if constant_ptr.is_null() {
203+
panic!("Constant ID for keyword is not present in the pool");
204+
}
205+
206+
let constant = &*constant_ptr;
207+
std::slice::from_raw_parts(constant.start, constant.length)
208+
}
209+
}
210+
}
211+
186212
#[cfg(test)]
187213
mod tests {
188214
use super::*;

0 commit comments

Comments
 (0)