diff --git a/rust/ruby-rbs/build.rs b/rust/ruby-rbs/build.rs index beec0238e..d105f247f 100644 --- a/rust/ruby-rbs/build.rs +++ b/rust/ruby-rbs/build.rs @@ -171,6 +171,15 @@ fn generate(config: &Config) -> Result<(), Box> { )?; writeln!(file, " }}")?; } + "rbs_keyword" => { + writeln!(file, " pub fn {}(&self) -> RBSKeyword {{", field.name)?; + writeln!( + file, + " RBSKeyword::new(self.parser, unsafe {{ (*self.pointer).{} }})", + field.name + )?; + writeln!(file, " }}")?; + } _ => eprintln!("Unknown field type: {}", field.c_type), } } diff --git a/rust/ruby-rbs/src/lib.rs b/rust/ruby-rbs/src/lib.rs index 13fb8a9ea..20069ebf9 100644 --- a/rust/ruby-rbs/src/lib.rs +++ b/rust/ruby-rbs/src/lib.rs @@ -183,6 +183,32 @@ impl RBSSymbol { } } +pub struct RBSKeyword { + parser: *mut rbs_parser_t, + pointer: *const rbs_keyword, +} + +impl RBSKeyword { + pub fn new(parser: *mut rbs_parser_t, pointer: *const rbs_keyword) -> Self { + Self { parser, pointer } + } + + pub fn name(&self) -> &[u8] { + unsafe { + let constant_ptr = rbs_constant_pool_id_to_constant( + &(*self.parser).constant_pool, + (*self.pointer).constant_id, + ); + if constant_ptr.is_null() { + panic!("Constant ID for keyword is not present in the pool"); + } + + let constant = &*constant_ptr; + std::slice::from_raw_parts(constant.start, constant.length) + } + } +} + #[cfg(test)] mod tests { use super::*;