Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ongoing metadata writer improvements and new command line tool #2319

Merged
merged 17 commits into from
Feb 2, 2023
Prev Previous commit
Next Next commit
writer
kennykerr committed Jan 31, 2023
commit af8fb99c6b3404151b9b886ad9554352eb586484
40 changes: 37 additions & 3 deletions crates/tools/riddle/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use metadata::writer;
use std::io::Read;
use syn::{parse::*, *};

@@ -64,15 +65,48 @@ impl Parse for Interface {
}
}

fn syn_to_writer(module: Module) -> Result<Vec<writer::Item>> {
//println!("{:#?}", &module);
let mut items = Vec::new();
module_to_writer(&module.name.to_string(), &module, &mut items)?;
Ok(items)
}

fn module_to_writer(namespace: &str, module: &Module, items: &mut Vec<writer::Item>) -> Result<()> {
for member in &module.members {
match member {
ModuleMember::Module(module) => module_to_writer(&format!("{namespace}.{}", module.name.to_string()), module, items)?,
ModuleMember::Interface(interface) => interface_to_writer(&namespace, interface, items)?,
}
}
Ok(())
}

fn interface_to_writer(namespace: &str, interface: &Interface, items: &mut Vec<writer::Item>) -> Result<()> {
let mut methods = Vec::new();

for method in &interface.methods {
methods.push(writer::Method::new(&method.sig.ident.to_string(), writer::Type::Void, vec![]));
}

items.push(writer::Interface::item(&namespace, &interface.name.to_string(), methods));
Ok(())
}

fn main() {
let filename = "crates/tools/riddle/src/test.ridl";
let filename = "crates/tools/riddle/src/test.rs";
let output = "crates/tools/riddle/src/test.winmd";

let mut file = std::fs::File::open(filename).expect("failed to open file");
let mut input = String::new();
file.read_to_string(&mut input).expect("failed to read file");

let result = parse_str::<Module>(&input);
let result = parse_str::<Module>(&input).and_then(syn_to_writer);
match result {
Ok(result) => println!("{result:#?}"),
Ok(items) => {
let buffer = writer::write("test", true, &items, &[]);
std::fs::write(output, buffer).expect("failed to write file");
}
Err(error) => {
let start = error.span().start();
println!("error: {error}\n --> {}:{:?}:{:?} ", filename, start.line, start.column);
Original file line number Diff line number Diff line change
@@ -3,8 +3,8 @@ mod Things {
fn Method(&self, p1: u8) -> f32;
}
mod More {
interface2 IOtherThing {
fn Method(&self, p1: u8) -> f32;
interface IOtherThing {
fn OtherMethod(&self, p1: u8) -> f32;
}
}
}