Skip to main content

asimov_runner/programs/
resolver.rs

1// This is free and unencumbered software released into the public domain.
2
3use crate::{Executor, ExecutorError, Input, Output};
4use async_trait::async_trait;
5use derive_more::Debug;
6use std::{
7    ffi::OsStr,
8    io::{Cursor, Read},
9    process::Stdio,
10};
11use tokio::io::{AsyncBufReadExt, AsyncRead, AsyncWrite};
12
13pub use asimov_patterns::ResolverOptions;
14
15/// See: https://asimov-specs.github.io/program-patterns/#resolver
16pub type ResolverResult = std::result::Result<Vec<String>, ExecutorError>;
17
18/// See: https://asimov-specs.github.io/program-patterns/#resolver
19#[allow(unused)]
20#[derive(Debug)]
21pub struct Resolver {
22    executor: Executor,
23    options: ResolverOptions,
24    input: String,
25    output: Output,
26}
27
28impl Resolver {
29    pub fn new(
30        program: impl AsRef<OsStr>,
31        input: impl AsRef<str>,
32        output: Output,
33        options: ResolverOptions,
34    ) -> Self {
35        let input = input.as_ref().to_string();
36        let mut executor = Executor::new(program);
37        executor
38            .command()
39            .args(if let Some(limit) = options.limit {
40                vec![format!("--limit={}", limit)]
41            } else {
42                vec![]
43            })
44            .args(&options.other)
45            .arg(&input)
46            .stdin(Stdio::null())
47            .stdout(output.as_stdio())
48            .stderr(Stdio::piped());
49
50        Self {
51            executor,
52            options,
53            input,
54            output,
55        }
56    }
57
58    pub async fn execute(&mut self) -> ResolverResult {
59        let _stdout = self.executor.execute().await?;
60        //let lines = stdout.lines().into_iter().collect::<Vec<_>>().await?; // FIXME
61        Ok(Vec::new())
62    }
63}
64
65impl asimov_patterns::Resolver<Vec<String>, ExecutorError> for Resolver {}
66
67#[async_trait]
68impl asimov_patterns::Execute<Vec<String>, ExecutorError> for Resolver {
69    async fn execute(&mut self) -> ResolverResult {
70        self.execute().await
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77    use asimov_patterns::Execute;
78
79    #[tokio::test]
80    async fn test_execute() {
81        // TODO
82    }
83}