asimov_patterns/programs/
runner.rs

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