Files
continuwuity/src/admin/context.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

43 lines
992 B
Rust
Raw Normal View History

2025-01-04 16:57:07 +00:00
use std::{fmt, time::SystemTime};
2025-01-04 16:57:07 +00:00
use futures::{
Future, FutureExt, TryFutureExt,
2025-01-04 16:57:07 +00:00
io::{AsyncWriteExt, BufWriter},
lock::Mutex,
};
2024-08-28 04:09:46 +00:00
use ruma::EventId;
2025-04-22 01:41:02 +00:00
use tuwunel_core::Result;
use tuwunel_service::Services;
2024-07-27 00:11:41 +00:00
pub(crate) struct Context<'a> {
2024-07-27 00:11:41 +00:00
pub(crate) services: &'a Services,
pub(crate) body: &'a [&'a str],
pub(crate) timer: SystemTime,
2024-08-28 04:09:46 +00:00
pub(crate) reply_id: Option<&'a EventId>,
2025-01-04 16:57:07 +00:00
pub(crate) output: Mutex<BufWriter<Vec<u8>>>,
}
impl Context<'_> {
2025-01-04 16:57:07 +00:00
pub(crate) fn write_fmt(
&self,
arguments: fmt::Arguments<'_>,
) -> impl Future<Output = Result> + Send + '_ + use<'_> {
2025-01-04 16:57:07 +00:00
let buf = format!("{arguments}");
self.write_string(buf)
2025-01-04 16:57:07 +00:00
}
#[inline]
pub(crate) async fn write_string(&self, s: String) -> Result { self.write_str(&s).await }
2025-01-04 16:57:07 +00:00
pub(crate) fn write_str<'a>(
&'a self,
s: &'a str,
) -> impl Future<Output = Result> + Send + 'a {
self.output.lock().then(async move |mut output| {
2025-04-22 04:42:26 +00:00
output
.write_all(s.as_bytes())
.map_err(Into::into)
.await
2025-01-04 16:57:07 +00:00
})
}
2024-07-27 00:11:41 +00:00
}