Skip to main content

ruby_rbs_sys/
lib.rs

1#![allow(
2    clippy::useless_transmute,
3    clippy::missing_safety_doc,
4    clippy::ptr_offset_with_cast,
5    dead_code,
6    non_camel_case_types,
7    non_upper_case_globals,
8    non_snake_case
9)]
10pub mod bindings {
11    include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
12}
13
14#[cfg(test)]
15mod tests {
16    use super::bindings::*;
17
18    use rbs_encoding_type_t::RBS_ENCODING_UTF_8;
19
20    #[test]
21    fn test_rbs_parser_bindings() {
22        let rbs_code = r#"
23                class User
24                  attr_reader name: String
25                  def initialize: (String) -> void
26                end
27
28                module Authentication
29                  def authenticate: (String, String) -> bool
30                end
31            "#;
32
33        let bytes = rbs_code.as_bytes();
34        let start_ptr = bytes.as_ptr() as *const i8;
35        let end_ptr = unsafe { start_ptr.add(bytes.len()) } as *const i8;
36
37        let rbs_string = unsafe { rbs_string_new(start_ptr, end_ptr) };
38        let encoding_ptr =
39            unsafe { &rbs_encodings[RBS_ENCODING_UTF_8 as usize] as *const rbs_encoding_t };
40        let parser = unsafe { rbs_parser_new(rbs_string, encoding_ptr, 0, bytes.len() as i32) };
41
42        let mut signature: *mut rbs_signature_t = std::ptr::null_mut();
43        let result = unsafe { rbs_parse_signature(parser, &mut signature) };
44
45        assert!(result, "Failed to parse RBS signature");
46        assert!(!signature.is_null(), "Signature should not be null");
47
48        unsafe { rbs_parser_free(parser) };
49    }
50}