crux_platform/
lib.rs

1#![deny(clippy::pedantic)]
2//! TODO mod docs
3
4pub mod command;
5
6use crux_core::capability::{CapabilityContext, Operation};
7use crux_core::macros::Capability;
8use serde::{Deserialize, Serialize};
9
10#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
11pub struct PlatformRequest;
12
13// TODO revisit this
14#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
15pub struct PlatformResponse(pub String);
16
17impl Operation for PlatformRequest {
18    type Output = PlatformResponse;
19}
20
21#[derive(Capability)]
22pub struct Platform<Ev> {
23    context: CapabilityContext<PlatformRequest, Ev>,
24}
25
26impl<Ev> Platform<Ev>
27where
28    Ev: 'static,
29{
30    #[must_use]
31    pub fn new(context: CapabilityContext<PlatformRequest, Ev>) -> Self {
32        Self { context }
33    }
34
35    pub fn get<F>(&self, callback: F)
36    where
37        F: FnOnce(PlatformResponse) -> Ev + Send + Sync + 'static,
38    {
39        self.context.spawn({
40            let context = self.context.clone();
41            async move {
42                let response = context.request_from_shell(PlatformRequest).await;
43
44                context.update_app(callback(response));
45            }
46        });
47    }
48}