Skip to content

Commit fac4c0d

Browse files
Switch structs to storing CommonAttrs instead of just attrs
1 parent c361163 commit fac4c0d

File tree

15 files changed

+58
-61
lines changed

15 files changed

+58
-61
lines changed

crates/cxx-qt-gen/src/generator/cpp/inherit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn generate(
2727

2828
for &method in inherited_methods {
2929
// Skip if the cfg attributes are not resolved to true
30-
if !try_eval_attributes(opt.cfg_evaluator.as_ref(), &method.cfgs)? {
30+
if !try_eval_attributes(opt.cfg_evaluator.as_ref(), &method.common_attrs.cfgs)? {
3131
continue;
3232
}
3333

crates/cxx-qt-gen/src/generator/cpp/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl GeneratedCppBlocks {
7272
.iter()
7373
.filter_map(|qobject| {
7474
// Skip if the cfg attributes are not resolved to true
75-
match try_eval_attributes(opt.cfg_evaluator.as_ref(), &qobject.declaration.cfgs)
75+
match try_eval_attributes(opt.cfg_evaluator.as_ref(), &qobject.declaration.common_attrs.cfgs)
7676
{
7777
Ok(true) => {
7878
Some(GeneratedCppQObject::from(qobject, &parser.type_names, opt))

crates/cxx-qt-gen/src/generator/cpp/qenum.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn generate_declaration(
3939
opt: &GeneratedOpt,
4040
) -> Result<String> {
4141
// Skip if the cfg attributes are not resolved to true
42-
if !try_eval_attributes(opt.cfg_evaluator.as_ref(), &qenum.cfgs)? {
42+
if !try_eval_attributes(opt.cfg_evaluator.as_ref(), &qenum.common_attrs.cfgs)? {
4343
return Ok(String::new());
4444
}
4545

@@ -75,7 +75,7 @@ pub fn generate_on_qobject<'a>(
7575

7676
for qenum in qenums {
7777
// Skip if the cfg attributes are not resolved to true
78-
if !try_eval_attributes(opt.cfg_evaluator.as_ref(), &qenum.cfgs)? {
78+
if !try_eval_attributes(opt.cfg_evaluator.as_ref(), &qenum.common_attrs.cfgs)? {
7979
continue;
8080
}
8181

crates/cxx-qt-gen/src/generator/cpp/signal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub fn generate_cpp_signal(
8989
let mut generated = CppSignalFragment::default();
9090

9191
// Skip if the cfg attributes are not resolved to true
92-
if !try_eval_attributes(opt.cfg_evaluator.as_ref(), &signal.cfgs)? {
92+
if !try_eval_attributes(opt.cfg_evaluator.as_ref(), &signal.common_attrs.cfgs)? {
9393
return Ok(generated);
9494
}
9595

crates/cxx-qt-gen/src/generator/rust/externcxxqt.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl GeneratedRustFragment {
2222
} else {
2323
quote! {}
2424
};
25-
let extern_block_docs = &extern_cxxqt_block.docs;
25+
let extern_block_docs = &extern_cxxqt_block.common_attrs.docs;
2626

2727
// Add the pass through blocks
2828
let unsafety = &extern_cxxqt_block.unsafety;
@@ -60,6 +60,7 @@ impl GeneratedRustFragment {
6060
#[cxx_name = #cxx_name]
6161
}
6262
};
63+
// TODO! Can we make extract_docs return references, and then use here?
6364
let cfgs: Vec<&Attribute> = obj
6465
.declaration
6566
.attrs

crates/cxx-qt-gen/src/generator/rust/inherit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ pub fn generate(
4848
if method.safe {
4949
std::mem::swap(&mut unsafe_call, &mut unsafe_block);
5050
}
51-
let doc_comments = &method.docs;
52-
let cfgs = &method.cfgs;
51+
let doc_comments = &method.common_attrs.docs;
52+
let cfgs = &method.common_attrs.cfgs;
5353
let namespace = qobject_names.namespace_tokens();
5454

5555
syn::parse2(quote_spanned! {

crates/cxx-qt-gen/src/generator/rust/qenum.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ pub fn generate_cxx_mod_contents(qenums: &[ParsedQEnum]) -> Vec<Item> {
1616
let item = &qenum.item;
1717
let vis = &item.vis;
1818
let variants = &item.variants;
19-
let docs = &qenum.docs;
20-
let cfgs = &qenum.cfgs;
19+
let docs = &qenum.common_attrs.docs;
20+
let cfgs = &qenum.common_attrs.cfgs;
2121

2222
let cxx_namespace = if namespace.is_none() {
2323
quote! {}

crates/cxx-qt-gen/src/generator/rust/qobject.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl GeneratedRustFragment {
2828
let namespace_idents = NamespaceName::from(qobject);
2929

3030
let mut generated = vec![
31-
generate_qobject_definitions(&qobject_names, &qobject.cfgs, &qobject.docs)?,
31+
generate_qobject_definitions(&qobject_names, &qobject.common_attrs.cfgs, &qobject.common_attrs.docs)?,
3232
generate_rust_properties(
3333
&qobject.properties,
3434
&qobject_names,
@@ -57,7 +57,7 @@ impl GeneratedRustFragment {
5757
&qobject_names,
5858
&namespace_idents,
5959
type_names,
60-
&qobject.cfgs,
60+
&qobject.common_attrs.cfgs,
6161
)?);
6262
}
6363

@@ -75,9 +75,9 @@ impl GeneratedRustFragment {
7575
&qobject_names,
7676
&namespace_idents,
7777
type_names,
78-
&qobject.cfgs,
78+
&qobject.common_attrs.cfgs,
7979
)?,
80-
cxxqttype::generate(&qobject_names, type_names, &qobject.cfgs)?,
80+
cxxqttype::generate(&qobject_names, type_names, &qobject.common_attrs.cfgs)?,
8181
]);
8282

8383
Ok(GeneratedRustFragment::flatten(generated))

crates/cxx-qt-gen/src/generator/rust/signals.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ pub fn generate_rust_signal(
100100
} else {
101101
Some(quote! { unsafe })
102102
};
103-
let doc_comments = &signal.docs;
104-
let cfgs = &signal.cfgs;
103+
let doc_comments = &signal.common_attrs.docs;
104+
let cfgs = &signal.common_attrs.cfgs;
105105
let namespace = if let Some(namespace) = qobject_name.namespace() {
106106
quote_spanned! { span=> #[namespace = #namespace ] }
107107
} else {

crates/cxx-qt-gen/src/parser/externcxxqt.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ use crate::{
1111
syntax::{attribute::attribute_get_path, expr::expr_to_string},
1212
};
1313
use syn::{
14-
spanned::Spanned, Attribute, Error, ForeignItem, ForeignItemFn, Ident, ItemForeignMod, Result,
14+
spanned::Spanned, Error, ForeignItem, ForeignItemFn, Ident, ItemForeignMod, Result,
1515
Token,
1616
};
17+
use crate::parser::CommonAttrs;
1718

1819
/// Representation of an extern "C++Qt" block
1920
#[derive(Default)]
2021
pub struct ParsedExternCxxQt {
2122
/// The namespace of the type in C++.
2223
pub namespace: Option<String>,
23-
/// The Top level docs on the module
24-
pub docs: Vec<Attribute>,
24+
/// All the universal top level attributes for the block
25+
pub common_attrs: CommonAttrs,
2526
/// Whether this block has an unsafe token
2627
pub unsafety: Option<Token![unsafe]>,
2728
/// Items which can be passed into the extern "C++Qt" block
@@ -55,7 +56,7 @@ impl ParsedExternCxxQt {
5556

5657
let mut extern_cxx_block = ParsedExternCxxQt {
5758
namespace,
58-
docs: common_attrs.docs,
59+
common_attrs,
5960
unsafety: foreign_mod.unsafety,
6061
..Default::default()
6162
};

crates/cxx-qt-gen/src/parser/inherit.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
//
44
// SPDX-License-Identifier: MIT OR Apache-2.0
55

6-
use crate::parser::{method::MethodFields, require_attributes, CaseConversion};
6+
use crate::parser::{method::MethodFields, require_attributes, CaseConversion, CommonAttrs};
77
use core::ops::Deref;
88
use quote::format_ident;
99
use std::ops::DerefMut;
10-
use syn::{Attribute, ForeignItemFn, Ident, Result};
10+
use syn::{ForeignItemFn, Ident, Result};
1111

1212
/// Describes a method found in an extern "RustQt" with #[inherit]
1313
pub struct ParsedInheritedMethod {
1414
/// The common fields which are available on all callable types
1515
pub method_fields: MethodFields,
16-
/// All the docs (each line) of the inherited method
17-
pub docs: Vec<Attribute>,
18-
/// Cfgs for the inherited method
19-
pub cfgs: Vec<Attribute>,
16+
/// All the universal attributes for the inherited method
17+
pub common_attrs: CommonAttrs,
2018
}
2119

2220
impl ParsedInheritedMethod {
@@ -34,8 +32,7 @@ impl ParsedInheritedMethod {
3432

3533
Ok(Self {
3634
method_fields: MethodFields::parse(method, auto_case)?,
37-
docs: common_attrs.docs,
38-
cfgs: common_attrs.cfgs,
35+
common_attrs
3936
})
4037
}
4138

crates/cxx-qt-gen/src/parser/mod.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,24 @@ impl CaseConversion {
8787
}
8888
}
8989

90-
/// Iterate the attributes of the method to extract cfg attributes
91-
pub fn extract_cfgs(attrs: &[Attribute]) -> Vec<Attribute> {
90+
/// Helper function to extract all of a particular attribute from the slice
91+
fn extract_attr(attrs: &[Attribute], target: &str) -> Vec<Attribute> {
9292
attrs
9393
.iter()
94-
.filter(|attr| path_compare_str(attr.meta.path(), &["cfg"]))
94+
.filter(|attr| path_compare_str(attr.meta.path(), &[target]))
9595
.cloned()
9696
.collect()
9797
}
9898

99+
/// Iterate the attributes of the method to extract cfg attributes
100+
pub fn extract_cfgs(attrs: &[Attribute]) -> Vec<Attribute> {
101+
extract_attr(attrs, "cfg")
102+
}
103+
99104
/// Iterate the attributes of the method to extract Doc attributes (doc comments are parsed as this)
100105
pub fn extract_docs(attrs: &[Attribute]) -> Vec<Attribute> {
101-
attrs
102-
.iter()
103-
.filter(|attr| path_compare_str(attr.meta.path(), &["doc"]))
104-
.cloned()
105-
.collect()
106+
extract_attr(attrs, "doc")
107+
106108
}
107109

108110
/// Splits a path by :: separators e.g. "cxx_qt::bridge" becomes ["cxx_qt", "bridge"]
@@ -116,9 +118,10 @@ fn split_path(path_str: &str) -> Vec<&str> {
116118
}
117119

118120
/// Attributes which should be passed through, and are available on most things
121+
#[derive(Clone, Debug, Default)]
119122
pub struct CommonAttrs {
120-
docs: Vec<Attribute>,
121-
cfgs: Vec<Attribute>,
123+
pub docs: Vec<Attribute>,
124+
pub cfgs: Vec<Attribute>,
122125
}
123126

124127
/// Collects a Map of all attributes found from the allowed list

crates/cxx-qt-gen/src/parser/qenum.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
//
44
// SPDX-License-Identifier: MIT OR Apache-2.0
55

6-
use crate::parser::CaseConversion;
6+
use crate::parser::{CaseConversion, CommonAttrs};
77
use crate::{naming::Name, parser::require_attributes, syntax::path::path_compare_str};
88
use quote::ToTokens;
9-
use syn::{Attribute, Ident, ItemEnum, Result, Variant};
9+
use syn::{Ident, ItemEnum, Result, Variant};
1010

1111
pub struct ParsedQEnum {
1212
/// The name of the QObject
@@ -17,10 +17,8 @@ pub struct ParsedQEnum {
1717
pub qobject: Option<Ident>,
1818
/// The original enum item
1919
pub item: ItemEnum,
20-
/// Docs from the qenum
21-
pub docs: Vec<Attribute>,
22-
/// Cfgs from the qenum
23-
pub cfgs: Vec<Attribute>,
20+
/// All the universal attributes for the enum
21+
pub common_attrs: CommonAttrs,
2422
}
2523

2624
impl ParsedQEnum {
@@ -94,8 +92,7 @@ impl ParsedQEnum {
9492
name,
9593
qobject,
9694
variants,
97-
docs: common_attrs.docs,
98-
cfgs: common_attrs.cfgs,
95+
common_attrs,
9996
item: qenum,
10097
})
10198
}

crates/cxx-qt-gen/src/parser/qobject.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::{
1111
#[cfg(test)]
1212
use quote::format_ident;
1313
use syn::{Attribute, Error, Ident, Meta, Result};
14+
use crate::parser::CommonAttrs;
1415

1516
/// Metadata for registering QML element
1617
#[derive(Clone, Debug, Default, Eq, PartialEq)]
@@ -40,10 +41,8 @@ pub struct ParsedQObject {
4041
pub has_qobject_macro: bool,
4142
/// The original declaration entered by the user, i.e. a type alias with a list of attributes
4243
pub declaration: ForeignTypeIdentAlias,
43-
/// Cfgs for the object
44-
pub cfgs: Vec<Attribute>,
45-
/// Docs for the object
46-
pub docs: Vec<Attribute>,
44+
/// All the universal attributes for the object
45+
pub common_attrs: CommonAttrs,
4746
}
4847

4948
impl ParsedQObject {
@@ -74,8 +73,10 @@ impl ParsedQObject {
7473
ident_left: format_ident!("MyObject"),
7574
ident_right: format_ident!("MyObjectRust"),
7675
},
77-
cfgs: vec![],
78-
docs: vec![],
76+
common_attrs: CommonAttrs {
77+
docs: vec![],
78+
cfgs: vec![],
79+
}
7980
}
8081
}
8182

@@ -124,8 +125,7 @@ impl ParsedQObject {
124125
properties,
125126
qml_metadata,
126127
has_qobject_macro,
127-
cfgs: common_attrs.cfgs,
128-
docs: common_attrs.docs,
128+
common_attrs
129129
})
130130
}
131131

crates/cxx-qt-gen/src/parser/signals.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use crate::{
88
};
99
use core::ops::Deref;
1010
use std::ops::DerefMut;
11-
use syn::{spanned::Spanned, Attribute, Error, ForeignItemFn, Result, Visibility};
11+
use syn::{spanned::Spanned, Error, ForeignItemFn, Result, Visibility};
12+
use crate::parser::CommonAttrs;
1213

1314
#[derive(Clone)]
1415
/// Describes an individual Signal
@@ -19,10 +20,8 @@ pub struct ParsedSignal {
1920
pub inherit: bool,
2021
/// Whether the signal is private
2122
pub private: bool,
22-
/// All the doc attributes (each line) of the signal
23-
pub docs: Vec<Attribute>,
24-
/// Cfgs for signal
25-
pub cfgs: Vec<Attribute>,
23+
/// All the universal attributes for the signal
24+
pub common_attrs: CommonAttrs,
2625
}
2726

2827
impl ParsedSignal {
@@ -58,8 +57,7 @@ impl ParsedSignal {
5857
method_fields: fields,
5958
inherit,
6059
private,
61-
docs: common_attrs.docs,
62-
cfgs: common_attrs.cfgs,
60+
common_attrs,
6361
})
6462
}
6563
}

0 commit comments

Comments
 (0)