Skip to main content

asimov_runner/programs/
indexer.rs

1// This is free and unencumbered software released into the public domain.
2
3use crate::{Executor, ExecutorError, GraphInput, NoOutput};
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::{AsyncRead, AsyncWrite};
12
13pub use asimov_patterns::IndexerOptions;
14
15/// See: https://asimov-specs.github.io/program-patterns/#indexer
16pub type IndexerResult = std::result::Result<(), ExecutorError>;
17
18/// See: https://asimov-specs.github.io/program-patterns/#indexer
19#[allow(unused)]
20#[derive(Debug)]
21pub struct Indexer {
22    executor: Executor,
23    options: IndexerOptions,
24    input: GraphInput,
25    output: NoOutput,
26}
27
28impl Indexer {
29    pub fn new(program: impl AsRef<OsStr>, input: GraphInput, options: IndexerOptions) -> Self {
30        let mut executor = Executor::new(program);
31        executor
32            .command()
33            .args(if let Some(ref input) = options.input {
34                vec![format!("--input={}", input)]
35            } else {
36                vec![]
37            })
38            .args(&options.other)
39            .stdin(input.as_stdio())
40            .stdout(Stdio::null())
41            .stderr(Stdio::piped());
42
43        Self {
44            executor,
45            options,
46            input,
47            output: (),
48        }
49    }
50
51    pub async fn execute(&mut self) -> IndexerResult {
52        let _stdout = self.executor.execute_with_input(&mut self.input).await?;
53        Ok(())
54    }
55}
56
57impl asimov_patterns::Indexer<ExecutorError> for Indexer {}
58
59#[async_trait]
60impl asimov_patterns::Execute<(), ExecutorError> for Indexer {
61    async fn execute(&mut self) -> IndexerResult {
62        self.execute().await
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69    use asimov_patterns::Execute;
70
71    #[tokio::test]
72    async fn test_execute() {
73        // TODO
74    }
75}