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 gateway;11pub mod iroh_tunnel;12pub mod port;1314#[derive(Clone, Serialize, Hash, Eq, Debug, PartialEq, Deserialize)]15pub enum Address {16 User,17 Agent,18 AgentPrivileged,19 Plugin(u16),20 Ephemeral(uuid::Uuid),21}22impl AddressT for Address {}2324pub mod plugin;2526#[derive(thiserror::Error, Debug)]27pub enum Error {28 #[error("listener is dead")]29 ListenerDead,30 #[error("response: {0}")]31 Response(String),32}3334impl From<ListenerForYourRequestHasBeenDeadError> for Error {35 fn from(_value: ListenerForYourRequestHasBeenDeadError) -> Self {36 Self::ListenerDead37 }38}39impl From<serde_json::Error> for Error {40 fn from(e: serde_json::Error) -> Self {41 Self::Response(format!("{e}"))42 }43}44impl From<Error> for ResponseError {45 fn from(val: Error) -> Self {46 ResponseError(val.to_string())47 }48}49impl From<ResponseError> for Error {50 fn from(value: ResponseError) -> Self {51 Self::Response(value.0)52 }53}54impl ErrorT for Error {}5556#[derive(Serialize, Deserialize, Debug)]57pub struct TestNotification {58 pub value: u32,59}60notification!((0x0100) TestNotification);6162pub struct BifConfig;63impl bifrostlink::Config for BifConfig {64 type Address = Address;6566 type Error = Error;6768 type EncodedData = Vec<u8>;6970 fn decode_headers(71 data_with_headers: Bytes,72 ) -> Result<(OpaquePacketWrapper<Self::Address>, Self::EncodedData), Self::Error> {73 let (header, data): (OpaquePacketWrapper<Self::Address>, Vec<u8>) =74 serde_json::from_slice(&data_with_headers)?;75 Ok((header, data))76 }7778 fn decode_data<T: DeserializeOwned>(data: Self::EncodedData) -> Result<T, Self::Error> {79 let v: T = serde_json::from_slice(&data)?;80 Ok(v)81 }8283 fn encode_data<T: Serialize>(headers: OpaquePacketWrapper<Self::Address>, data: T) -> Bytes {84 let data = serde_json::to_vec(&data).expect("serialization shouldn't fail");85 let o = serde_json::to_vec(&(headers, data)).expect("serialization shouldn't fail");86 o.into()87 }88}