clap_runner/context/
mod.rs

1use faststr::FastStr;
2
3mod bound;
4mod inner;
5
6pub use bound::*;
7use tokio::io::AsyncWriteExt;
8
9#[derive(Clone, Debug)]
10pub struct Context(pub std::sync::Arc<tokio::sync::Mutex<inner::Context>>);
11
12impl Context {
13    pub fn new_stdout() -> Self {
14        Self(std::sync::Arc::new(tokio::sync::Mutex::new(
15            inner::Context::new_stdout(),
16        )))
17    }
18    pub fn new_with_output<O: OutputBound>(o: std::sync::Arc<tokio::sync::Mutex<O>>) -> Self {
19        Self(std::sync::Arc::new(tokio::sync::Mutex::new(
20            inner::Context::new_with_output(o),
21        )))
22    }
23
24    pub async fn set_abort_reason(&self, reason: FastStr) {
25        self.0.as_ref().lock().await.abort_reason = Some(reason);
26    }
27
28    pub async fn write_all(&self, src: &[u8]) -> tokio::io::Result<()> {
29        self.0.lock().await.output.lock().await.write_all(src).await
30    }
31}