Skip to main content

scatterbrain/api/
types.rs

1#[cfg(feature = "flutter")]
2pub use crate::api::api::SbSession;
3use crate::api::proto::PairingSynAck;
4pub use crate::response::{Identity, Message};
5pub use core::future::Future;
6#[cfg(feature = "flutter")]
7use flutter_rust_bridge::frb;
8#[cfg(feature = "flutter")]
9pub use flutter_rust_bridge::DartFnFuture;
10pub use serde::{Deserialize, Serialize};
11pub use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
12
13pub use std::pin::Pin;
14
15use uuid::Uuid;
16#[derive(Serialize, Deserialize)]
17pub struct CryptoConfig {
18    pub secretkey: String,
19    pub pubkey: String,
20    pub remotekey: Option<String>,
21}
22
23impl CryptoConfig {
24    pub fn generate() -> CryptoConfig {
25        SessionState::default().b64()
26    }
27}
28
29pub use crate::api::proto::{
30    Ack, CryptoMessage, GetEvents, GetIdentityCommand, GetMessagesCmd, IdentityResponse,
31    ImportIdentityCommand, ImportIdentityResponse, MessageResponse, MessageType, PairingAck,
32    PairingInitiate, PairingRequest, SbEvents, SendMessageCmd, TypePrefix, UnitResponse,
33};
34#[cfg_attr(feature = "flutter", flutter_rust_bridge::frb(non_opaque))]
35pub type DartFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
36#[cfg_attr(feature = "flutter", flutter_rust_bridge::frb(non_opaque))]
37pub type DartSyncFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + Sync + 'a>>;
38
39use crate::crypto::{EncodeB64, SessionState};
40
41pub trait GetType {
42    fn get_type() -> MessageType;
43    fn get_type_message(&self) -> TypePrefix {
44        TypePrefix {
45            message_type: Self::get_type().into(),
46        }
47    }
48}
49
50impl GetType for GetMessagesCmd {
51    fn get_type() -> MessageType {
52        MessageType::GetMessage
53    }
54}
55
56impl GetType for GetIdentityCommand {
57    fn get_type() -> MessageType {
58        MessageType::GetIdentity
59    }
60}
61
62impl GetType for SendMessageCmd {
63    fn get_type() -> MessageType {
64        MessageType::SendMessage
65    }
66}
67
68impl GetType for MessageType {
69    fn get_type() -> MessageType {
70        MessageType::Message
71    }
72}
73
74impl GetType for UnitResponse {
75    fn get_type() -> MessageType {
76        MessageType::UnitResponse
77    }
78}
79
80impl GetType for CryptoMessage {
81    fn get_type() -> MessageType {
82        MessageType::CryptoMessage
83    }
84}
85
86impl GetType for PairingRequest {
87    fn get_type() -> MessageType {
88        MessageType::PairingRequest
89    }
90}
91
92impl GetType for PairingInitiate {
93    fn get_type() -> MessageType {
94        MessageType::PairingInitiate
95    }
96}
97
98impl GetType for PairingAck {
99    fn get_type() -> MessageType {
100        MessageType::PairingAck
101    }
102}
103
104impl GetType for Ack {
105    fn get_type() -> MessageType {
106        MessageType::Ack
107    }
108}
109
110impl GetType for IdentityResponse {
111    fn get_type() -> MessageType {
112        MessageType::IdentityResponse
113    }
114}
115
116impl GetType for MessageResponse {
117    fn get_type() -> MessageType {
118        MessageType::MessageResponse
119    }
120}
121
122impl GetType for ImportIdentityCommand {
123    fn get_type() -> MessageType {
124        MessageType::ImportIdentity
125    }
126}
127
128impl GetType for ImportIdentityResponse {
129    fn get_type() -> MessageType {
130        MessageType::ImportIdentityResponse
131    }
132}
133
134impl GetType for GetEvents {
135    fn get_type() -> MessageType {
136        MessageType::GetEvents
137    }
138}
139
140impl GetType for SbEvents {
141    fn get_type() -> MessageType {
142        MessageType::DesktopEvents
143    }
144}
145
146impl GetType for PairingSynAck {
147    fn get_type() -> MessageType {
148        MessageType::PairingSynack
149    }
150}
151
152#[derive(Debug)]
153#[cfg_attr(feature = "flutter", frb(non_opaque))]
154pub enum ImportIdentityState {
155    Initiated(Uuid),
156    Complete(Uuid),
157}