Skip to content

Commit

Permalink
cxx-qt-gen: parse all items and use Path::get_ident instead of helper
Browse files Browse the repository at this point in the history
This then allows us to easily parse extern "C++Qt" later.

Related to #577
  • Loading branch information
ahayzen-kdab committed Jul 26, 2023
1 parent e49b9db commit 16e0c8d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 57 deletions.
23 changes: 13 additions & 10 deletions crates/cxx-qt-gen/src/parser/cxxqtdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
//
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::syntax::attribute::attribute_find_path;
use crate::syntax::foreignmod::{foreign_mod_to_foreign_item_types, ForeignTypeIdentAlias};
use crate::syntax::safety::Safety;
use crate::syntax::{attribute::attribute_find_path, path::path_to_single_ident};
use crate::{
parser::{inherit::ParsedInheritedMethod, qobject::ParsedQObject, signals::ParsedSignal},
syntax::expr::expr_to_string,
Expand Down Expand Up @@ -275,16 +275,19 @@ impl ParsedCxxQtData {
// If the implementation has a T
// then this is the block of methods to be implemented on the C++ object
if let Type::Path(TypePath { path, .. }) = imp.self_ty.as_ref() {
// Find if we are an impl block for a qobject
if let Some(qobject) = self.qobjects.get_mut(&path_to_single_ident(path)?) {
// If we are a trait then process it otherwise add to others
if imp.trait_.is_some() {
qobject.parse_trait_impl(imp)?;
} else {
qobject.others.push(Item::Impl(imp));
}
// If this path is an ident then try to match to a QObject
if let Some(ident) = path.get_ident() {
// Find if we are an impl block for a qobject
if let Some(qobject) = self.qobjects.get_mut(ident) {
// If we are a trait then process it otherwise add to others
if imp.trait_.is_some() {
qobject.parse_trait_impl(imp)?;
} else {
qobject.others.push(Item::Impl(imp));
}

return Ok(None);
return Ok(None);
}
}
}

Expand Down
34 changes: 10 additions & 24 deletions crates/cxx-qt-gen/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,35 +72,21 @@ impl Parser {
// Find any QObject structs
cxx_qt_data.find_qobject_types(&items.1)?;

// Loop through any qobjects that were found
if !cxx_qt_data.qobjects.is_empty() {
for item in items.1.drain(..) {
// Try to find any CXX-Qt items, if found add them to the relevant
// qobject. Otherwise return them to be added to other
if let Some(other) = cxx_qt_data.parse_cxx_qt_item(item)? {
// Load any CXX name mappings
cxx_qt_data.populate_mappings_from_item(
&other,
&bridge_namespace,
&module.ident,
)?;

// Unknown item so add to the other list
others.push(other);
}
}
} else {
// Load any CXX name mappings
for item in &items.1 {
// Loop through items and load into qobject or others and populate mappings
for item in items.1.drain(..) {
// Try to find any CXX-Qt items, if found add them to the relevant
// qobject. Otherwise return them to be added to other
if let Some(other) = cxx_qt_data.parse_cxx_qt_item(item)? {
// Load any CXX name mappings
cxx_qt_data.populate_mappings_from_item(
item,
&other,
&bridge_namespace,
&module.ident,
)?;
}

// No qobjects found so pass everything through
others.extend(items.1);
// Unknown item so add to the other list
others.push(other);
}
}

// Add all the QObject types to the qualified mappings
Expand Down
24 changes: 1 addition & 23 deletions crates/cxx-qt-gen/src/syntax/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//
// SPDX-License-Identifier: MIT OR Apache-2.0

use syn::{spanned::Spanned, Error, Ident, Path, Result};
use syn::Path;

/// Returns whether the [syn::Path] matches a given string slice
pub fn path_compare_str(path: &Path, string: &[&str]) -> bool {
Expand All @@ -20,18 +20,6 @@ pub fn path_compare_str(path: &Path, string: &[&str]) -> bool {
== 0
}

/// Returns the first [syn::Ident] from the [syn::Path] segments, errors if there are none or many
pub fn path_to_single_ident(path: &Path) -> Result<Ident> {
if path.segments.len() == 1 {
Ok(path.segments[0].ident.clone())
} else {
Err(Error::new(
path.span(),
"Expected only one segment in the Path",
))
}
}

#[cfg(test)]
mod tests {
use syn::parse_quote;
Expand All @@ -46,14 +34,4 @@ mod tests {
assert!(!path_compare_str(&path, &["a", "c", "b"]));
assert!(!path_compare_str(&path, &["a", "b", "c", "d"]));
}

#[test]
fn test_path_to_single_ident() {
let path: Path = parse_quote! { a::b::c };
assert!(path_to_single_ident(&path).is_err());

let path: Path = parse_quote! { a };
let ident = path_to_single_ident(&path).unwrap();
assert_eq!(ident, "a");
}
}

0 comments on commit 16e0c8d

Please sign in to comment.