Commit 0d8c53ec authored by Ryan Hunt's avatar Ryan Hunt
Browse files

Remove Cargo from public interface

parent aa9c20bc
......@@ -15,7 +15,8 @@ use bindgen::parser::{self, Parse};
pub struct Builder {
config: Config,
srcs: Vec<path::PathBuf>,
lib: Option<Cargo>,
lib: Option<(path::PathBuf, Option<String>)>,
lib_cargo: Option<Cargo>,
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<P: AsRef<path::Path>>(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<P: AsRef<path::Path>,
S: AsRef<str>>(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,
......
......@@ -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<Cargo, String> {
pub(crate) fn load(crate_dir: &Path,
binding_crate_name: Option<&str>,
use_cargo_lock: bool) -> Result<Cargo, String> {
let toml_path = crate_dir.join("Cargo.toml");
let lock_path = crate_dir.join("Cargo.lock");
......
......@@ -8,4 +8,4 @@ mod cargo_lock;
mod cargo_metadata;
mod cargo_toml;
pub use self::cargo::*;
pub(crate) use self::cargo::*;
......@@ -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;
......@@ -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<Vec<String>>,
exclude: &Vec<String>,
expand: &Vec<String>) -> ParseResult {
pub(crate) fn parse_lib(lib: Cargo,
parse_deps: bool,
include: &Option<Vec<String>>,
exclude: &Vec<String>,
expand: &Vec<String>) -> ParseResult {
let mut context = Parser {
lib: lib,
parse_deps: parse_deps,
......
......@@ -28,12 +28,8 @@ pub fn generate<P: AsRef<Path>>(crate_dir: P) -> Result<Bindings, String> {
/// A utility function for build scripts to generate bindings for a crate with a
/// custom config.
pub fn generate_with_config<P: AsRef<Path>>(crate_dir: P, config: Config) -> Result<Bindings, String> {
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()
}
......@@ -85,7 +85,7 @@ fn load_bindings<'a>(input: &Path, matches: &ArgMatches<'a>) -> Result<Bindings,
Builder::new().with_config(config)
.with_std_types()
.with_crate(lib)
.with_cargo(lib)
.generate()
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment