Skip to content

Generate method groups in rust? #96

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

Closed
wants to merge 7 commits into from
Closed
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Fork notes

This fork tries to extend the code generation for a particular use case.
The goal is to generate actual C# classes based on function name prefixes.

# csbindgen
[![Crates](https://img.shields.io/crates/v/csbindgen.svg)](https://crates.io/crates/csbindgen) [![Api Rustdoc](https://img.shields.io/badge/api-rustdoc-blue)](https://docs.rs/csbindgen)

Expand Down
1 change: 1 addition & 0 deletions csbindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ repository = "https://github.com/Cysharp/csbindgen/"
[dependencies]
syn = { version = "2.0.68", features = ["full", "parsing"] }
regex = "1.10.5"
convert_case = "0.6.0"
58 changes: 51 additions & 7 deletions csbindgen/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::convert::identity;
use std::path::PathBuf;
use std::{
error::Error,
fs::{File, OpenOptions},
io::{self, Write},
path::Path,
};
use std::convert::identity;

use crate::{generate, GenerateKind};

Expand Down Expand Up @@ -37,6 +37,14 @@ pub struct BindgenOptions {
pub csharp_file_header: String,
pub csharp_file_footer: String,
pub always_included_types: Vec<String>,
pub csharp_method_groups: Vec<MethodGroup>,
}

#[derive(Debug, PartialEq, Eq, Hash)]
pub struct MethodGroup {
pub rust_prefix: String,
pub csharp_class: String,
pub rust_type: String,
}

impl Default for Builder {
Expand Down Expand Up @@ -66,6 +74,7 @@ impl Default for Builder {
csharp_file_header: "".to_string(),
csharp_file_footer: "".to_string(),
always_included_types: vec![],
csharp_method_groups: vec![],
},
}
}
Expand All @@ -78,7 +87,9 @@ impl Builder {

/// Add an input .rs file(such as generated from bindgen) to generate binding.
pub fn input_bindgen_file<T: AsRef<Path>>(mut self, input_bindgen_file: T) -> Builder {
self.options.input_bindgen_files.push(input_bindgen_file.as_ref().to_path_buf());
self.options
.input_bindgen_files
.push(input_bindgen_file.as_ref().to_path_buf());
self
}

Expand All @@ -99,9 +110,13 @@ impl Builder {
/// Adds a list of types that will always be considered to be included in the
/// generated bindings, even if not part of any function signature
pub fn always_included_types<I, S>(mut self, always_included_types: I) -> Builder
where I: IntoIterator<Item = S>, S: ToString
where
I: IntoIterator<Item = S>,
S: ToString,
{
self.options.always_included_types.extend(always_included_types.into_iter().map(|v| v.to_string()));
self.options
.always_included_types
.extend(always_included_types.into_iter().map(|v| v.to_string()));
self
}

Expand Down Expand Up @@ -214,17 +229,27 @@ impl Builder {
/// equivalent to csharp_generate_const_filter(|_| csharp_generate_const)
#[deprecated(note = "User csharp_generate_const_filter instead")]
pub fn csharp_generate_const(self, csharp_generate_const: bool) -> Builder {
self.csharp_generate_const_filter(if csharp_generate_const { |_| true } else { |_| false })
self.csharp_generate_const_filter(if csharp_generate_const {
|_| true
} else {
|_| false
})
}

/// configure C# generate const filter, default `|_| false`
pub fn csharp_generate_const_filter(mut self, csharp_generate_const_filter: fn(const_name: &str) -> bool) -> Builder {
pub fn csharp_generate_const_filter(
mut self,
csharp_generate_const_filter: fn(const_name: &str) -> bool,
) -> Builder {
self.options.csharp_generate_const_filter = csharp_generate_const_filter;
self
}

/// configure the mappings that C# type name from rust original type name, default `|x| x`
pub fn csharp_type_rename(mut self, csharp_type_rename: fn(rust_type_name: String) -> String) -> Builder {
pub fn csharp_type_rename(
mut self,
csharp_type_rename: fn(rust_type_name: String) -> String,
) -> Builder {
self.options.csharp_type_rename = csharp_type_rename;
self
}
Expand All @@ -241,6 +266,25 @@ impl Builder {
self
}

pub fn csharp_group_methods<T1, T2, T3>(
mut self,
rust_prefix: T1,
csharp_class: T2,
rust_type: T3,
) -> Builder
where
T1: Into<String>,
T2: Into<String>,
T3: Into<String>,
{
self.options.csharp_method_groups.push(MethodGroup {
csharp_class: csharp_class.into(),
rust_prefix: rust_prefix.into(),
rust_type: rust_type.into(),
});
self
}

pub fn generate_csharp_file<P: AsRef<Path>>(
&self,
csharp_output_path: P,
Expand Down
Loading