-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bindgen: Overhaul the bindgen utility
It now generates the target language directly instead of generating a c header which then needs to be separately converted to the target language. It also supports generating multiple languages in the future.
- Loading branch information
Showing
15 changed files
with
400 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use crate::parse::{Enum, Source}; | ||
use heck::AsLowerCamelCase; | ||
use std::io::{self, Write}; | ||
|
||
pub(crate) fn generate(source: &Source, out: &mut dyn Write) -> io::Result<()> { | ||
for (name, value) in &source.enums { | ||
generate_enum(name, value, out)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn generate_enum(name: &str, value: &Enum, out: &mut dyn Write) -> io::Result<()> { | ||
writeln!(out, "enum {name} {{")?; | ||
|
||
for variant in &value.variants { | ||
writeln!(out, " {},", AsLowerCamelCase(&variant.name))?; | ||
} | ||
|
||
writeln!(out, " ;")?; | ||
writeln!(out)?; | ||
|
||
// decode | ||
writeln!(out, " static {} decode(int n) {{", name)?; | ||
writeln!(out, " switch (n) {{")?; | ||
|
||
for variant in &value.variants { | ||
writeln!( | ||
out, | ||
" case {}: return {}.{};", | ||
variant.value, | ||
name, | ||
AsLowerCamelCase(&variant.name) | ||
)?; | ||
} | ||
|
||
writeln!( | ||
out, | ||
" default: throw ArgumentError('invalid value: $n');" | ||
)?; | ||
writeln!(out, " }}")?; | ||
writeln!(out, " }}")?; | ||
writeln!(out)?; | ||
|
||
// encode | ||
writeln!(out, " int encode() {{")?; | ||
writeln!(out, " switch (this) {{")?; | ||
|
||
for variant in &value.variants { | ||
writeln!( | ||
out, | ||
" case {}.{}: return {};", | ||
name, | ||
AsLowerCamelCase(&variant.name), | ||
variant.value | ||
)?; | ||
} | ||
|
||
writeln!(out, " }}")?; | ||
writeln!(out, " }}")?; | ||
writeln!(out)?; | ||
|
||
writeln!(out, "}}")?; | ||
writeln!(out)?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,43 @@ | ||
use cbindgen::{Builder, EnumConfig, Language, RenameRule}; | ||
use std::path::Path; | ||
mod dart; | ||
mod parse; | ||
|
||
use clap::{Parser, ValueEnum}; | ||
use parse::{parse_file, Source}; | ||
use std::io; | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
// Generate the C bindings header | ||
|
||
#[cfg(feature = "env_logger")] | ||
env_logger::init(); | ||
|
||
let output_path = Path::new("target").join("bindings.h"); | ||
|
||
Builder::new() | ||
.with_config(cbindgen::Config { | ||
language: Language::C, | ||
enumeration: EnumConfig { | ||
rename_variants: RenameRule::CamelCase, | ||
..Default::default() | ||
}, | ||
..Default::default() | ||
}) | ||
.with_src(Path::new("ffi").join("src").join("lib.rs")) | ||
.with_src(Path::new("bridge").join("src").join("constants.rs")) | ||
.generate()? | ||
.write_to_file(output_path); | ||
let options = Options::parse(); | ||
|
||
let source_files = [ | ||
"bridge/src/protocol/mod.rs", | ||
"ffi/src/lib.rs", | ||
"lib/src/access_control/access_mode.rs", | ||
"lib/src/directory/entry_type.rs", | ||
]; | ||
let mut source = Source::new(); | ||
|
||
for source_file in source_files { | ||
parse_file(source_file, &mut source)?; | ||
} | ||
|
||
match options.language { | ||
Language::Dart => dart::generate(&source, &mut io::stdout())?, | ||
Language::Kotlin => todo!(), | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(about)] | ||
struct Options { | ||
/// Language to generate the bindings for | ||
#[arg(short, long)] | ||
language: Language, | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, ValueEnum)] | ||
enum Language { | ||
Dart, | ||
Kotlin, | ||
} |
Oops, something went wrong.