1use std::future::Future;23use bifrostlink::declarative::endpoints;4use bifrostlink::error::{ErrorT, ListenerForYourRequestHasBeenDeadError, ResponseError};5use bifrostlink::notification;6use bifrostlink::packet::OpaquePacketWrapper;7use bifrostlink::{AddressT, Config};8use serde::de::DeserializeOwned;9use serde::{Deserialize, Serialize};1011pub mod editor;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(Serialize, Deserialize, Debug, thiserror::Error)]25pub enum ElevateError {26 #[error("elevation failed: {0}")]27 Failed(String),28}2930pub trait Elevator: Send + Sync {31 fn elevate(&self) -> impl Future<Output = Result<(), ElevateError>> + Send;32}3334pub struct ElevateEndpoints<E>(pub E);3536#[endpoints(ns = 3)]37impl<E: Elevator + 'static> ElevateEndpoints<E> {38 #[endpoints(id = 1)]39 async fn elevate(&self) -> Result<(), ElevateError> {40 self.0.elevate().await41 }42}4344#[derive(thiserror::Error, Debug)]45pub enum Error {46 #[error("listener is dead")]47 ListenerDead,48 #[error("response: {0}")]49 Response(String),5051 #[error(transparent)]52 Ui(#[from] remowt_ui_prompt::Error),53}5455impl From<ListenerForYourRequestHasBeenDeadError> for Error {56 fn from(_value: ListenerForYourRequestHasBeenDeadError) -> Self {57 Self::ListenerDead58 }59}60impl From<serde_json::Error> for Error {61 fn from(_value: serde_json::Error) -> Self {62 Self::ListenerDead63 }64}65impl From<Error> for ResponseError {66 fn from(val: Error) -> Self {67 ResponseError(val.to_string())68 }69}70impl From<ResponseError> for Error {71 fn from(value: ResponseError) -> Self {72 Self::Response(value.0)73 }74}75impl ErrorT for Error {}7677#[derive(Serialize, Deserialize, Debug)]78pub struct TestNotification {79 pub value: u32,80}81notification!((0x0100) TestNotification);8283pub struct BifConfig;84impl bifrostlink::Config for BifConfig {85 type Address = Address;8687 type Error = Error;8889 type EncodedData = Vec<u8>;9091 fn decode_headers(92 data_with_headers: bytes::Bytes,93 ) -> Result<(OpaquePacketWrapper<Self::Address>, Self::EncodedData), Self::Error> {94 let (header, data): (OpaquePacketWrapper<Self::Address>, Vec<u8>) =95 serde_json::from_slice(&data_with_headers)?;96 Ok((header, data))97 }9899 fn decode_data<T: DeserializeOwned>(data: Self::EncodedData) -> Result<T, Self::Error> {100 let v: T = serde_json::from_slice(&data)?;101 Ok(v)102 }103104 fn encode_data<T: Serialize>(105 headers: OpaquePacketWrapper<Self::Address>,106 data: T,107 ) -> bytes::Bytes {108 let data = serde_json::to_vec(&data).expect("serialization shouldn't fail");109 let o = serde_json::to_vec(&(headers, data)).expect("serialization shouldn't fail");110 o.into()111 }112}