Skip to content

Commit

Permalink
Add external docs url config option
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Erhardt <aaron.erhardt@t-online.de>
  • Loading branch information
AaronErhardt committed Oct 15, 2022
1 parent b3147f2 commit 577bbbb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/codegen/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ pub fn define_fundamental_type(
let sys_crate_name = env.main_sys_crate_name();
writeln!(w, "{} {{", use_glib_type(env, "wrapper!"))?;
doc_alias(w, glib_name, "", 1)?;
external_doc_link(
w,
env.config.external_docs_url.as_deref(),
type_name,
&visibility,
1,
)?;
writeln!(
w,
"\t{} struct {}(Shared<{}::{}>);",
Expand Down Expand Up @@ -235,6 +242,13 @@ pub fn define_object_type(

writeln!(w, "{} {{", use_glib_type(env, "wrapper!"))?;
doc_alias(w, glib_name, "", 1)?;
external_doc_link(
w,
env.config.external_docs_url.as_deref(),
type_name,
&visibility,
1,
)?;
if parents.is_empty() {
writeln!(
w,
Expand Down Expand Up @@ -909,6 +923,29 @@ pub fn doc_alias(w: &mut dyn Write, name: &str, comment_prefix: &str, indent: us
)
}

pub fn external_doc_link(
w: &mut dyn Write,
external_url: Option<&str>,
name: &str,
visibility: &Visibility,
indent: usize,
) -> Result<()> {
// Don't generate the external doc link on non-public types.
if !visibility.is_public() {
Ok(())
} else if let Some(external_url) = external_url {
writeln!(
w,
"{}/// This doc is limited due to license limitations. Please look at [our official docs]({}/index.html?search={}) for more information.",
tabs(indent),
external_url.trim_end_matches("/"),
name
)
} else {
Ok(())
}
}

pub fn doc_hidden(
w: &mut dyn Write,
doc_hidden: bool,
Expand Down
17 changes: 17 additions & 0 deletions src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ pub struct Config {
pub lib_version_overrides: HashMap<Version, Version>,
pub feature_dependencies: HashMap<Version, Vec<String>>,
pub dox_feature_dependencies: Vec<String>,
/// An url that will be inserted into the docs as link that links
/// to another doc source, for example when builds on docs.rs
/// are limited due to license issues.
pub external_docs_url: Option<String>,
}

impl Config {
Expand Down Expand Up @@ -346,6 +350,7 @@ impl Config {
let lib_version_overrides = read_lib_version_overrides(&toml)?;
let feature_dependencies = read_feature_dependencies(&toml)?;
let dox_feature_dependencies = read_dox_feature_dependencies(&toml)?;
let external_docs_url = read_external_docs_url(&toml)?;

Ok(Config {
work_mode,
Expand Down Expand Up @@ -375,6 +380,7 @@ impl Config {
lib_version_overrides,
feature_dependencies,
dox_feature_dependencies,
external_docs_url,
})
}

Expand Down Expand Up @@ -548,6 +554,17 @@ fn read_feature_dependencies(toml: &toml::Value) -> Result<HashMap<Version, Vec<
Ok(map)
}

fn read_external_docs_url(toml: &toml::Value) -> Result<Option<String>, String> {
Ok(
if let Some(value) = toml.lookup("options.external_docs_url") {
let value = value.as_result_str("options.external_docs_url")?;
Some(value.to_string())
} else {
None
},
)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 577bbbb

Please sign in to comment.