retrom_plugin_service_client/
lib.rs

1use desktop::RetromPluginServiceClient;
2pub use error::{Error, Result};
3use hyper::Uri;
4use hyper_rustls::HttpsConnector;
5use hyper_util::client::legacy::{connect::HttpConnector, Client};
6use retrom_codegen::retrom::{
7    emulator_service_client::EmulatorServiceClient, game_service_client::GameServiceClient,
8    metadata_service_client::MetadataServiceClient, platform_service_client::PlatformServiceClient,
9};
10use std::str::FromStr;
11use tauri::{
12    plugin::{Builder, TauriPlugin},
13    Manager, Runtime,
14};
15use tonic::body::Body;
16use tonic_web::{GrpcWebCall, GrpcWebClientService};
17
18mod commands;
19mod desktop;
20mod error;
21
22type GrpcWebClient = GrpcWebClientService<Client<HttpsConnector<HttpConnector>, GrpcWebCall<Body>>>;
23
24type MetadataClient = MetadataServiceClient<GrpcWebClient>;
25type GameClient = GameServiceClient<GrpcWebClient>;
26type EmulatorClient = EmulatorServiceClient<GrpcWebClient>;
27type PlatformClient = PlatformServiceClient<GrpcWebClient>;
28
29/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the retrom-plugin-service-client APIs.
30pub trait RetromPluginServiceClientExt<R: Runtime> {
31    fn service_client(&self) -> &RetromPluginServiceClient<R>;
32    fn get_metadata_client(&self) -> impl std::future::Future<Output = MetadataClient>;
33    fn get_game_client(&self) -> impl std::future::Future<Output = GameClient>;
34    fn get_emulator_client(&self) -> impl std::future::Future<Output = EmulatorClient>;
35    fn get_platform_client(&self) -> impl std::future::Future<Output = PlatformClient>;
36}
37
38impl<R: Runtime, T: Manager<R>> crate::RetromPluginServiceClientExt<R> for T {
39    fn service_client(&self) -> &RetromPluginServiceClient<R> {
40        self.state::<RetromPluginServiceClient<R>>().inner()
41    }
42
43    async fn get_platform_client(&self) -> PlatformClient {
44        let state = self.service_client();
45        let host = state.get_service_host().await;
46
47        let uri = Uri::from_str(&host).expect("Could not parse URI");
48        let grpc_web_client = state.get_grpc_web_client();
49
50        PlatformServiceClient::with_origin(grpc_web_client, uri)
51    }
52
53    async fn get_emulator_client(&self) -> EmulatorClient {
54        let state = self.service_client();
55        let host = state.get_service_host().await;
56
57        let uri = Uri::from_str(&host).expect("Could not parse URI");
58        let grpc_web_client = state.get_grpc_web_client();
59
60        EmulatorServiceClient::with_origin(grpc_web_client, uri)
61    }
62
63    async fn get_game_client(&self) -> GameClient {
64        let state = self.service_client();
65        let host = state.get_service_host().await;
66
67        let uri = Uri::from_str(&host).expect("Could not parse URI");
68        let grpc_web_client = state.get_grpc_web_client();
69
70        GameServiceClient::with_origin(grpc_web_client, uri)
71    }
72
73    async fn get_metadata_client(&self) -> MetadataClient {
74        let state = self.service_client();
75        let host = state.get_service_host().await;
76
77        let uri = Uri::from_str(&host).expect("Could not parse URI");
78        let grpc_web_client = state.get_grpc_web_client();
79
80        MetadataServiceClient::with_origin(grpc_web_client, uri)
81    }
82}
83
84/// Initializes the plugin.
85pub fn init<R: Runtime>() -> TauriPlugin<R> {
86    Builder::new("retrom-plugin-service-client")
87        .invoke_handler(tauri::generate_handler![])
88        .setup(|app, api| {
89            let retrom_plugin_service_client = desktop::init(app, api)?;
90            app.manage(retrom_plugin_service_client);
91            Ok(())
92        })
93        .build()
94}