git.delta.rocks / fleet / refs/commits / 087938500f2e

difftreelog

source

remowt/crates/remowt-link-shared/src/lib.rs2.2 KiBsourcehistory
1use bifrostlink::error::{ErrorT, ListenerForYourRequestHasBeenDeadError, ResponseError};2use bifrostlink::notification;3use bifrostlink::packet::OpaquePacketWrapper;4use bifrostlink::AddressT;5use bytes::Bytes;6use serde::de::DeserializeOwned;7use serde::{Deserialize, Serialize};89pub mod editor;10pub mod iroh_tunnel;11pub mod port;1213#[derive(Clone, Serialize, Hash, Eq, Debug, PartialEq, Deserialize)]14pub enum Address {15	User,16	Agent,17	AgentPrivileged,18	Plugin(u16),19}20impl AddressT for Address {}2122pub mod plugin;2324#[derive(thiserror::Error, Debug)]25pub enum Error {26	#[error("listener is dead")]27	ListenerDead,28	#[error("response: {0}")]29	Response(String),3031	#[error(transparent)]32	Ui(#[from] remowt_ui_prompt::Error),33}3435impl From<ListenerForYourRequestHasBeenDeadError> for Error {36	fn from(_value: ListenerForYourRequestHasBeenDeadError) -> Self {37		Self::ListenerDead38	}39}40impl From<serde_json::Error> for Error {41	fn from(_value: serde_json::Error) -> Self {42		Self::ListenerDead43	}44}45impl From<Error> for ResponseError {46	fn from(val: Error) -> Self {47		ResponseError(val.to_string())48	}49}50impl From<ResponseError> for Error {51	fn from(value: ResponseError) -> Self {52		Self::Response(value.0)53	}54}55impl ErrorT for Error {}5657#[derive(Serialize, Deserialize, Debug)]58pub struct TestNotification {59	pub value: u32,60}61notification!((0x0100) TestNotification);6263pub struct BifConfig;64impl bifrostlink::Config for BifConfig {65	type Address = Address;6667	type Error = Error;6869	type EncodedData = Vec<u8>;7071	fn decode_headers(72		data_with_headers: Bytes,73	) -> Result<(OpaquePacketWrapper<Self::Address>, Self::EncodedData), Self::Error> {74		let (header, data): (OpaquePacketWrapper<Self::Address>, Vec<u8>) =75			serde_json::from_slice(&data_with_headers)?;76		Ok((header, data))77	}7879	fn decode_data<T: DeserializeOwned>(data: Self::EncodedData) -> Result<T, Self::Error> {80		let v: T = serde_json::from_slice(&data)?;81		Ok(v)82	}8384	fn encode_data<T: Serialize>(headers: OpaquePacketWrapper<Self::Address>, data: T) -> Bytes {85		let data = serde_json::to_vec(&data).expect("serialization shouldn't fail");86		let o = serde_json::to_vec(&(headers, data)).expect("serialization shouldn't fail");87		o.into()88	}89}