Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Handle doc generation for docs.rs
  • Loading branch information
GuillaumeGomez committed Jul 25, 2019
1 parent 22b8475 commit e2a1328
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
13 changes: 12 additions & 1 deletion src/codegen/sys/build.rs
Expand Up @@ -26,21 +26,32 @@ fn generate_build_script(w: &mut dyn Write, env: &Env) -> Result<()> {
write!(
w,
"{}",
r##"extern crate pkg_config;
r##"#[cfg(not(feature = "docs-rs"))]
extern crate pkg_config;
#[cfg(not(feature = "docs-rs"))]
use pkg_config::{Config, Error};
#[cfg(not(feature = "docs-rs"))]
use std::env;
#[cfg(not(feature = "docs-rs"))]
use std::io::prelude::*;
#[cfg(not(feature = "docs-rs"))]
use std::io;
#[cfg(not(feature = "docs-rs"))]
use std::process;
#[cfg(feature = "docs-rs")]
fn main() {} // prevent linking libraries to avoid documentation failure
#[cfg(not(feature = "docs-rs"))]
fn main() {
if let Err(s) = find() {
let _ = writeln!(io::stderr(), "{}", s);
process::exit(1);
}
}
#[cfg(not(feature = "docs-rs"))]
fn find() -> Result<(), Error> {
"##
)?;
Expand Down
41 changes: 33 additions & 8 deletions src/codegen/sys/cargo_toml.rs
Expand Up @@ -92,19 +92,44 @@ fn fill_in(root: &mut Table, env: &Env) {

{
let features = upsert_table(root, "features");
let versions = env
let mut versions = env
.namespaces
.main()
.versions
.iter()
.filter(|&&v| v > env.config.min_cfg_version);
versions.fold(None::<Version>, |prev, &version| {
let prev_array: Vec<Value> =
prev.iter().map(|v| Value::String(v.to_feature())).collect();
features.insert(version.to_feature(), Value::Array(prev_array));
Some(version)
});
.filter(|&&v| v > env.config.min_cfg_version)
.collect::<Vec<_>>();
versions.sort_unstable();
let max_version = versions.last().map(|x| *x);
versions
.into_iter()
.fold(None::<Version>, |prev, &version| {
let prev_array: Vec<Value> =
prev.iter().map(|v| Value::String(v.to_feature())).collect();
features.insert(version.to_feature(), Value::Array(prev_array));
Some(version)
});
features.insert("dox".to_string(), Value::Array(Vec::new()));
features.insert("docs-rs".to_string(), Value::Array(Vec::new()));

let docs_rs_metadata = upsert_table(root, "package.metadata.docs.rs");
let mut docs_rs_features = env.config.docs_rs_features.clone();
docs_rs_features.push("docs-rs".to_owned());
docs_rs_features.push("dox".to_owned());
if let Some(version) = max_version {
docs_rs_features.push(version.to_feature());
}
docs_rs_features.sort_unstable();
docs_rs_features.dedup(); // we remove duplicates if there is any
docs_rs_metadata.insert(
"features".to_string(),
Value::Array(
docs_rs_features
.into_iter()
.map(|s| Value::String(s))
.collect::<Vec<_>>(),
),
);
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/config/config.rs
Expand Up @@ -39,6 +39,7 @@ pub struct Config {
pub concurrency: library::Concurrency,
pub single_version_file: Option<PathBuf>,
pub generate_display_trait: bool,
pub docs_rs_features: Vec<String>,
}

impl Config {
Expand Down Expand Up @@ -156,6 +157,22 @@ impl Config {
None => true,
};

let mut docs_rs_features = Vec::new();
for v in match toml.lookup("options.docs_rs_features") {
Some(v) => v.as_result_vec("options.docs_rs_features")?.as_slice(),
None => &[],
} {
docs_rs_features.push(match v.as_str() {
Some(s) => s.to_owned(),
None => {
return Err(format!(
"Invalid `docs_rs_features` value element, expected a string, found {}",
v.type_str()
))
}
});
}

// options.concurrency is the default of all objects if nothing
// else is configured
let mut objects = toml
Expand Down Expand Up @@ -213,6 +230,7 @@ impl Config {
concurrency,
single_version_file,
generate_display_trait,
docs_rs_features,
})
}

Expand Down

0 comments on commit e2a1328

Please sign in to comment.