From aa0b50698fdf33cbcc36a0998a890234ec81e0a9 Mon Sep 17 00:00:00 2001 From: Tait Hoyem Date: Wed, 19 Jun 2024 13:23:43 -0600 Subject: [PATCH] Use ChooserStatic trait, iter_into combinator --- odilia/src/tower/handlers.rs | 53 ++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/odilia/src/tower/handlers.rs b/odilia/src/tower/handlers.rs index c8275dc..fd28d5a 100644 --- a/odilia/src/tower/handlers.rs +++ b/odilia/src/tower/handlers.rs @@ -2,8 +2,11 @@ use crate::state::ScreenReaderState; use crate::tower::{ - choice::ChoiceService, choice::Chooser, - from_state::TryFromState, iter_svc::IterService, service_set::ServiceSet, Handler, ServiceExt as OdiliaServiceExt, + choice::ChoiceService, + choice::{Chooser, ChooserStatic}, + from_state::TryFromState, + service_set::ServiceSet, + Handler, ServiceExt as OdiliaServiceExt, }; use atspi::AtspiError; use atspi::BusProperties; @@ -41,6 +44,23 @@ type Error = OdiliaError; type AtspiHandler = BoxCloneService; type CommandHandler = BoxCloneService; +impl ChooserStatic<(&'static str, &'static str)> for E +where + E: BusProperties, +{ + fn identifier() -> (&'static str, &'static str) { + (E::DBUS_INTERFACE, E::DBUS_MEMBER) + } +} +impl ChooserStatic for C +where + C: CommandType, +{ + fn identifier() -> CommandDiscriminants { + C::CTYPE + } +} + impl Chooser<(&'static str, &'static str)> for Event { fn identifier(&self) -> (&'static str, &'static str) { (self.interface(), self.member()) @@ -104,7 +124,7 @@ impl Handlers { where H: Handler + Send + Clone + 'static, >::Future: Send, - C: CommandType + Send + 'static, + C: CommandType + ChooserStatic + Send + 'static, Command: TryInto, OdiliaError: From<>::Error> + From<, C>>::Error>, @@ -119,9 +139,8 @@ impl Handlers { .request_async_try_from() .with_state(Arc::clone(&self.state)) .request_try_from(); - let dn = C::CTYPE; let bs = BoxCloneService::new(try_cmd_service); - self.command.entry(dn).or_default().push(bs); + self.command.entry(C::identifier()).or_default().push(bs); Self { state: self.state, atspi: self.atspi, command: self.command } } pub fn atspi_listener(mut self, handler: H) -> Self @@ -133,6 +152,7 @@ impl Handlers { + BusProperties + TryFrom + EventProperties + + ChooserStatic<(&'static str, &'static str)> + Clone + Send + 'static, @@ -148,18 +168,17 @@ impl Handlers { .unwrap_map(|res| res.try_into_commands()) .request_async_try_from() .with_state(Arc::clone(&self.state)) - .request_try_from(); - let dn = ( - ::DBUS_INTERFACE, - ::DBUS_MEMBER, - ); - let iter_svc = IterService::new(serv.clone(), self.command.clone()).map_result( - |res: Result>>, OdiliaError>| { - res?.into_iter().flatten().collect::>() - }, - ); - let bs = BoxCloneService::new(iter_svc); - self.atspi.entry(dn).or_default().push(bs); + .request_try_from() + .iter_into(self.command.clone()) + .map_result( + |res: Result>>, OdiliaError>| { + res?.into_iter() + .flatten() + .collect::>() + }, + ); + let bs = BoxCloneService::new(serv); + self.atspi.entry(E::identifier()).or_default().push(bs); Self { state: self.state, atspi: self.atspi, command: self.command } } }