Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion rust/ruby-rbs/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ struct Config {
nodes: Vec<Node>,
}

#[derive(Debug, Deserialize)]
struct NodeField {
name: String,
c_type: String,
}

#[derive(Debug, Deserialize)]
struct Node {
name: String,
rust_name: String,
fields: Option<Vec<NodeField>>,
}

fn main() -> Result<(), Box<dyn Error>> {
Expand Down Expand Up @@ -40,7 +47,31 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {

// TODO: Go through all of the nodes and generate the structs to back them up
for node in &config.nodes {
writeln!(file, "pub struct {} {{}}\n", node.rust_name)?;
writeln!(file, "pub struct {} {{", node.rust_name)?;
if let Some(fields) = &node.fields {
for field in fields {
match field.c_type.as_str() {
"rbs_string" => writeln!(file, " {}: *const rbs_string_t,", field.name)?,
_ => eprintln!("Unknown field type: {}", field.c_type),
}
}
}
writeln!(file, "}}\n")?;

writeln!(file, "impl {} {{", node.rust_name)?;
if let Some(fields) = &node.fields {
for field in fields {
match field.c_type.as_str() {
"rbs_string" => {
writeln!(file, " pub fn {}(&self) -> RBSString {{", field.name)?;
writeln!(file, " RBSString::new(self.{})", field.name)?;
writeln!(file, " }}")?;
}
_ => eprintln!("Unknown field type: {}", field.c_type),
}
}
}
writeln!(file, "}}\n")?;
}

// Generate the Node enum to wrap all of the structs
Expand Down
18 changes: 18 additions & 0 deletions rust/ruby-rbs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
use ruby_rbs_sys::bindings::*;

pub struct RBSString {
pointer: *const rbs_string_t,
}

impl RBSString {
pub fn new(pointer: *const rbs_string_t) -> Self {
Self { pointer }
}

pub fn as_bytes(&self) -> &[u8] {
unsafe {
let s = *self.pointer;
std::slice::from_raw_parts(s.start as *const u8, s.end.offset_from(s.start) as usize)
}
}
}
Loading