Skip to main content

asimov_patterns/programs/
runner.rs

1// This is free and unencumbered software released into the public domain.
2
3use crate::Execute;
4use alloc::{collections::btree_map::BTreeMap, string::String, vec::Vec};
5use bon::Builder;
6
7/// Language runtime engine. Consumes text input conforming to a grammar,
8/// executes it, and produces the execution result as output.
9///
10/// See: https://asimov-specs.github.io/program-patterns/#runner
11pub trait Runner<T, E>: Execute<T, E> {}
12
13/// Configuration options for [`Runner`].
14///
15/// # Examples
16///
17/// ```rust
18/// use asimov_patterns::RunnerOptions;
19///
20/// let options = RunnerOptions::builder()
21///     .define("my_key", "my_value")
22///     .build();
23/// ```
24#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Builder)]
25#[builder(derive(Debug), on(String, into))]
26pub struct RunnerOptions {
27    /// Extended nonstandard runner options.
28    #[builder(field)]
29    pub other: Vec<String>,
30
31    /// Define key/value pairs.
32    #[builder(field)]
33    pub define: BTreeMap<String, String>,
34}
35
36impl<S: runner_options_builder::State> RunnerOptionsBuilder<S> {
37    pub fn other(mut self, flag: impl Into<String>) -> Self {
38        self.other.push(flag.into());
39        self
40    }
41
42    pub fn maybe_other(mut self, flag: Option<impl Into<String>>) -> Self {
43        if let Some(flag) = flag {
44            self.other.push(flag.into());
45        }
46        self
47    }
48
49    pub fn define(mut self, key: impl Into<String>, val: impl Into<String>) -> Self {
50        self.define.insert(key.into(), val.into());
51        self
52    }
53}