Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codegen/enum: Do not generate doc(cfg) on match arms #994

Merged
merged 4 commits into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/codegen/enums.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{
analysis::{imports::Imports, namespaces},
codegen::general::{
self, cfg_deprecated, derives, version_condition, version_condition_string,
self, cfg_deprecated, derives, version_condition, version_condition_no_doc,
version_condition_string,
},
config::gobjects::GObject,
env::Env,
Expand Down Expand Up @@ -174,7 +175,7 @@ fn generate_enum(
enum_.name
)?;
for member in &members {
version_condition(w, env, member.version, false, 3)?;
version_condition_no_doc(w, env, member.version, false, 3)?;
writeln!(w, "\t\t\t{0}::{1} => \"{1}\",", enum_.name, member.name)?;
}
writeln!(
Expand All @@ -201,7 +202,7 @@ impl ToGlib for {name} {{
ffi_name = enum_.c_type
)?;
for member in &members {
version_condition(w, env, member.version, false, 3)?;
version_condition_no_doc(w, env, member.version, false, 3)?;
writeln!(
w,
"\t\t\t{}::{} => {}::{},",
Expand Down Expand Up @@ -238,7 +239,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
assert = assert
)?;
for member in &members {
version_condition(w, env, member.version, false, 3)?;
version_condition_no_doc(w, env, member.version, false, 3)?;
writeln!(
w,
"\t\t\t{} => {}::{},",
Expand Down Expand Up @@ -304,7 +305,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
)?;

for member in &members {
version_condition(w, env, member.version, false, 3)?;
version_condition_no_doc(w, env, member.version, false, 3)?;
writeln!(
w,
"\t\t\t{} => Some({}::{}),",
Expand Down
58 changes: 41 additions & 17 deletions src/codegen/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,24 @@ pub fn version_condition(
Ok(())
}

pub fn version_condition_no_doc(
w: &mut dyn Write,
env: &Env,
version: Option<Version>,
commented: bool,
indent: usize,
) -> Result<()> {
match version {
Some(v) if v > env.config.min_cfg_version => {
if let Some(s) = cfg_condition_string_no_doc(&Some(v.to_cfg()), commented, indent) {
writeln!(w, "{}", s)?
}
}
_ => {}
}
Ok(())
}

pub fn version_condition_string(
env: &Env,
version: Option<Version>,
Expand All @@ -504,14 +522,7 @@ pub fn version_condition_string(
) -> Option<String> {
match version {
Some(v) if v > env.config.min_cfg_version => {
let comment = if commented { "//" } else { "" };
Some(format!(
"{0}{1}#[cfg(any({2}, feature = \"dox\"))]\n\
{0}{1}#[cfg_attr(feature = \"dox\", doc(cfg({2})))]",
tabs(indent),
comment,
v.to_cfg()
))
cfg_condition_string(&Some(v.to_cfg()), commented, indent)
}
_ => None,
}
Expand All @@ -523,15 +534,9 @@ pub fn not_version_condition(
commented: bool,
indent: usize,
) -> Result<()> {
if let Some(v) = version {
let comment = if commented { "//" } else { "" };
let s = format!(
"{0}{1}#[cfg(any(not({2}), feature = \"dox\"))]\n\
{0}{1}#[cfg_attr(feature = \"dox\", doc(cfg(not({2}))))]",
tabs(indent),
comment,
v.to_cfg()
);
if let Some(s) = version.and_then(|v| {
cfg_condition_string(&Some(format!("not({})", v.to_cfg())), commented, indent)
}) {
writeln!(w, "{}", s)?;
}
Ok(())
Expand Down Expand Up @@ -569,6 +574,25 @@ pub fn cfg_condition(
Ok(())
}

pub fn cfg_condition_string_no_doc(
cfg_condition: &Option<String>,
commented: bool,
indent: usize,
) -> Option<String> {
match cfg_condition.as_ref() {
Some(v) => {
let comment = if commented { "//" } else { "" };
Some(format!(
"{0}{1}#[cfg(any({2}, feature = \"dox\"))]",
tabs(indent),
comment,
v
))
}
None => None,
}
}

pub fn cfg_condition_string(
cfg_condition: &Option<String>,
commented: bool,
Expand Down
26 changes: 7 additions & 19 deletions src/codegen/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
rust_type::{rust_type, rust_type_full},
},
case::CaseExt,
codegen::general::version_condition_string,
codegen::general::{version_condition_no_doc, version_condition_string},
env::Env,
library, nameutil,
traits::IntoString,
Expand Down Expand Up @@ -235,28 +235,16 @@ impl {}Builder {{
)?;
for property in &properties {
let name = nameutil::mangle_keywords(nameutil::signal_to_snake(&property.name));
let version_condition_string = version_condition_string(env, property.version, false, 2);
let condition_tabs = if version_condition_string.is_some() {
"\t"
} else {
""
};
if let Some(ref version_condition_string) = version_condition_string {
writeln!(w, "{}", version_condition_string)?;
writeln!(w, " {{")?;
}
version_condition_no_doc(w, env, property.version, false, 2)?;
writeln!(
w,
"{tabs} if let Some(ref {field}) = self.{field} {{
{tabs} properties.push((\"{name}\", {field}));
{tabs} }}",
"\
if let Some(ref {field}) = self.{field} {{
properties.push((\"{name}\", {field}));
}}",
name = property.name,
field = name,
tabs = condition_tabs
field = name
)?;
if version_condition_string.is_some() {
writeln!(w, " }}")?;
}
}
let glib_crate_name = if env.namespaces.is_glib_crate {
"crate"
Expand Down
7 changes: 1 addition & 6 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ pub enum Version {

impl Version {
pub fn to_cfg(self) -> String {
use self::Version::*;
match self {
Full(major, minor, 0) => format!("feature = \"v{}_{}\"", major, minor),
Full(major, minor, patch) => format!("feature = \"v{}_{}_{}\"", major, minor, patch),
Short(major) => format!("feature = \"v{}\"", major),
}
format!("feature = \"{}\"", self.to_feature())
}

pub fn to_feature(self) -> String {
Expand Down