git.delta.rocks / remowt / refs/commits / 6d9cf16dada2

difftreelog

source

crates/remowt-link-shared/src/lib.rs2.1 KiBsourcehistory
1use bifrostlink::error::{ErrorT, ListenerForYourRequestHasBeenDeadError, ResponseError};2use bifrostlink::notification;3use bifrostlink::packet::OpaquePacketWrapper;4use bifrostlink::AddressT;5use serde::de::DeserializeOwned;6use serde::{Deserialize, Serialize};78pub mod editor;9pub mod port;1011#[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 {}1920pub mod plugin;2122#[derive(thiserror::Error, Debug)]23pub enum Error {24	#[error("listener is dead")]25	ListenerDead,26	#[error("response: {0}")]27	Response(String),2829	#[error(transparent)]30	Ui(#[from] remowt_ui_prompt::Error),31}3233impl From<ListenerForYourRequestHasBeenDeadError> for Error {34	fn from(_value: ListenerForYourRequestHasBeenDeadError) -> Self {35		Self::ListenerDead36	}37}38impl From<serde_json::Error> for Error {39	fn from(_value: serde_json::Error) -> Self {40		Self::ListenerDead41	}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 {}5455#[derive(Serialize, Deserialize, Debug)]56pub struct TestNotification {57	pub value: u32,58}59notification!((0x0100) TestNotification);6061pub struct BifConfig;62impl bifrostlink::Config for BifConfig {63	type Address = Address;6465	type Error = Error;6667	type EncodedData = Vec<u8>;6869	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	}7677	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	}8182	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}