Skip to content

Commit

Permalink
refactor: Use Name in SignalName&SignalHelperName
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonMatthesKDAB committed Jun 4, 2024
1 parent f4619b3 commit d9b362f
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 88 deletions.
8 changes: 4 additions & 4 deletions crates/cxx-qt-gen/src/generator/cpp/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub fn generate_cpp_signal(
let idents_helper = QSignalHelperName::new(&idents, qobject_name)?;

let signal_ident = idents.name.cxx_unqualified();
let free_connect_ident_cpp = idents_helper.connect_name.cpp;
let free_connect_ident_cpp = idents_helper.connect_name.cxx_unqualified();

// Retrieve the parameters for the signal
let parameters = parameter_types_and_values(&signal.parameters, type_names, qobject_name)?;
Expand Down Expand Up @@ -213,7 +213,7 @@ pub fn generate_cpp_signals(
mod tests {
use super::*;

use crate::generator::naming::{qobject::tests::create_qobjectname, CombinedIdent};
use crate::generator::naming::qobject::tests::create_qobjectname;
use crate::parser::parameter::ParsedFunctionParameter;
use indoc::indoc;
use pretty_assertions::assert_str_eq;
Expand Down Expand Up @@ -628,7 +628,7 @@ mod tests {
r#"
namespace mynamespace::rust::cxxqtgen1 {
::QMetaObject::Connection
ObjRust_signalCxxNameConnect(mynamespace::ObjCpp& self, ::mynamespace::rust::cxxqtgen1::ObjRustCxxQtSignalHandlersignalCxxName closure, ::Qt::ConnectionType type);
ObjCpp_signalCxxNameConnect(mynamespace::ObjCpp& self, ::mynamespace::rust::cxxqtgen1::ObjRustCxxQtSignalHandlersignalCxxName closure, ::Qt::ConnectionType type);
} // namespace mynamespace::rust::cxxqtgen1
"#}
);
Expand Down Expand Up @@ -662,7 +662,7 @@ mod tests {
namespace mynamespace::rust::cxxqtgen1 {
::QMetaObject::Connection
ObjRust_signalCxxNameConnect(mynamespace::ObjCpp& self, ::mynamespace::rust::cxxqtgen1::ObjRustCxxQtSignalHandlersignalCxxName closure, ::Qt::ConnectionType type)
ObjCpp_signalCxxNameConnect(mynamespace::ObjCpp& self, ::mynamespace::rust::cxxqtgen1::ObjRustCxxQtSignalHandlersignalCxxName closure, ::Qt::ConnectionType type)
{
return ::QObject::connect(
&self,
Expand Down
59 changes: 30 additions & 29 deletions crates/cxx-qt-gen/src/generator/naming/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,42 @@
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
use crate::parser::signals::ParsedSignal;
use crate::{generator::naming::CombinedIdent, naming::Name};
use crate::{naming::Name, parser::signals::ParsedSignal};
use convert_case::{Case, Casing};
use quote::format_ident;
use syn::{Ident, Result};

/// Names for parts of a Q_SIGNAL
pub struct QSignalName {
pub name: Name,
pub connect_name: CombinedIdent,
pub connect_name: Name,
pub on_name: Ident,
}

impl From<&ParsedSignal> for QSignalName {
fn from(signal: &ParsedSignal) -> Self {
Self {
name: signal.name.clone(),
connect_name: CombinedIdent::connect_from_signal(&signal.name),
connect_name: connect_name_from_signal(&signal.name),
on_name: on_from_signal(signal.name.rust_unqualified()),
}
}
}

fn on_from_signal(ident: &Ident) -> Ident {
format_ident!("on_{}", ident.to_string().to_case(Case::Snake))
fn connect_name_from_signal(name: &Name) -> Name {
name.clone()
.with_rust_name(format_ident!("connect_{}", name.rust_unqualified()))
// Use signalConnect instead of onSignal here so that we don't
// create a C++ name that is similar to the QML naming scheme for signals
.with_cxx_name(format!("{}Connect", name.cxx_unqualified()))
}

impl CombinedIdent {
fn connect_from_signal(ident: &Name) -> Self {
Self {
// Use signalConnect instead of onSignal here so that we don't
// create a C++ name that is similar to the QML naming scheme for signals
cpp: format_ident!("{}Connect", ident.cxx_unqualified().to_case(Case::Camel)),
rust: format_ident!(
"connect_{}",
ident.rust_unqualified().to_string().to_case(Case::Snake)
),
}
}
fn on_from_signal(ident: &Ident) -> Ident {
format_ident!("on_{}", ident.to_string().to_case(Case::Snake))
}

pub struct QSignalHelperName {
pub connect_name: CombinedIdent,
pub connect_name: Name,
pub function_call: Ident,
pub function_drop: Ident,
pub handler_alias: Ident,
Expand Down Expand Up @@ -80,13 +73,21 @@ impl QSignalHelperName {
namespace.join("::")
};

let connect_name = Name::new(format_ident!(
"{}_{}",
qobject_name.rust_unqualified(),
idents.connect_name.rust_unqualified()
))
.with_cxx_name(format!(
"{}_{}",
qobject_name.cxx_unqualified(),
idents.connect_name.cxx_unqualified()
));

// TODO: in the future we might improve the naming of the methods
// to avoid collisions (maybe use a separator similar to how CXX uses $?)
Ok(Self {
connect_name: CombinedIdent {
cpp: format_ident!("{}_{}", qobject_ident, idents.connect_name.cpp),
rust: format_ident!("{}_{}", qobject_ident, idents.connect_name.rust),
},
connect_name,
function_drop: format_ident!("drop_{qobject_ident}_signal_handler_{signal_ident}"),
function_call: format_ident!("call_{qobject_ident}_signal_handler_{signal_ident}"),
handler_alias_namespaced: format!("::{namespace}::{handler_alias}"),
Expand Down Expand Up @@ -125,10 +126,10 @@ mod tests {
names.name.rust_unqualified(),
&format_ident!("data_changed")
);
assert_eq!(names.connect_name.cpp, format_ident!("dataChangedConnect"));
assert_eq!(names.connect_name.cxx_unqualified(), "dataChangedConnect");
assert_eq!(
names.connect_name.rust,
format_ident!("connect_data_changed")
names.connect_name.rust_unqualified(),
&format_ident!("connect_data_changed")
);
assert_eq!(names.on_name, format_ident!("on_data_changed"));
}
Expand All @@ -155,10 +156,10 @@ mod tests {
names.name.rust_unqualified(),
&format_ident!("existing_signal")
);
assert_eq!(names.connect_name.cpp, format_ident!("baseNameConnect"));
assert_eq!(names.connect_name.cxx_unqualified(), "baseNameConnect");
assert_eq!(
names.connect_name.rust,
format_ident!("connect_existing_signal")
names.connect_name.rust_unqualified(),
&format_ident!("connect_existing_signal")
);
assert_eq!(names.on_name, format_ident!("on_existing_signal"));
}
Expand Down
12 changes: 6 additions & 6 deletions crates/cxx-qt-gen/src/generator/rust/property/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ mod tests {

#[doc(hidden)]
#[namespace = "rust::cxxqtgen1"]
#[rust_name = "MyObject_connect_trivial_property_changed"]
fn MyObject_trivialPropertyChangedConnect(self_value: Pin<&mut MyObject>, signal_handler: MyObjectCxxQtSignalHandlertrivialPropertyChanged, conn_type: CxxQtConnectionType) -> CxxQtQMetaObjectConnection;
#[cxx_name = "MyObject_trivialPropertyChangedConnect"]
fn MyObject_connect_trivial_property_changed(self_value: Pin<&mut MyObject>, signal_handler: MyObjectCxxQtSignalHandlertrivialPropertyChanged, conn_type: CxxQtConnectionType) -> CxxQtQMetaObjectConnection;
}
},
);
Expand Down Expand Up @@ -412,8 +412,8 @@ mod tests {

#[doc(hidden)]
#[namespace = "rust::cxxqtgen1"]
#[rust_name = "MyObject_connect_opaque_property_changed"]
fn MyObject_opaquePropertyChangedConnect(self_value: Pin<&mut MyObject>, signal_handler: MyObjectCxxQtSignalHandleropaquePropertyChanged, conn_type: CxxQtConnectionType) -> CxxQtQMetaObjectConnection;
#[cxx_name = "MyObject_opaquePropertyChangedConnect"]
fn MyObject_connect_opaque_property_changed(self_value: Pin<&mut MyObject>, signal_handler: MyObjectCxxQtSignalHandleropaquePropertyChanged, conn_type: CxxQtConnectionType) -> CxxQtQMetaObjectConnection;
}
},
);
Expand Down Expand Up @@ -537,8 +537,8 @@ mod tests {

#[doc(hidden)]
#[namespace = "rust::cxxqtgen1"]
#[rust_name = "MyObject_connect_unsafe_property_changed"]
fn MyObject_unsafePropertyChangedConnect(self_value: Pin<&mut MyObject>, signal_handler: MyObjectCxxQtSignalHandlerunsafePropertyChanged, conn_type: CxxQtConnectionType) -> CxxQtQMetaObjectConnection;
#[cxx_name = "MyObject_unsafePropertyChangedConnect"]
fn MyObject_connect_unsafe_property_changed(self_value: Pin<&mut MyObject>, signal_handler: MyObjectCxxQtSignalHandlerunsafePropertyChanged, conn_type: CxxQtConnectionType) -> CxxQtQMetaObjectConnection;
}
},
);
Expand Down
37 changes: 18 additions & 19 deletions crates/cxx-qt-gen/src/generator/rust/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ pub fn generate_rust_signal(
let qobject_name_rust = qobject_name.rust_unqualified();

let signal_name_cpp = idents.name.cxx_unqualified();
let connect_ident_rust = idents.connect_name.rust;
let connect_ident_rust = idents.connect_name.rust_unqualified();
let on_ident_rust = idents.on_name;
let original_method = &signal.method;

let free_connect_ident_cpp = idents_helper.connect_name.cpp;
let free_connect_ident_rust = idents_helper.connect_name.rust;
let free_connect_ident_rust_str = free_connect_ident_rust.to_string();
let free_connect_ident_cpp = idents_helper.connect_name.cxx_unqualified();
let free_connect_ident_rust = idents_helper.connect_name.rust_unqualified();

let parameters_cxx: Vec<FnArg> = signal
.parameters
Expand Down Expand Up @@ -111,8 +110,8 @@ pub fn generate_rust_signal(

#[doc(hidden)]
#[namespace = #namespace_str]
#[rust_name = #free_connect_ident_rust_str]
fn #free_connect_ident_cpp(self_value: #self_type_cxx, signal_handler: #signal_handler_alias, conn_type: CxxQtConnectionType) -> CxxQtQMetaObjectConnection;
#[cxx_name = #free_connect_ident_cpp]
fn #free_connect_ident_rust(self_value: #self_type_cxx, signal_handler: #signal_handler_alias, conn_type: CxxQtConnectionType) -> CxxQtQMetaObjectConnection;
}
});

Expand Down Expand Up @@ -247,7 +246,7 @@ pub fn generate_rust_signals(
mod tests {
use super::*;

use crate::generator::naming::{qobject::tests::create_qobjectname, CombinedIdent};
use crate::generator::naming::qobject::tests::create_qobjectname;
use crate::parser::parameter::ParsedFunctionParameter;
use crate::tests::assert_tokens_eq;
use quote::{format_ident, quote};
Expand Down Expand Up @@ -299,8 +298,8 @@ mod tests {

#[doc(hidden)]
#[namespace = "rust::cxxqtgen1"]
#[rust_name = "MyObject_connect_ready"]
fn MyObject_readyConnect(self_value: Pin<&mut MyObject>, signal_handler: MyObjectCxxQtSignalHandlerready, conn_type: CxxQtConnectionType) -> CxxQtQMetaObjectConnection;
#[cxx_name = "MyObject_readyConnect"]
fn MyObject_connect_ready(self_value: Pin<&mut MyObject>, signal_handler: MyObjectCxxQtSignalHandlerready, conn_type: CxxQtConnectionType) -> CxxQtQMetaObjectConnection;
}
},
);
Expand Down Expand Up @@ -462,8 +461,8 @@ mod tests {

#[doc(hidden)]
#[namespace = "rust::cxxqtgen1"]
#[rust_name = "MyObject_connect_data_changed"]
fn MyObject_dataChangedConnect(self_value: Pin<&mut MyObject>, signal_handler: MyObjectCxxQtSignalHandlerdataChanged, conn_type: CxxQtConnectionType) -> CxxQtQMetaObjectConnection;
#[cxx_name = "MyObject_dataChangedConnect"]
fn MyObject_connect_data_changed(self_value: Pin<&mut MyObject>, signal_handler: MyObjectCxxQtSignalHandlerdataChanged, conn_type: CxxQtConnectionType) -> CxxQtQMetaObjectConnection;
}
},
);
Expand Down Expand Up @@ -620,8 +619,8 @@ mod tests {

#[doc(hidden)]
#[namespace = "rust::cxxqtgen1"]
#[rust_name = "MyObject_connect_unsafe_signal"]
fn MyObject_unsafeSignalConnect(self_value: Pin<&mut MyObject>, signal_handler: MyObjectCxxQtSignalHandlerunsafeSignal, conn_type: CxxQtConnectionType) -> CxxQtQMetaObjectConnection;
#[cxx_name = "MyObject_unsafeSignalConnect"]
fn MyObject_connect_unsafe_signal(self_value: Pin<&mut MyObject>, signal_handler: MyObjectCxxQtSignalHandlerunsafeSignal, conn_type: CxxQtConnectionType) -> CxxQtQMetaObjectConnection;
}
},
);
Expand Down Expand Up @@ -773,8 +772,8 @@ mod tests {

#[doc(hidden)]
#[namespace = "rust::cxxqtgen1"]
#[rust_name = "MyObject_connect_existing_signal"]
fn MyObject_baseNameConnect(self_value: Pin<&mut MyObject>, signal_handler: MyObjectCxxQtSignalHandlerbaseName, conn_type: CxxQtConnectionType) -> CxxQtQMetaObjectConnection;
#[cxx_name = "MyObject_baseNameConnect"]
fn MyObject_connect_existing_signal(self_value: Pin<&mut MyObject>, signal_handler: MyObjectCxxQtSignalHandlerbaseName, conn_type: CxxQtConnectionType) -> CxxQtQMetaObjectConnection;
}
},
);
Expand Down Expand Up @@ -925,8 +924,8 @@ mod tests {

#[doc(hidden)]
#[namespace = "rust::cxxqtgen1"]
#[rust_name = "MyObject_connect_ready"]
fn MyObject_readyConnect(self_value: Pin<&mut MyObject>, signal_handler: MyObjectCxxQtSignalHandlerready, conn_type: CxxQtConnectionType) -> CxxQtQMetaObjectConnection;
#[cxx_name = "MyObject_readyConnect"]
fn MyObject_connect_ready(self_value: Pin<&mut MyObject>, signal_handler: MyObjectCxxQtSignalHandlerready, conn_type: CxxQtConnectionType) -> CxxQtQMetaObjectConnection;
}
},
);
Expand Down Expand Up @@ -1069,8 +1068,8 @@ mod tests {

#[doc(hidden)]
#[namespace = "rust::cxxqtgen1"]
#[rust_name = "MyObject_connect_ready"]
fn MyObject_readyConnect(self_value: Pin<&mut MyObject>, signal_handler: MyObjectCxxQtSignalHandlerready, conn_type: CxxQtConnectionType) -> CxxQtQMetaObjectConnection;
#[cxx_name = "MyObject_readyConnect"]
fn MyObject_connect_ready(self_value: Pin<&mut MyObject>, signal_handler: MyObjectCxxQtSignalHandlerready, conn_type: CxxQtConnectionType) -> CxxQtQMetaObjectConnection;
}
},
);
Expand Down
4 changes: 2 additions & 2 deletions crates/cxx-qt-gen/test_outputs/passthrough_and_naming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static_assert(

namespace mynamespace::rust::cxxqtgen1 {
::QMetaObject::Connection
ExternObject_dataReadyConnect(
ExternObjectCpp_dataReadyConnect(
mynamespace::ExternObjectCpp& self,
::mynamespace::rust::cxxqtgen1::ExternObjectCxxQtSignalHandlerdataReady
closure,
Expand Down Expand Up @@ -152,7 +152,7 @@ static_assert(

namespace mynamespace::rust::cxxqtgen1 {
::QMetaObject::Connection
ExternObject_errorOccurredConnect(
ExternObjectCpp_errorOccurredConnect(
mynamespace::ExternObjectCpp& self,
::mynamespace::rust::cxxqtgen1::ExternObjectCxxQtSignalHandlererrorOccurred
closure,
Expand Down
4 changes: 2 additions & 2 deletions crates/cxx-qt-gen/test_outputs/passthrough_and_naming.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ QPushButton_clickedConnect(

namespace mynamespace::rust::cxxqtgen1 {
::QMetaObject::Connection
ExternObject_dataReadyConnect(
ExternObjectCpp_dataReadyConnect(
mynamespace::ExternObjectCpp& self,
::mynamespace::rust::cxxqtgen1::ExternObjectCxxQtSignalHandlerdataReady
closure,
Expand All @@ -79,7 +79,7 @@ ExternObject_dataReadyConnect(

namespace mynamespace::rust::cxxqtgen1 {
::QMetaObject::Connection
ExternObject_errorOccurredConnect(
ExternObjectCpp_errorOccurredConnect(
mynamespace::ExternObjectCpp& self,
::mynamespace::rust::cxxqtgen1::ExternObjectCxxQtSignalHandlererrorOccurred
closure,
Expand Down
Loading

0 comments on commit d9b362f

Please sign in to comment.