1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//! `InputMode` — how a recipe receives its input, plus its wire-string form
//! (the input node's `mode` param: `file-upload`, `url`, `text`, `none`).
use std::fmt;
use std::str::FromStr;
/// How the recipe expects to receive its input data.
/// Read from the input node's `mode` parameter.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum InputMode {
/// User uploads files (default). CLI reads file paths from disk.
#[default]
FileUpload,
/// User provides a URL. CLI accepts a URL string.
Url,
/// User provides text content. CLI accepts a text string.
Text,
/// Recipe takes no input at all — all configuration comes from node
/// fields. Used by utility recipes (session managers, system toggles)
/// where there is nothing to feed in.
None,
}
/// Params a run's input preparation injects into the processing node: the
/// URL in `url` mode, the text in `text` mode, each under its mode's name.
/// They carry the run's input, not the recipe's config, so any node takes them.
pub const INJECTED_PARAMS: [&str; 2] = ["url", "text"];
impl InputMode {
/// The wire string used for the input node's `mode` param.
pub fn as_str(self) -> &'static str {
match self {
InputMode::FileUpload => "file-upload",
InputMode::Url => "url",
InputMode::Text => "text",
InputMode::None => "none",
}
}
}
impl fmt::Display for InputMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for InputMode {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"file-upload" => Ok(InputMode::FileUpload),
"url" => Ok(InputMode::Url),
"text" => Ok(InputMode::Text),
"none" => Ok(InputMode::None),
other => Err(format!("unknown input mode: {other}")),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const ALL: [(InputMode, &str); 4] = [
(InputMode::FileUpload, "file-upload"),
(InputMode::Url, "url"),
(InputMode::Text, "text"),
(InputMode::None, "none"),
];
#[test]
fn injected_params_are_named_after_the_url_and_text_modes() {
assert_eq!(
INJECTED_PARAMS,
[InputMode::Url.as_str(), InputMode::Text.as_str()]
);
}
#[test]
fn as_str_returns_wire_string() {
for (mode, wire) in ALL {
assert_eq!(mode.as_str(), wire);
}
}
#[test]
fn display_matches_as_str() {
for (mode, wire) in ALL {
assert_eq!(mode.to_string(), wire);
}
}
#[test]
fn from_str_round_trips() {
for (mode, wire) in ALL {
assert_eq!(wire.parse::<InputMode>(), Ok(mode));
}
}
#[test]
fn from_str_rejects_unknown_mode() {
let err = "carrier-pigeon".parse::<InputMode>().unwrap_err();
assert_eq!(err, "unknown input mode: carrier-pigeon");
}
}