Skip to main content

remowt_link_shared/
lib.rs

1use bifrostlink::error::{ErrorT, ListenerForYourRequestHasBeenDeadError, ResponseError};
2use bifrostlink::notification;
3use bifrostlink::packet::OpaquePacketWrapper;
4use bifrostlink::AddressT;
5use serde::de::DeserializeOwned;
6use serde::{Deserialize, Serialize};
7
8pub mod editor;
9pub mod port;
10
11#[derive(Clone, Serialize, Hash, Eq, Debug, PartialEq, Deserialize)]
12pub enum Address {
13	User,
14	Agent,
15	AgentPrivileged,
16	Plugin(u16),
17}
18impl AddressT for Address {}
19
20pub mod plugin;
21
22#[derive(thiserror::Error, Debug)]
23pub enum Error {
24	#[error("listener is dead")]
25	ListenerDead,
26	#[error("response: {0}")]
27	Response(String),
28
29	#[error(transparent)]
30	Ui(#[from] remowt_ui_prompt::Error),
31}
32
33impl From<ListenerForYourRequestHasBeenDeadError> for Error {
34	fn from(_value: ListenerForYourRequestHasBeenDeadError) -> Self {
35		Self::ListenerDead
36	}
37}
38impl From<serde_json::Error> for Error {
39	fn from(_value: serde_json::Error) -> Self {
40		Self::ListenerDead
41	}
42}
43impl From<Error> for ResponseError {
44	fn from(val: Error) -> Self {
45		ResponseError(val.to_string())
46	}
47}
48impl From<ResponseError> for Error {
49	fn from(value: ResponseError) -> Self {
50		Self::Response(value.0)
51	}
52}
53impl ErrorT for Error {}
54
55#[derive(Serialize, Deserialize, Debug)]
56pub struct TestNotification {
57	pub value: u32,
58}
59notification!((0x0100) TestNotification);
60
61pub struct BifConfig;
62impl bifrostlink::Config for BifConfig {
63	type Address = Address;
64
65	type Error = Error;
66
67	type EncodedData = Vec<u8>;
68
69	fn decode_headers(
70		data_with_headers: bytes::Bytes,
71	) -> Result<(OpaquePacketWrapper<Self::Address>, Self::EncodedData), Self::Error> {
72		let (header, data): (OpaquePacketWrapper<Self::Address>, Vec<u8>) =
73			serde_json::from_slice(&data_with_headers)?;
74		Ok((header, data))
75	}
76
77	fn decode_data<T: DeserializeOwned>(data: Self::EncodedData) -> Result<T, Self::Error> {
78		let v: T = serde_json::from_slice(&data)?;
79		Ok(v)
80	}
81
82	fn encode_data<T: Serialize>(
83		headers: OpaquePacketWrapper<Self::Address>,
84		data: T,
85	) -> bytes::Bytes {
86		let data = serde_json::to_vec(&data).expect("serialization shouldn't fail");
87		let o = serde_json::to_vec(&(headers, data)).expect("serialization shouldn't fail");
88		o.into()
89	}
90}