From 0d8c53ecbcef0d415aa5e97fd2f456b31f3eb014 Mon Sep 17 00:00:00 2001 From: Ryan Hunt Date: Tue, 14 Nov 2017 01:13:54 -0500 Subject: [PATCH] Remove Cargo from public interface --- src/bindgen/builder.rs | 51 ++++++++++++++++++++++++++++++++++---- src/bindgen/cargo/cargo.rs | 8 +++--- src/bindgen/cargo/mod.rs | 2 +- src/bindgen/mod.rs | 4 ++- src/bindgen/parser.rs | 10 ++++---- src/lib.rs | 6 +---- src/main.rs | 2 +- 7 files changed, 61 insertions(+), 22 deletions(-) diff --git a/src/bindgen/builder.rs b/src/bindgen/builder.rs index 2951644..53df663 100644 --- a/src/bindgen/builder.rs +++ b/src/bindgen/builder.rs @@ -15,7 +15,8 @@ use bindgen::parser::{self, Parse}; pub struct Builder { config: Config, srcs: Vec, - lib: Option, + lib: Option<(path::PathBuf, Option)>, + lib_cargo: Option, std_types: bool, } @@ -25,6 +26,7 @@ impl Builder { config: Config::default(), srcs: Vec::new(), lib: None, + lib_cargo: None, std_types: false, } } @@ -44,9 +46,32 @@ impl Builder { self } - pub fn with_crate(mut self, lib: Cargo) -> Builder { + #[allow(unused)] + pub fn with_crate>(mut self, lib_dir: P) -> Builder { debug_assert!(self.lib.is_none()); - self.lib = Some(lib); + debug_assert!(self.lib_cargo.is_none()); + self.lib = Some((path::PathBuf::from(lib_dir.as_ref()), None)); + self + } + + #[allow(unused)] + pub fn with_crate_and_name, + S: AsRef>(mut self, + lib_dir: P, + binding_lib_name: S) -> Builder + { + debug_assert!(self.lib.is_none()); + debug_assert!(self.lib_cargo.is_none()); + self.lib = Some((path::PathBuf::from(lib_dir.as_ref()), + Some(String::from(binding_lib_name.as_ref())))); + self + } + + #[allow(unused)] + pub(crate) fn with_cargo(mut self, lib: Cargo) -> Builder { + debug_assert!(self.lib.is_none()); + debug_assert!(self.lib_cargo.is_none()); + self.lib_cargo = Some(lib); self } @@ -61,8 +86,24 @@ impl Builder { result.extend_with(&parser::parse_src(x)?); } - if let Some(x) = self.lib.clone() { - result.extend_with(&parser::parse_lib(x, + if let Some((lib_dir, binding_lib_name)) = self.lib.clone() { + let cargo = if let Some(binding_lib_name) = binding_lib_name { + Cargo::load(&lib_dir, + Some(&binding_lib_name), + self.config.parse.parse_deps)? + } else { + Cargo::load(&lib_dir, + None, + self.config.parse.parse_deps)? + }; + + result.extend_with(&parser::parse_lib(cargo, + self.config.parse.parse_deps, + &self.config.parse.include, + &self.config.parse.exclude, + &self.config.parse.expand)?); + } else if let Some(cargo) = self.lib_cargo.clone() { + result.extend_with(&parser::parse_lib(cargo, self.config.parse.parse_deps, &self.config.parse.include, &self.config.parse.exclude, diff --git a/src/bindgen/cargo/cargo.rs b/src/bindgen/cargo/cargo.rs index 69462cf..c035dd8 100644 --- a/src/bindgen/cargo/cargo.rs +++ b/src/bindgen/cargo/cargo.rs @@ -24,7 +24,7 @@ pub(crate) struct PackageRef { /// A collection of metadata for a library from cargo. #[derive(Clone, Debug)] -pub struct Cargo { +pub(crate) struct Cargo { manifest_path: PathBuf, binding_crate_name: String, @@ -36,9 +36,9 @@ impl Cargo { /// Gather metadata from cargo for a specific library and binding crate /// name. If dependency finding isn't needed then Cargo.lock files don't /// need to be parsed. - pub fn load(crate_dir: &Path, - binding_crate_name: Option<&str>, - use_cargo_lock: bool) -> Result { + pub(crate) fn load(crate_dir: &Path, + binding_crate_name: Option<&str>, + use_cargo_lock: bool) -> Result { let toml_path = crate_dir.join("Cargo.toml"); let lock_path = crate_dir.join("Cargo.lock"); diff --git a/src/bindgen/cargo/mod.rs b/src/bindgen/cargo/mod.rs index e527117..4164c16 100644 --- a/src/bindgen/cargo/mod.rs +++ b/src/bindgen/cargo/mod.rs @@ -8,4 +8,4 @@ mod cargo_lock; mod cargo_metadata; mod cargo_toml; -pub use self::cargo::*; +pub(crate) use self::cargo::*; diff --git a/src/bindgen/mod.rs b/src/bindgen/mod.rs index 52f0f71..d0bfaeb 100644 --- a/src/bindgen/mod.rs +++ b/src/bindgen/mod.rs @@ -49,7 +49,9 @@ mod rename; mod utilities; mod writer; -pub use self::cargo::*; +#[allow(unused)] +pub(crate) use self::cargo::*; + pub use self::config::*; pub use self::bindings::Bindings; pub use self::builder::Builder; diff --git a/src/bindgen/parser.rs b/src/bindgen/parser.rs index cfc3d72..177c3b6 100644 --- a/src/bindgen/parser.rs +++ b/src/bindgen/parser.rs @@ -42,11 +42,11 @@ pub fn parse_src(src_file: &Path) -> ParseResult { /// Inside a crate, `mod` and `extern crate` declarations are followed /// and parsed. To find an external crate, the parser uses the `cargo metadata` /// command to find the location of dependencies. -pub fn parse_lib(lib: Cargo, - parse_deps: bool, - include: &Option>, - exclude: &Vec, - expand: &Vec) -> ParseResult { +pub(crate) fn parse_lib(lib: Cargo, + parse_deps: bool, + include: &Option>, + exclude: &Vec, + expand: &Vec) -> ParseResult { let mut context = Parser { lib: lib, parse_deps: parse_deps, diff --git a/src/lib.rs b/src/lib.rs index 911948e..75a9ac9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,12 +28,8 @@ pub fn generate>(crate_dir: P) -> Result { /// A utility function for build scripts to generate bindings for a crate with a /// custom config. pub fn generate_with_config>(crate_dir: P, config: Config) -> Result { - let cargo = Cargo::load(crate_dir.as_ref(), - None, - config.parse.parse_deps)?; - Builder::new().with_config(config) .with_std_types() - .with_crate(cargo) + .with_crate(crate_dir) .generate() } diff --git a/src/main.rs b/src/main.rs index 109dd31..a962854 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,7 +85,7 @@ fn load_bindings<'a>(input: &Path, matches: &ArgMatches<'a>) -> Result