From b035fd963d6acd97b58f92305352a097912978e9 Mon Sep 17 00:00:00 2001 From: Andrew Hayzen Date: Wed, 26 Jul 2023 14:21:56 +0100 Subject: [PATCH] cxx-qt-gen: parse all items and use Path::get_ident instead of helper This then allows us to easily parse extern "C++Qt" later. Related to #577 --- crates/cxx-qt-gen/src/parser/cxxqtdata.rs | 23 ++++++++------- crates/cxx-qt-gen/src/parser/mod.rs | 34 +++++++---------------- crates/cxx-qt-gen/src/syntax/path.rs | 24 +--------------- 3 files changed, 24 insertions(+), 57 deletions(-) diff --git a/crates/cxx-qt-gen/src/parser/cxxqtdata.rs b/crates/cxx-qt-gen/src/parser/cxxqtdata.rs index 65a3b7ee1..1c6033942 100644 --- a/crates/cxx-qt-gen/src/parser/cxxqtdata.rs +++ b/crates/cxx-qt-gen/src/parser/cxxqtdata.rs @@ -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, @@ -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); + } } } diff --git a/crates/cxx-qt-gen/src/parser/mod.rs b/crates/cxx-qt-gen/src/parser/mod.rs index 7fbd1b398..97399e5d1 100644 --- a/crates/cxx-qt-gen/src/parser/mod.rs +++ b/crates/cxx-qt-gen/src/parser/mod.rs @@ -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 diff --git a/crates/cxx-qt-gen/src/syntax/path.rs b/crates/cxx-qt-gen/src/syntax/path.rs index f3376db31..5cb2ebbcf 100644 --- a/crates/cxx-qt-gen/src/syntax/path.rs +++ b/crates/cxx-qt-gen/src/syntax/path.rs @@ -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 { @@ -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 { - 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; @@ -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"); - } }