isothrum_machine/
context.rs1use std::sync::Arc;
6use std::time::Duration;
7
8use isothrum_runner::{Captured, ProcessRunner, ProcessSpec};
9
10use crate::exec::{self, Unanswered};
11use crate::host::Host;
12
13pub const PROBE_TIMEOUT: Duration = Duration::from_secs(25);
17
18#[non_exhaustive]
20pub struct Context {
21 pub runner: Arc<dyn ProcessRunner>,
23 pub host: Host,
25 pub probe_timeout: Duration,
27}
28
29impl Context {
30 #[must_use]
32 pub fn new(runner: Arc<dyn ProcessRunner>, host: Host) -> Context {
33 Context {
34 runner,
35 host,
36 probe_timeout: PROBE_TIMEOUT,
37 }
38 }
39
40 pub async fn here(runner: Arc<dyn ProcessRunner>) -> Context {
42 let host = Host::here(runner.as_ref()).await;
43 Context::new(runner, host)
44 }
45
46 #[must_use]
48 pub fn with_probe_timeout(self, limit: Duration) -> Context {
49 Context { probe_timeout: limit, ..self }
50 }
51
52 pub(crate) async fn probe(&self, program: &str, args: &[&str]) -> Result<Captured, Unanswered> {
54 exec::probe(self.runner.as_ref(), self.probe_timeout, program, args).await
55 }
56
57 pub(crate) async fn probe_within(&self, limit: Duration, spec: &ProcessSpec) -> Result<Captured, Unanswered> {
59 exec::probe_spec(self.runner.as_ref(), limit, spec).await
60 }
61
62 pub(crate) async fn read_ok(&self, program: &str, args: &[&str]) -> Result<String, String> {
65 exec::read_ok(self.runner.as_ref(), self.probe_timeout, program, args).await
66 }
67
68 pub(crate) async fn tool_starts(&self, program: &str, args: &[&str]) -> bool {
72 self.probe(program, args).await.as_ref().map_or_else(Unanswered::tool_is_there, |_| true)
73 }
74}
75
76impl std::fmt::Debug for Context {
77 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78 f.debug_struct("Context")
79 .field("host", &self.host)
80 .field("probe_timeout", &self.probe_timeout)
81 .finish_non_exhaustive()
82 }
83}