Skip to content

Commit c361163

Browse files
Return docs and cfgs from require_attributes, and make common struct
1 parent a332032 commit c361163

File tree

11 files changed

+43
-46
lines changed

11 files changed

+43
-46
lines changed

crates/cxx-qt-gen/bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/home/ben/cxx-qt/crates/cxx-qt-gen/src/generator/naming/property.rs

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

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

6-
use crate::parser::extract_docs;
76
use crate::{
87
parser::{
98
externqobject::ParsedExternQObject, require_attributes, signals::ParsedSignal,
@@ -40,13 +39,11 @@ impl ParsedExternCxxQt {
4039
parent_namespace: Option<&str>,
4140
) -> Result<Self> {
4241
// TODO: support cfg on foreign mod blocks
43-
let attrs = require_attributes(
42+
let (attrs, common_attrs) = require_attributes(
4443
&foreign_mod.attrs,
4544
&["namespace", "doc", "auto_cxx_name", "auto_rust_name"],
4645
)?;
4746

48-
let docs = extract_docs(&foreign_mod.attrs);
49-
5047
let auto_case = CaseConversion::from_attrs(&attrs)?;
5148

5249
let namespace = attrs
@@ -58,7 +55,7 @@ impl ParsedExternCxxQt {
5855

5956
let mut extern_cxx_block = ParsedExternCxxQt {
6057
namespace,
61-
docs,
58+
docs: common_attrs.docs,
6259
unsafety: foreign_mod.unsafety,
6360
..Default::default()
6461
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl ParsedExternQObject {
3232
module_ident: &Ident,
3333
parent_namespace: Option<&str>,
3434
) -> Result<ParsedExternQObject> {
35-
let attributes = require_attributes(&ty.attrs, &Self::ALLOWED_ATTRS)?;
35+
let (attributes, _common_attrs) = require_attributes(&ty.attrs, &Self::ALLOWED_ATTRS)?;
3636

3737
let base_class = parse_base_type(&attributes)?;
3838

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

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

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

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

6-
use crate::parser::{
7-
extract_cfgs, extract_docs, method::MethodFields, require_attributes, CaseConversion,
8-
};
6+
use crate::parser::{method::MethodFields, require_attributes, CaseConversion};
97
use core::ops::Deref;
108
use quote::format_ident;
119
use std::ops::DerefMut;
@@ -32,14 +30,12 @@ impl ParsedInheritedMethod {
3230
];
3331

3432
pub fn parse(method: ForeignItemFn, auto_case: CaseConversion) -> Result<Self> {
35-
require_attributes(&method.attrs, &Self::ALLOWED_ATTRS)?;
36-
let docs = extract_docs(&method.attrs);
37-
let cfgs = extract_cfgs(&method.attrs);
33+
let (_attrs, common_attrs) = require_attributes(&method.attrs, &Self::ALLOWED_ATTRS)?;
3834

3935
Ok(Self {
4036
method_fields: MethodFields::parse(method, auto_case)?,
41-
docs,
42-
cfgs,
37+
docs: common_attrs.docs,
38+
cfgs: common_attrs.cfgs,
4339
})
4440
}
4541

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use crate::parser::CaseConversion;
66
use crate::{
77
naming::Name,
8-
parser::{extract_cfgs, parameter::ParsedFunctionParameter, require_attributes},
8+
parser::{parameter::ParsedFunctionParameter, require_attributes},
99
syntax::{foreignmod, types},
1010
};
1111
use core::ops::Deref;
@@ -121,8 +121,7 @@ impl ParsedMethod {
121121
unsafe_block: bool,
122122
) -> Result<Self> {
123123
let fields = MethodFields::parse(method, auto_case)?;
124-
let attrs = require_attributes(&fields.method.attrs, &Self::ALLOWED_ATTRS)?;
125-
let cfgs = extract_cfgs(&fields.method.attrs);
124+
let (attrs, common_attrs) = require_attributes(&fields.method.attrs, &Self::ALLOWED_ATTRS)?;
126125

127126
// Determine if the method is invokable
128127
let is_qinvokable = attrs.contains_key("qinvokable");
@@ -134,7 +133,7 @@ impl ParsedMethod {
134133
specifiers,
135134
is_qinvokable,
136135
is_pure,
137-
cfgs,
136+
cfgs: common_attrs.cfgs,
138137
unsafe_block,
139138
})
140139
}

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,29 @@ fn split_path(path_str: &str) -> Vec<&str> {
115115
path
116116
}
117117

118+
/// Attributes which should be passed through, and are available on most things
119+
pub struct CommonAttrs {
120+
docs: Vec<Attribute>,
121+
cfgs: Vec<Attribute>,
122+
}
123+
118124
/// Collects a Map of all attributes found from the allowed list
119125
/// Will error if an attribute which is not in the allowed list is found
126+
///
127+
/// Has the option to allow a set of common attributes such as doc, cfg, etc...
120128
pub fn require_attributes<'a>(
121129
attrs: &'a [Attribute],
122130
allowed: &'a [&str],
123-
) -> Result<BTreeMap<&'a str, &'a Attribute>> {
131+
) -> Result<(BTreeMap<&'a str, &'a Attribute>, CommonAttrs)> {
124132
let mut output = BTreeMap::default();
125133
for attr in attrs {
126134
let index = allowed
127135
.iter()
128136
.position(|string| path_compare_str(attr.meta.path(), &split_path(string)));
129137
if let Some(index) = index {
130138
output.insert(allowed[index], attr); // Doesn't error on duplicates
131-
} else {
139+
}
140+
else {
132141
return Err(Error::new(
133142
attr.span(),
134143
format!(
@@ -138,7 +147,11 @@ pub fn require_attributes<'a>(
138147
));
139148
}
140149
}
141-
Ok(output)
150+
let common = CommonAttrs {
151+
docs: extract_docs(attrs),
152+
cfgs: extract_cfgs(attrs),
153+
};
154+
Ok((output, common))
142155
}
143156

144157
// Extract base identifier from attribute
@@ -196,7 +209,7 @@ pub struct Parser {
196209

197210
impl Parser {
198211
fn parse_mod_attributes(module: &mut ItemMod) -> Result<Option<String>> {
199-
let attrs = require_attributes(&module.attrs, &["doc", "cxx_qt::bridge"])?;
212+
let (attrs, _common_attrs) = require_attributes(&module.attrs, &["doc", "cxx_qt::bridge"])?;
200213
let mut namespace = None;
201214

202215
// Check for the cxx_qt::bridge attribute

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

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

6-
use crate::parser::{extract_cfgs, extract_docs, CaseConversion};
6+
use crate::parser::CaseConversion;
77
use crate::{naming::Name, parser::require_attributes, syntax::path::path_compare_str};
88
use quote::ToTokens;
99
use syn::{Attribute, Ident, ItemEnum, Result, Variant};
@@ -60,9 +60,7 @@ impl ParsedQEnum {
6060
parent_namespace: Option<&str>,
6161
module: &Ident,
6262
) -> Result<Self> {
63-
require_attributes(&qenum.attrs, &Self::ALLOWED_ATTRS)?;
64-
let cfgs = extract_cfgs(&qenum.attrs);
65-
let docs = extract_docs(&qenum.attrs);
63+
let (_attrs, common_attrs) = require_attributes(&qenum.attrs, &Self::ALLOWED_ATTRS)?;
6664

6765
if qenum.variants.is_empty() {
6866
return Err(syn::Error::new_spanned(
@@ -96,8 +94,8 @@ impl ParsedQEnum {
9694
name,
9795
qobject,
9896
variants,
99-
docs,
100-
cfgs,
97+
docs: common_attrs.docs,
98+
cfgs: common_attrs.cfgs,
10199
item: qenum,
102100
})
103101
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct ParsedQNamespace {
1515

1616
impl ParsedQNamespace {
1717
pub fn parse(mac: ItemMacro) -> Result<Self> {
18-
let attrs = require_attributes(&mac.attrs, &["qml_element"])?;
18+
let (attrs, _common_attrs) = require_attributes(&mac.attrs, &["qml_element"])?;
1919
let namespace_literal: LitStr = syn::parse2(mac.mac.tokens)?;
2020
let namespace = namespace_literal.value();
2121
if namespace.contains(char::is_whitespace) {

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55

66
use crate::{
77
naming::Name,
8-
parser::{extract_cfgs, property::ParsedQProperty, require_attributes},
8+
parser::{property::ParsedQProperty, require_attributes, parse_base_type, CaseConversion},
99
syntax::{expr::expr_to_string, foreignmod::ForeignTypeIdentAlias, path::path_compare_str},
1010
};
1111
#[cfg(test)]
1212
use quote::format_ident;
13-
14-
use crate::parser::{extract_docs, parse_base_type, CaseConversion};
1513
use syn::{Attribute, Error, Ident, Meta, Result};
1614

1715
/// Metadata for registering QML element
@@ -88,9 +86,7 @@ impl ParsedQObject {
8886
module: &Ident,
8987
auto_case: CaseConversion,
9088
) -> Result<Self> {
91-
let attributes = require_attributes(&declaration.attrs, &Self::ALLOWED_ATTRS)?;
92-
let cfgs = extract_cfgs(&declaration.attrs);
93-
let docs = extract_docs(&declaration.attrs);
89+
let (attributes, common_attrs) = require_attributes(&declaration.attrs, &Self::ALLOWED_ATTRS)?;
9490

9591
let has_qobject_macro = attributes.contains_key("qobject");
9692

@@ -128,13 +124,13 @@ impl ParsedQObject {
128124
properties,
129125
qml_metadata,
130126
has_qobject_macro,
131-
cfgs,
132-
docs,
127+
cfgs: common_attrs.cfgs,
128+
docs: common_attrs.docs,
133129
})
134130
}
135131

136132
fn parse_qml_metadata(name: &Name, attrs: &[Attribute]) -> Result<Option<QmlElementMetadata>> {
137-
let attributes = require_attributes(attrs, &Self::ALLOWED_ATTRS)?;
133+
let (attributes, _common_attributes) = require_attributes(attrs, &Self::ALLOWED_ATTRS)?;
138134
if let Some(attr) = attributes.get("qml_element") {
139135
// Extract the name of the qml_element from macro, else use the c++ name
140136
// This will use the name provided by cxx_name if that attr was present

0 commit comments

Comments
 (0)