Skip to main content

remowt_link_shared/
lib.rs

1use std::future::Future;
2
3use 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};
10
11pub mod editor;
12
13#[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 {}
21
22pub mod plugin;
23
24pub 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};
27
28#[derive(Serialize, Deserialize, Debug, thiserror::Error)]
29pub enum ElevateError {
30	#[error("elevation failed: {0}")]
31	Failed(String),
32}
33
34pub trait Elevator: Send + Sync {
35	fn elevate(&self) -> impl Future<Output = Result<(), ElevateError>> + Send;
36}
37
38pub struct ElevateEndpoints<E>(pub E);
39
40#[endpoints(ns = 3)]
41impl<E: Elevator + 'static> ElevateEndpoints<E> {
42	#[endpoints(id = 1)]
43	async fn elevate(&self) -> Result<(), ElevateError> {
44		self.0.elevate().await
45	}
46}
47
48#[derive(thiserror::Error, Debug)]
49pub enum Error {
50	#[error("listener is dead")]
51	ListenerDead,
52	#[error("response: {0}")]
53	Response(String),
54
55	#[error(transparent)]
56	Ui(#[from] remowt_ui_prompt::Error),
57}
58
59impl From<ListenerForYourRequestHasBeenDeadError> for Error {
60	fn from(_value: ListenerForYourRequestHasBeenDeadError) -> Self {
61		Self::ListenerDead
62	}
63}
64impl From<serde_json::Error> for Error {
65	fn from(_value: serde_json::Error) -> Self {
66		Self::ListenerDead
67	}
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 {}
80
81#[derive(Serialize, Deserialize, Debug)]
82pub struct TestNotification {
83	pub value: u32,
84}
85notification!((0x0100) TestNotification);
86
87pub struct BifConfig;
88impl bifrostlink::Config for BifConfig {
89	type Address = Address;
90
91	type Error = Error;
92
93	type EncodedData = Vec<u8>;
94
95	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	}
102
103	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	}
107
108	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}