use &Path as input path parameter

This commit is contained in:
Aster 2023-03-27 13:27:11 +08:00
parent 1bd4f42ba2
commit 743abbed28
5 changed files with 26 additions and 22 deletions

2
.gitignore vendored
View File

@ -1,6 +1,8 @@
/target /target
/Cargo.lock /Cargo.lock
# IDE
.idea/
.vs/ .vs/
dotnet-sandbox/obj/ dotnet-sandbox/obj/
dotnet-sandbox/bin/ dotnet-sandbox/bin/

View File

@ -1,4 +1,4 @@
use std::{collections::HashMap}; use std::collections::HashMap;
use crate::type_meta::{RustType, TypeKind}; use crate::type_meta::{RustType, TypeKind};
@ -44,7 +44,7 @@ impl AliasMap {
let d = self.normalize_deep(x); let d = self.normalize_deep(x);
(d.type_name.clone(), Some(d)) (d.type_name.clone(), Some(d))
} }
None => (name.to_owned(), None) None => (name.to_owned(), None),
} }
} }

View File

@ -1,3 +1,4 @@
use std::path::PathBuf;
use std::{ use std::{
error::Error, error::Error,
fs::{File, OpenOptions}, fs::{File, OpenOptions},
@ -12,8 +13,8 @@ pub struct Builder {
} }
pub struct BindgenOptions { pub struct BindgenOptions {
pub input_bindgen_file: String, pub input_bindgen_file: PathBuf,
pub input_extern_files: Vec<String>, pub input_extern_files: Vec<PathBuf>,
pub method_filter: fn(method_name: String) -> bool, pub method_filter: fn(method_name: String) -> bool,
pub rust_method_type_path: String, pub rust_method_type_path: String,
pub rust_method_prefix: String, pub rust_method_prefix: String,
@ -33,7 +34,7 @@ impl Default for Builder {
fn default() -> Self { fn default() -> Self {
Self { Self {
options: BindgenOptions { options: BindgenOptions {
input_bindgen_file: "".to_string(), input_bindgen_file: PathBuf::new(),
input_extern_files: vec![], input_extern_files: vec![],
method_filter: |x| !x.starts_with('_'), method_filter: |x| !x.starts_with('_'),
rust_method_type_path: "".to_string(), rust_method_type_path: "".to_string(),
@ -59,16 +60,16 @@ impl Builder {
} }
/// Change an input .rs file(such as generated from bindgen) to generate binding. /// Change an input .rs file(such as generated from bindgen) to generate binding.
pub fn input_bindgen_file<T: Into<String>>(mut self, input_bindgen_file: T) -> Builder { pub fn input_bindgen_file<T: AsRef<Path>>(mut self, input_bindgen_file: T) -> Builder {
self.options.input_bindgen_file = input_bindgen_file.into(); self.options.input_bindgen_file = input_bindgen_file.as_ref().to_path_buf();
self self
} }
/// Add an input .rs file for collect extern methods to C# binding. /// Add an input .rs file for collect extern methods to C# binding.
pub fn input_extern_file<T: Into<String>>(mut self, input_extern_file: T) -> Builder { pub fn input_extern_file<T: AsRef<Path>>(mut self, input_extern_file: T) -> Builder {
self.options self.options
.input_extern_files .input_extern_files
.push(input_extern_file.into()); .push(input_extern_file.as_ref().to_path_buf());
self self
} }
@ -165,7 +166,7 @@ impl Builder {
&self, &self,
csharp_output_path: P, csharp_output_path: P,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
if !self.options.input_bindgen_file.is_empty() { if self.has_input_file() {
let (_, csharp) = generate(GenerateKind::InputBindgen, &self.options)?; let (_, csharp) = generate(GenerateKind::InputBindgen, &self.options)?;
let mut csharp_file = make_file(csharp_output_path.as_ref())?; let mut csharp_file = make_file(csharp_output_path.as_ref())?;
@ -173,7 +174,7 @@ impl Builder {
csharp_file.flush()?; csharp_file.flush()?;
} }
if !self.options.input_extern_files.is_empty() { if self.has_input_externals() {
let (_, csharp) = generate(GenerateKind::InputExtern, &self.options)?; let (_, csharp) = generate(GenerateKind::InputExtern, &self.options)?;
let mut csharp_file = make_file(csharp_output_path.as_ref())?; let mut csharp_file = make_file(csharp_output_path.as_ref())?;
@ -184,12 +185,19 @@ impl Builder {
Ok(()) Ok(())
} }
fn has_input_file(&self) -> bool {
!self.options.input_bindgen_file.to_string_lossy().is_empty()
}
fn has_input_externals(&self) -> bool {
!self.options.input_extern_files.is_empty()
}
pub fn generate_to_file<P: AsRef<Path>>( pub fn generate_to_file<P: AsRef<Path>>(
&self, &self,
rust_output_path: P, rust_output_path: P,
csharp_output_path: P, csharp_output_path: P,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
if !self.options.input_bindgen_file.is_empty() { if self.has_input_file() {
let (rust, csharp) = generate(GenerateKind::InputBindgen, &self.options)?; let (rust, csharp) = generate(GenerateKind::InputBindgen, &self.options)?;
if let Some(rust) = rust { if let Some(rust) = rust {
@ -204,7 +212,7 @@ impl Builder {
csharp_file.flush()?; csharp_file.flush()?;
} }
if !self.options.input_extern_files.is_empty() { if self.has_input_externals() {
let (rust, csharp) = generate(GenerateKind::InputExtern, &self.options)?; let (rust, csharp) = generate(GenerateKind::InputExtern, &self.options)?;
if let Some(rust) = rust { if let Some(rust) = rust {

View File

@ -38,7 +38,7 @@ pub(crate) fn generate(
for path in paths { for path in paths {
let file_content = std::fs::read_to_string(path) let file_content = std::fs::read_to_string(path)
.expect(("input file not found, path:".to_string() + path).as_str()); .expect(&format!("input file not found, path: {}", path.display()));
let file_ast = syn::parse_file(file_content.as_str())?; let file_ast = syn::parse_file(file_content.as_str())?;
match generate_kind { match generate_kind {

View File

@ -271,13 +271,7 @@ impl RustType {
sb.push_str(type_name.as_str()); sb.push_str(type_name.as_str());
} else { } else {
let type_name = type_csharp_string.as_str(); let type_name = type_csharp_string.as_str();
sb.push_str( sb.push_str(format!("void/* {}[] */", type_name).as_str());
format!(
"void/* {}[] */",
type_name
)
.as_str(),
);
} }
} }
TypeKind::Function(parameters, return_type) => { TypeKind::Function(parameters, return_type) => {