Skip to content

Commit c831813

Browse files
Use CommonAttrs everywhere
1 parent fac4c0d commit c831813

File tree

8 files changed

+28
-32
lines changed

8 files changed

+28
-32
lines changed

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ use crate::{
77
generator::rust::{fragment::GeneratedRustFragment, signals::generate_rust_signal},
88
naming::TypeNames,
99
parser::externcxxqt::ParsedExternCxxQt,
10-
syntax::path::path_compare_str,
1110
};
1211
use quote::quote;
13-
use syn::{parse_quote, Attribute, Item, Result};
12+
use syn::{parse_quote, Item, Result};
1413

1514
impl GeneratedRustFragment {
1615
pub fn from_extern_cxx_qt(
@@ -60,19 +59,9 @@ impl GeneratedRustFragment {
6059
#[cxx_name = #cxx_name]
6160
}
6261
};
63-
// TODO! Can we make extract_docs return references, and then use here?
64-
let cfgs: Vec<&Attribute> = obj
65-
.declaration
66-
.attrs
67-
.iter()
68-
.filter(|attr| path_compare_str(attr.meta.path(), &["cfg"]))
69-
.collect();
70-
let docs: Vec<&Attribute> = obj
71-
.declaration
72-
.attrs
73-
.iter()
74-
.filter(|attr| path_compare_str(attr.meta.path(), &["doc"]))
75-
.collect();
62+
// TODO: (cfg everywhere) Can we make extract_docs return references, and then use here?
63+
let cfgs = obj.common_attrs.cfgs.iter().collect::<Vec<_>>();
64+
let docs = obj.common_attrs.docs.iter().collect::<Vec<_>>();
7665
qobject_items.push(parse_quote! {
7766
#namespace
7867
#cxx_name

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use crate::{
1515
naming::TypeNames,
1616
};
1717
use quote::quote;
18-
use syn::{parse_quote, Attribute, Result};
18+
use syn::{parse_quote, Result};
19+
use crate::parser::CommonAttrs;
1920

2021
impl GeneratedRustFragment {
2122
pub fn from_qobject(
@@ -28,7 +29,7 @@ impl GeneratedRustFragment {
2829
let namespace_idents = NamespaceName::from(qobject);
2930

3031
let mut generated = vec![
31-
generate_qobject_definitions(&qobject_names, &qobject.common_attrs.cfgs, &qobject.common_attrs.docs)?,
32+
generate_qobject_definitions(&qobject_names, &qobject.common_attrs)?,
3233
generate_rust_properties(
3334
&qobject.properties,
3435
&qobject_names,
@@ -87,9 +88,11 @@ impl GeneratedRustFragment {
8788
/// Generate the C++ and Rust CXX definitions for the QObject
8889
fn generate_qobject_definitions(
8990
qobject_idents: &QObjectNames,
90-
cfgs: &[Attribute],
91-
docs: &[Attribute],
91+
common_attrs: &CommonAttrs
9292
) -> Result<GeneratedRustFragment> {
93+
let docs = &common_attrs.docs;
94+
let cfgs = &common_attrs.cfgs;
95+
9396
let cpp_class_name_rust = &qobject_idents.name.rust_unqualified();
9497
let cpp_class_name_cpp = &qobject_idents.name.cxx_unqualified();
9598

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ impl ParsedExternCxxQt {
3939
module_ident: &Ident,
4040
parent_namespace: Option<&str>,
4141
) -> Result<Self> {
42-
// TODO: support cfg on foreign mod blocks
42+
// TODO: (cfg everywhere) support cfg on foreign mod blocks
4343
let (attrs, common_attrs) = require_attributes(
4444
&foreign_mod.attrs,
45-
&["namespace", "doc", "auto_cxx_name", "auto_rust_name"],
45+
&["doc", "namespace", "auto_cxx_name", "auto_rust_name"],
4646
)?;
4747

4848
let auto_case = CaseConversion::from_attrs(&attrs)?;
@@ -72,7 +72,7 @@ impl ParsedExternCxxQt {
7272
ForeignItem::Type(foreign_ty) => {
7373
// Test that there is a #[qobject] attribute on any type
7474
//
75-
// TODO: what happens to any docs here?
75+
// TODO: (cfg everywhere) what happens to any docs here?
7676
if attribute_get_path(&foreign_ty.attrs, &["qobject"]).is_some() {
7777
let extern_ty =
7878
ParsedExternQObject::parse(foreign_ty, module_ident, parent_namespace)?;

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//
44
// SPDX-License-Identifier: MIT OR Apache-2.0
55
use crate::naming::Name;
6-
use crate::parser::{parse_base_type, require_attributes, CaseConversion};
6+
use crate::parser::{parse_base_type, require_attributes, CaseConversion, CommonAttrs};
77
use syn::{ForeignItemType, Ident, Result};
88

99
/// A representation of a QObject to be generated in an extern C++ block
@@ -14,15 +14,17 @@ pub struct ParsedExternQObject {
1414
pub declaration: ForeignItemType,
1515
/// The base class of the struct
1616
pub base_class: Option<Ident>,
17+
/// Common attrs for this object
18+
pub common_attrs: CommonAttrs,
1719
}
1820

1921
impl ParsedExternQObject {
2022
const ALLOWED_ATTRS: [&'static str; 7] = [
23+
"cfg",
24+
"doc",
2125
"cxx_name",
2226
"rust_name",
2327
"namespace",
24-
"cfg",
25-
"doc",
2628
"qobject",
2729
"base",
2830
];
@@ -32,7 +34,8 @@ impl ParsedExternQObject {
3234
module_ident: &Ident,
3335
parent_namespace: Option<&str>,
3436
) -> Result<ParsedExternQObject> {
35-
let (attributes, _common_attrs) = require_attributes(&ty.attrs, &Self::ALLOWED_ATTRS)?;
37+
// TODO: (cfg everywhere) should these have docs kept with them in the generated code?
38+
let (attributes, common_attrs) = require_attributes(&ty.attrs, &Self::ALLOWED_ATTRS)?;
3639

3740
let base_class = parse_base_type(&attributes)?;
3841

@@ -46,6 +49,7 @@ impl ParsedExternQObject {
4649
)?,
4750
declaration: ty,
4851
base_class,
52+
common_attrs,
4953
})
5054
}
5155
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl ParsedExternRustQt {
3737
module_ident: &Ident,
3838
parent_namespace: Option<&str>,
3939
) -> Result<Self> {
40-
// TODO: support cfg on foreign mod blocks
40+
// TODO: (cfg everywhere) support cfg on foreign mod blocks
4141
let (attrs, _common_attrs) = require_attributes(
4242
&foreign_mod.attrs,
4343
&["namespace", "auto_cxx_name", "auto_rust_name"],

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ pub struct ParsedInheritedMethod {
1919

2020
impl ParsedInheritedMethod {
2121
const ALLOWED_ATTRS: [&'static str; 6] = [
22+
"cfg",
23+
"doc",
2224
"cxx_name",
2325
"rust_name",
2426
"qinvokable",
25-
"doc",
2627
"inherit",
27-
"cfg",
2828
];
2929

3030
pub fn parse(method: ForeignItemFn, auto_case: CaseConversion) -> Result<Self> {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ pub struct ParsedQObject {
4747

4848
impl ParsedQObject {
4949
const ALLOWED_ATTRS: [&'static str; 11] = [
50+
"cfg",
51+
"doc",
5052
"cxx_name",
5153
"rust_name",
5254
"namespace",
53-
"cfg",
54-
"doc",
5555
"qobject",
5656
"base",
5757
"qml_element",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct ParsedSignal {
2626

2727
impl ParsedSignal {
2828
const ALLOWED_ATTRS: [&'static str; 6] =
29-
["cfg", "cxx_name", "rust_name", "inherit", "doc", "qsignal"];
29+
["cfg", "doc", "cxx_name", "rust_name", "inherit", "qsignal"];
3030

3131
#[cfg(test)]
3232
/// Test fn for creating a mocked signal from a method body

0 commit comments

Comments
 (0)