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

difftreelog

source

crates/remowt-link-shared/src/lib.rs2.8 KiBsourcehistory
1use bifrostlink::declarative::endpoints;2use bifrostlink::error::{ErrorT, ListenerForYourRequestHasBeenDeadError, ResponseError};3use bifrostlink::notification;4use bifrostlink::packet::OpaquePacketWrapper;5use bifrostlink::{AddressT, Config};6use serde::de::DeserializeOwned;7use serde::{Deserialize, Serialize};89pub mod editor;1011#[derive(Clone, Serialize, Hash, Eq, Debug, PartialEq, Deserialize)]12pub enum Address {13	User,14	Agent,15	AgentPrivileged,16}17impl AddressT for Address {}1819pub use remowt_fs::{Error as FsError, Fs, FsClient};20pub use remowt_pty::{Error as PtyError, Pty, PtyClient, ShellId};21pub use remowt_systemd::{Error as SystemdError, Systemd, SystemdClient};2223#[derive(Serialize, Deserialize, Debug, thiserror::Error)]24pub enum ElevateError {25	#[error("elevation failed: {0}")]26	Failed(String),27}2829pub trait Elevator: Send + Sync {30	fn elevate(&self) -> impl std::future::Future<Output = Result<(), ElevateError>> + Send;31}3233pub struct ElevateEndpoints<E>(pub E);3435#[endpoints(ns = 3)]36impl<E: Elevator + 'static> ElevateEndpoints<E> {37	#[endpoints(id = 1)]38	async fn elevate(&self) -> Result<(), ElevateError> {39		self.0.elevate().await40	}41}4243#[derive(thiserror::Error, Debug)]44pub enum Error {45	#[error("listener is dead")]46	ListenerDead,47	#[error("response: {0}")]48	Response(String),4950	#[error(transparent)]51	Ui(#[from] ui_prompt::Error),52}5354impl From<ListenerForYourRequestHasBeenDeadError> for Error {55	fn from(_value: ListenerForYourRequestHasBeenDeadError) -> Self {56		Self::ListenerDead57	}58}59impl From<serde_json::Error> for Error {60	fn from(_value: serde_json::Error) -> Self {61		Self::ListenerDead62	}63}64impl From<Error> for ResponseError {65	fn from(val: Error) -> Self {66		ResponseError(val.to_string())67	}68}69impl From<ResponseError> for Error {70	fn from(value: ResponseError) -> Self {71		Self::Response(value.0)72	}73}74impl ErrorT for Error {}7576#[derive(Serialize, Deserialize, Debug)]77pub struct TestNotification {78	pub value: u32,79}80notification!((0x0100) TestNotification);8182pub struct BifConfig;83impl bifrostlink::Config for BifConfig {84	type Address = Address;8586	type Error = Error;8788	type EncodedData = Vec<u8>;8990	fn decode_headers(91		data_with_headers: bytes::Bytes,92	) -> Result<(OpaquePacketWrapper<Self::Address>, Self::EncodedData), Self::Error> {93		let (header, data): (OpaquePacketWrapper<Self::Address>, Vec<u8>) =94			serde_json::from_slice(&data_with_headers)?;95		Ok((header, data))96	}9798	fn decode_data<T: DeserializeOwned>(data: Self::EncodedData) -> Result<T, Self::Error> {99		let v: T = serde_json::from_slice(&data)?;100		Ok(v)101	}102103	fn encode_data<T: Serialize>(104		headers: OpaquePacketWrapper<Self::Address>,105		data: T,106	) -> bytes::Bytes {107		let data = serde_json::to_vec(&data).expect("serialization shouldn't fail");108		let o = serde_json::to_vec(&(headers, data)).expect("serialization shouldn't fail");109		o.into()110	}111}