Skip to main content

tuwunel_admin/
context.rs

1use std::{fmt, time::SystemTime};
2
3use futures::{
4	Future, FutureExt, TryFutureExt,
5	io::{AsyncWriteExt, BufWriter},
6	lock::Mutex,
7};
8use ruma::EventId;
9use tuwunel_core::Result;
10use tuwunel_service::Services;
11
12pub(crate) struct Context<'a> {
13	pub(crate) services: &'a Services,
14	pub(crate) body: &'a [&'a str],
15	pub(crate) timer: SystemTime,
16	pub(crate) reply_id: Option<&'a EventId>,
17	pub(crate) output: Mutex<BufWriter<Vec<u8>>>,
18}
19
20impl Context<'_> {
21	pub(crate) fn write_fmt(
22		&self,
23		arguments: fmt::Arguments<'_>,
24	) -> impl Future<Output = Result> + Send + '_ + use<'_> {
25		let buf = format!("{arguments}");
26		self.write_string(buf)
27	}
28
29	#[inline]
30	pub(crate) async fn write_string(&self, s: String) -> Result { self.write_str(&s).await }
31
32	pub(crate) fn write_str<'a>(
33		&'a self,
34		s: &'a str,
35	) -> impl Future<Output = Result> + Send + 'a {
36		self.output.lock().then(async move |mut output| {
37			output
38				.write_all(s.as_bytes())
39				.map_err(Into::into)
40				.await
41		})
42	}
43}