git.delta.rocks / remowt / refs/commits / f01cb7f97360

difftreelog

source

crates/remowt-link-shared/src/lib.rs2.9 KiBsourcehistory
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;2324pub use remowt_fs::{Error as FsError, Fs, FsClient};25pub use remowt_pty::{Error as PtyError, Pty, PtyClient, ShellId};26pub use remowt_systemd::{Error as SystemdError, Systemd, SystemdClient};2728#[derive(Serialize, Deserialize, Debug, thiserror::Error)]29pub enum ElevateError {30	#[error("elevation failed: {0}")]31	Failed(String),32}3334pub trait Elevator: Send + Sync {35	fn elevate(&self) -> impl Future<Output = Result<(), ElevateError>> + Send;36}3738pub struct ElevateEndpoints<E>(pub E);3940#[endpoints(ns = 3)]41impl<E: Elevator + 'static> ElevateEndpoints<E> {42	#[endpoints(id = 1)]43	async fn elevate(&self) -> Result<(), ElevateError> {44		self.0.elevate().await45	}46}4748#[derive(thiserror::Error, Debug)]49pub enum Error {50	#[error("listener is dead")]51	ListenerDead,52	#[error("response: {0}")]53	Response(String),5455	#[error(transparent)]56	Ui(#[from] remowt_ui_prompt::Error),57}5859impl From<ListenerForYourRequestHasBeenDeadError> for Error {60	fn from(_value: ListenerForYourRequestHasBeenDeadError) -> Self {61		Self::ListenerDead62	}63}64impl From<serde_json::Error> for Error {65	fn from(_value: serde_json::Error) -> Self {66		Self::ListenerDead67	}68}69impl From<Error> for ResponseError {70	fn from(val: Error) -> Self {71		ResponseError(val.to_string())72	}73}74impl From<ResponseError> for Error {75	fn from(value: ResponseError) -> Self {76		Self::Response(value.0)77	}78}79impl ErrorT for Error {}8081#[derive(Serialize, Deserialize, Debug)]82pub struct TestNotification {83	pub value: u32,84}85notification!((0x0100) TestNotification);8687pub struct BifConfig;88impl bifrostlink::Config for BifConfig {89	type Address = Address;9091	type Error = Error;9293	type EncodedData = Vec<u8>;9495	fn decode_headers(96		data_with_headers: bytes::Bytes,97	) -> Result<(OpaquePacketWrapper<Self::Address>, Self::EncodedData), Self::Error> {98		let (header, data): (OpaquePacketWrapper<Self::Address>, Vec<u8>) =99			serde_json::from_slice(&data_with_headers)?;100		Ok((header, data))101	}102103	fn decode_data<T: DeserializeOwned>(data: Self::EncodedData) -> Result<T, Self::Error> {104		let v: T = serde_json::from_slice(&data)?;105		Ok(v)106	}107108	fn encode_data<T: Serialize>(109		headers: OpaquePacketWrapper<Self::Address>,110		data: T,111	) -> bytes::Bytes {112		let data = serde_json::to_vec(&data).expect("serialization shouldn't fail");113		let o = serde_json::to_vec(&(headers, data)).expect("serialization shouldn't fail");114		o.into()115	}116}