difftreelog
feat elevation
in: trunk
1 file changed
crates/remowt-link-shared/src/lib.rsdiffbeforeafterboth1use bifrostlink::declarative::endpoints;1use bifrostlink::error::{ErrorT, ListenerForYourRequestHasBeenDeadError, ResponseError};2use bifrostlink::error::{ErrorT, ListenerForYourRequestHasBeenDeadError, ResponseError};3use bifrostlink::notification;4use bifrostlink::packet::OpaquePacketWrapper;2use bifrostlink::AddressT;5use bifrostlink::{AddressT, Config};6use serde::de::DeserializeOwned;3use serde::{Deserialize, Serialize};7use serde::{Deserialize, Serialize};89pub mod editor;4105#[derive(Clone, Serialize, Hash, Eq, Debug, PartialEq, Deserialize)]11#[derive(Clone, Serialize, Hash, Eq, Debug, PartialEq, Deserialize)]6pub enum Address {12pub enum Address {7 User,13 User,8 Agent,14 Agent,15 AgentPrivileged,9}16}10impl AddressT for Address {}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}114212#[derive(thiserror::Error, Debug)]43#[derive(thiserror::Error, Debug)]13pub enum Error {44pub enum Error {16 #[error("response: {0}")]47 #[error("response: {0}")]17 Response(String),48 Response(String),4950 #[error(transparent)]51 Ui(#[from] ui_prompt::Error),18}52}5319impl From<ListenerForYourRequestHasBeenDeadError> for Error {54impl From<ListenerForYourRequestHasBeenDeadError> for Error {38}73}39impl ErrorT for Error {}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}40112