use std::collections::{BTreeMap, HashMap, HashSet};
use std::sync::Arc;
use anyhow::Result;
use oxdock_func_macro::oxdock_func;
use oxdock_parser::{
Field, KEYWORD_INSPECT, SCRIPT_MODULE_NAME, STD_MODULE_NAME, SemaphoreState, Step, TypeTag,
Value, base_name, qualify, render_structural, split_qualified,
};
use oxdock_pipe::PipeHandle;
use oxdock_process::{CommandStdin, DefaultProcessManager, ProcessManager};
use super::io::StreamHandle;
use super::state::ExecState;
use super::steps::StepCtx;
use super::typing::TypeDescriptor;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FuncKind {
Script,
HostCtx,
HostPure,
}
impl FuncKind {
pub fn label(&self) -> &'static str {
match self {
FuncKind::Script => "script",
FuncKind::HostCtx | FuncKind::HostPure => "host",
}
}
}
#[derive(Debug, Clone)]
pub struct FuncParam {
pub name: String,
pub param_type: Option<TypeTag>,
pub allowed: Option<&'static [&'static str]>,
pub docs: &'static str,
pub options: Option<&'static [ParamOption]>,
pub optional: bool,
}
pub fn required_arity(params: &[FuncParam]) -> usize {
params
.iter()
.rev()
.take_while(|param| param.optional)
.fold(params.len(), |count, _| count - 1)
}
pub fn fill_optional_args(params: &[FuncParam], mut values: Vec<Value>) -> Vec<Value> {
assert!(
values.len() <= params.len(),
"arity gate must run before fill_optional_args"
);
assert!(
values.len() >= required_arity(params),
"arity gate must run before fill_optional_args"
);
while values.len() < params.len() {
values.push(Value::map(BTreeMap::new()));
}
values
}
#[derive(Debug, Clone)]
pub struct ParamOption {
pub name: &'static str,
pub value: TypeTag,
pub required: bool,
pub default: Option<&'static str>,
}
#[derive(Debug, Clone)]
pub struct FuncMeta {
pub name: String,
pub module: String,
pub kind: FuncKind,
pub params: Option<Vec<FuncParam>>,
pub returns: Option<TypeTag>,
pub rpn: bool,
pub summary: &'static str,
pub docs: &'static str,
pub examples: &'static [crate::Example],
}
pub type PureFn = Arc<dyn Fn(Vec<Value>) -> Result<Value> + Send + Sync>;
pub type NativeFn<P> = Arc<dyn Fn(&mut StepCtx<P>, Vec<Value>) -> Result<Value> + Send + Sync>;
pub trait OxDockFn<P: ProcessManager> {
fn registration() -> HostRegistration<P>;
}
pub enum HostRegistration<P: ProcessManager> {
Stateful {
name: String,
meta: FuncMeta,
func: NativeFn<P>,
},
Pure {
name: String,
meta: FuncMeta,
func: PureFn,
},
Both {
name: String,
meta: FuncMeta,
func: NativeFn<P>,
template: PureFn,
},
}
impl<P: ProcessManager> Clone for HostRegistration<P> {
fn clone(&self) -> Self {
match self {
HostRegistration::Stateful { name, meta, func } => HostRegistration::Stateful {
name: name.clone(),
meta: meta.clone(),
func: Arc::clone(func),
},
HostRegistration::Pure { name, meta, func } => HostRegistration::Pure {
name: name.clone(),
meta: meta.clone(),
func: Arc::clone(func),
},
HostRegistration::Both {
name,
meta,
func,
template,
} => HostRegistration::Both {
name: name.clone(),
meta: meta.clone(),
func: Arc::clone(func),
template: Arc::clone(template),
},
}
}
}
impl<P: ProcessManager> HostRegistration<P> {
pub fn meta(&self) -> &FuncMeta {
match self {
HostRegistration::Stateful { meta, .. } => meta,
HostRegistration::Pure { meta, .. } => meta,
HostRegistration::Both { meta, .. } => meta,
}
}
}
#[derive(Debug, Clone)]
pub(super) struct FuncDefData {
pub(super) params: Vec<(String, TypeTag)>,
pub(super) body: Vec<Step>,
}
pub(super) enum FuncBody<P: ProcessManager> {
Script(FuncDefData),
Pure(PureFn),
Ctx(NativeFn<P>),
Both { pure: PureFn, ctx: NativeFn<P> },
}
impl<P: ProcessManager> Clone for FuncBody<P> {
fn clone(&self) -> Self {
match self {
FuncBody::Script(def) => FuncBody::Script(def.clone()),
FuncBody::Pure(func) => FuncBody::Pure(Arc::clone(func)),
FuncBody::Ctx(func) => FuncBody::Ctx(Arc::clone(func)),
FuncBody::Both { pure, ctx } => FuncBody::Both {
pure: Arc::clone(pure),
ctx: Arc::clone(ctx),
},
}
}
}
pub(super) struct FuncEntry<P: ProcessManager> {
pub(super) meta: FuncMeta,
pub(super) body: FuncBody<P>,
}
impl<P: ProcessManager> Clone for FuncEntry<P> {
fn clone(&self) -> Self {
Self {
meta: self.meta.clone(),
body: self.body.clone(),
}
}
}
struct ScopeFrame<P: ProcessManager> {
defined: HashSet<String>,
shadowed: Vec<(String, FuncEntry<P>)>,
}
impl<P: ProcessManager> Clone for ScopeFrame<P> {
fn clone(&self) -> Self {
Self {
defined: self.defined.clone(),
shadowed: self.shadowed.clone(),
}
}
}
pub type PureTable = HashMap<String, HashMap<String, PureFn>>;
pub struct FunctionRegistry<P: ProcessManager> {
entries: HashMap<String, FuncEntry<P>>,
scopes: Vec<ScopeFrame<P>>,
pure_shared: Arc<PureTable>,
}
impl<P: ProcessManager> FunctionRegistry<P> {
pub(super) fn with_builtins() -> Self {
let mut reg = Self {
entries: HashMap::new(),
pure_shared: Arc::new(HashMap::new()),
scopes: vec![ScopeFrame {
defined: HashSet::new(),
shadowed: Vec::new(),
}],
};
for host in Self::builtin_registrations() {
match host {
HostRegistration::Stateful { name, meta, func } => {
reg.insert_qualified(STD_MODULE_NAME, name, meta, FuncBody::Ctx(func));
}
HostRegistration::Pure { name, meta, func } => {
reg.insert_qualified(STD_MODULE_NAME, name, meta, FuncBody::Pure(func));
}
HostRegistration::Both {
name,
meta,
func,
template,
} => {
reg.insert_qualified(
STD_MODULE_NAME,
name,
meta,
FuncBody::Both {
pure: template,
ctx: func,
},
);
}
}
}
reg
}
pub(super) fn builtin_registrations() -> Vec<HostRegistration<P>> {
vec![
Int::registration(),
Float::registration(),
FormatByteCount::registration(),
ParseByteCount::registration(),
FormatDuration::registration(),
ParseDuration::registration(),
Types::registration(),
TypeDescribe::registration(),
Glob::registration(),
LoadToml::registration(),
LoadJson::registration(),
ParseToml::registration(),
ParseJson::registration(),
PathType::registration(),
PathWritable::registration(),
IsExec::registration(),
HasKey::registration(),
MapSet::registration(),
MergeMaps::registration(),
Base64Encode::registration(),
Base64Decode::registration(),
Rand::registration(),
ToJson::registration(),
TypeOf::registration(),
Functions::registration(),
Describe::registration(),
IsTerminal::registration(),
Eof::registration(),
TextTrim::registration(),
TextTrimPrefix::registration(),
TextTrimSuffix::registration(),
TextToUpper::registration(),
TextToLower::registration(),
TextReplace::registration(),
TextReplaceRegex::registration(),
TextSplit::registration(),
TextJoin::registration(),
TextLines::registration(),
TextFields::registration(),
TextMatch::registration(),
TextExtract::registration(),
TextStartsWith::registration(),
TextEndsWith::registration(),
TextContains::registration(),
TextLength::registration(),
TextCount::registration(),
TextCountMatches::registration(),
TextGrep::registration(),
TextLinesRange::registration(),
SemaphoreNew::registration(),
SemaphoreTryAcquire::registration(),
SemaphoreAvailable::registration(),
]
}
pub(super) fn keys(&self) -> HashSet<String> {
self.entries.keys().cloned().collect()
}
pub(super) fn get(&self, name: &str) -> Option<FuncEntry<P>> {
self.entries.get(name).cloned()
}
fn insert_native(&mut self, name: String, meta: FuncMeta, body: FuncBody<P>) {
self.entries.insert(name, FuncEntry { meta, body });
self.rebuild_pure_shared();
}
fn rebuild_pure_shared(&mut self) {
let mut table: PureTable = HashMap::new();
for (name, entry) in &self.entries {
let func = match &entry.body {
FuncBody::Pure(func) => func,
FuncBody::Both { pure, .. } => pure,
FuncBody::Script(_) | FuncBody::Ctx(_) => continue,
};
if let Some((module, base)) = split_qualified(name) {
table
.entry(module.to_string())
.or_default()
.insert(base.to_string(), Arc::clone(func));
}
}
self.pure_shared = Arc::new(table);
}
fn insert_qualified(
&mut self,
module: &str,
base: String,
mut meta: FuncMeta,
body: FuncBody<P>,
) {
meta.name = qualify(module, &base);
meta.module = module.to_string();
if self.entries.contains_key(&meta.name) {
panic!("duplicate function registration `{}`", meta.name);
}
self.insert_native(meta.name.clone(), meta, body);
}
pub(super) fn register_host(
&mut self,
module: &str,
name: String,
mut meta: FuncMeta,
func: NativeFn<P>,
) {
meta.kind = FuncKind::HostCtx;
self.insert_qualified(module, name, meta, FuncBody::Ctx(func));
}
pub(super) fn register_pure_host(
&mut self,
module: &str,
name: String,
mut meta: FuncMeta,
func: PureFn,
) {
meta.kind = FuncKind::HostPure;
self.insert_qualified(module, name, meta, FuncBody::Pure(func));
}
pub(super) fn register_both_host(
&mut self,
module: &str,
name: String,
mut meta: FuncMeta,
func: NativeFn<P>,
template: PureFn,
) {
meta.kind = FuncKind::HostCtx;
self.insert_qualified(
module,
name,
meta,
FuncBody::Both {
pure: template,
ctx: func,
},
);
}
pub(super) fn define_script(
&mut self,
name: &str,
params: &[(String, TypeTag)],
body: &[Step],
) -> Result<()> {
let qualified = qualify(SCRIPT_MODULE_NAME, name);
let shadowable = matches!(
self.entries.get(&qualified).map(|entry| &entry.body),
Some(FuncBody::Script(_))
);
let reserved = self
.entries
.keys()
.any(|key| split_qualified(key).is_some_and(|(_, base)| base == name));
if reserved && !shadowable {
anyhow::bail!("cannot shadow reserved function `{name}`");
}
if self
.scopes
.last()
.is_some_and(|frame| frame.defined.contains(&qualified))
{
anyhow::bail!("duplicate function `{name}` in same scope");
}
let old = self.entries.insert(
qualified.clone(),
FuncEntry {
meta: FuncMeta {
name: qualified.clone(),
module: SCRIPT_MODULE_NAME.to_string(),
kind: FuncKind::Script,
params: Some(
params
.iter()
.map(|(name, param_type)| FuncParam {
name: name.clone(),
param_type: Some(param_type.clone()),
allowed: None,
docs: "",
options: None,
optional: false,
})
.collect(),
),
returns: None,
rpn: false,
summary: "DSL-defined function.",
docs: "Defined via FUNC in script.",
examples: &[],
},
body: FuncBody::Script(FuncDefData {
params: params.to_vec(),
body: body.to_vec(),
}),
},
);
if let Some(frame) = self.scopes.last_mut() {
frame.defined.insert(qualified.clone());
if let Some(old) = old {
frame.shadowed.push((qualified, old));
}
}
Ok(())
}
pub(super) fn push_scope(&mut self) {
self.scopes.push(ScopeFrame {
defined: HashSet::new(),
shadowed: Vec::new(),
});
}
pub(super) fn pop_scope(&mut self) {
let Some(frame) = self.scopes.pop() else {
return;
};
for name in frame.defined {
self.entries.remove(&name);
}
for (name, old) in frame.shadowed {
self.entries.insert(name, old);
}
}
pub(super) fn contains_script(&self, name: &str) -> bool {
matches!(
self.entries.get(name).map(|entry| &entry.body),
Some(FuncBody::Script(_))
)
}
fn clone_pure_fn(&self, name: &str) -> Option<PureFn> {
match self.entries.get(name)?.body {
FuncBody::Pure(ref func) => Some(Arc::clone(func)),
FuncBody::Both { ref pure, .. } => Some(Arc::clone(pure)),
_ => None,
}
}
pub(super) fn pure_shared(&self) -> Arc<PureTable> {
Arc::clone(&self.pure_shared)
}
pub(super) fn all_metas(&self) -> Vec<FuncMeta> {
self.entries_metas()
}
fn clone_ctx_fn(&self, name: &str) -> Option<NativeFn<P>> {
match self.entries.get(name)?.body {
FuncBody::Ctx(ref func) => Some(Arc::clone(func)),
FuncBody::Both { ref ctx, .. } => Some(Arc::clone(ctx)),
_ => None,
}
}
fn meta(&self, name: &str) -> Option<FuncMeta> {
self.entries.get(name).map(|entry| entry.meta.clone())
}
fn native_metas(&self) -> Vec<FuncMeta> {
let mut out: Vec<FuncMeta> = Vec::new();
for entry in self.entries.values() {
if !matches!(entry.body, FuncBody::Script(_)) {
out.push(entry.meta.clone());
}
}
out.sort_by(|a, b| a.name.cmp(&b.name));
out
}
fn entries_metas(&self) -> Vec<FuncMeta> {
let mut out: Vec<FuncMeta> = self
.entries
.values()
.map(|entry| entry.meta.clone())
.collect();
out.sort_by(|a, b| a.name.cmp(&b.name));
out
}
}
impl<P: ProcessManager> Clone for FunctionRegistry<P> {
fn clone(&self) -> Self {
Self {
entries: self.entries.clone(),
scopes: self.scopes.clone(),
pure_shared: Arc::clone(&self.pure_shared),
}
}
}
pub fn builtin_function_names() -> HashSet<String> {
let mut names = FunctionRegistry::<DefaultProcessManager>::with_builtins().keys();
names.insert(KEYWORD_INSPECT.to_string());
names
}
pub fn builtin_function_metas() -> Vec<FuncMeta> {
FunctionRegistry::<DefaultProcessManager>::with_builtins().native_metas()
}
pub fn std_module_table() -> oxdock_parser::ModuleTable {
let functions: HashSet<String> = builtin_function_metas()
.into_iter()
.map(|meta| base_name(&meta.name).to_string())
.collect();
oxdock_parser::ModuleTable {
modules: HashMap::from([(
STD_MODULE_NAME.to_string(),
Some(oxdock_parser::ModuleFuncs { functions }),
)]),
}
}
#[oxdock_func(pure, returns = TypeTag::Int)]
#[example(
name = "convert",
code = r###"
IMPORT [STD]
LET $n: INT = INT("42")
ASSERT_EQ $n 42
LET $flag: INT = INT(true)
ASSERT_EQ $flag 1
"###
)]
fn int(
#[union(Int, Float, String, Bool)]
val: Value,
) -> Result<Value> {
super::args::int_from_value(val)
}
#[oxdock_func(pure, returns = TypeTag::Float)]
#[example(
name = "convert",
code = r###"
IMPORT [STD]
LET $f: FLOAT = FLOAT("1.5")
ASSERT_EQ $f 1.5
LET $whole: FLOAT = FLOAT(2)
ASSERT_EQ $whole 2.0
"###
)]
fn float(
#[union(Float, Int, String)]
val: Value,
) -> Result<Value> {
super::args::float_from_value(val)
}
#[oxdock_func(pure, returns = TypeTag::String)]
#[example(
name = "format and round-trip",
code = r###"
# Byte counts render in the largest exact unit.
IMPORT [STD]
LET $pretty: STRING = FORMAT_BYTE_COUNT(1_048_576)
ASSERT_EQ $pretty "1MiB"
# The pair round-trips through the parser.
LET $back: INT = PARSE_BYTE_COUNT($pretty)
ASSERT_EQ $back 1_048_576
"###
)]
fn format_byte_count(
bytes: i64,
) -> Result<Value> {
if bytes < 0 {
return Err(anyhow::anyhow!(
"FORMAT_BYTE_COUNT needs a non-negative byte count, got {bytes}"
));
}
Ok(Value::string(oxdock_parser::command::format_byte_count(
bytes as u64,
)))
}
#[oxdock_func(pure, returns = TypeTag::Int)]
#[example(
name = "parse",
code = r###"
IMPORT [STD]
LET $n: INT = PARSE_BYTE_COUNT("10KB")
ASSERT_EQ $n 10_000
LET $identity: INT = PARSE_BYTE_COUNT(512)
ASSERT_EQ $identity 512
"###
)]
fn parse_byte_count(
#[union(Int, Float, String)]
text: Value,
) -> Result<Value> {
if let Some(n) = text.as_i64() {
if n < 0 {
return Err(anyhow::anyhow!(
"PARSE_BYTE_COUNT needs a non-negative byte count, got {n}"
));
}
return Ok(Value::int(n));
}
if let Some(f) = text.as_f64() {
if f.is_finite() && f.fract() == 0.0 && f >= 0.0 && f <= i64::MAX as f64 {
return Ok(Value::int(f as i64));
}
return Err(anyhow::anyhow!(
"PARSE_BYTE_COUNT needs an integral byte count, got {f:?}"
));
}
let Some(s) = text.as_str() else {
return Err(anyhow::anyhow!(
"PARSE_BYTE_COUNT needs a token or number, found {text:?}"
));
};
let n = oxdock_parser::command::parse_byte_count(s)?;
let n = i64::try_from(n)
.map_err(|_| anyhow::anyhow!("PARSE_BYTE_COUNT out of INT range: {s:?}"))?;
Ok(Value::int(n))
}
#[oxdock_func(pure, returns = TypeTag::String)]
#[example(
name = "format and round-trip",
code = r###"
# Durations render in the largest exact unit.
IMPORT [STD]
LET $pretty: STRING = FORMAT_DURATION(90_000)
ASSERT_EQ $pretty "90s"
# The pair round-trips through the parser.
LET $back: INT = PARSE_DURATION($pretty)
ASSERT_EQ $back 90_000
"###
)]
fn format_duration(
millis: i64,
) -> Result<Value> {
if millis < 0 {
return Err(anyhow::anyhow!(
"FORMAT_DURATION needs non-negative milliseconds, got {millis}"
));
}
Ok(Value::string(oxdock_parser::command::format_duration(
&std::time::Duration::from_millis(millis as u64),
)))
}
#[oxdock_func(pure, returns = TypeTag::Int)]
#[example(
name = "parse",
code = r###"
# A bare number means seconds, like TIMEOUT.
IMPORT [STD]
LET $ms: INT = PARSE_DURATION("2m")
ASSERT_EQ $ms 120_000
# Milliseconds stay exact.
LET $exact: INT = PARSE_DURATION("1500ms")
ASSERT_EQ $exact 1_500
# One optional space separates number from unit.
LET $spaced: INT = PARSE_DURATION("1 h")
ASSERT_EQ $spaced 3_600_000
"###
)]
fn parse_duration(
text: String,
) -> Result<Value> {
let duration = oxdock_parser::command::parse_duration(&text)?;
let millis = i64::try_from(duration.as_millis())
.map_err(|_| anyhow::anyhow!("PARSE_DURATION out of INT range: {text:?}"))?;
Ok(Value::int(millis))
}
#[oxdock_func(rpn, returns = TypeTag::ListOf(std::sync::Arc::new(TypeTag::String)))]
#[example(
name = "match",
code = r###"
IMPORT [STD]
WRITE "notes-a.md" "a"
WRITE "notes-b.md" "b"
WRITE "other.txt" "c"
LET $found: LIST<STRING> = GLOB("notes-*.md")
ASSERT_EQ $found.0 "notes-a.md"
ASSERT_EQ $found.1 "notes-b.md"
"###
)]
fn glob<P: ProcessManager>(
cx: &mut StepCtx<P>,
#[union(String, Path)]
pattern: Value,
) -> Result<Value> {
let text = super::args::string_or_path_text(&pattern)?;
super::args::glob_from_value(&[Value::string(text)], cx)
}
#[oxdock_func(rpn, returns = TypeTag::MapOf(std::sync::Arc::new(TypeTag::Any)))]
#[example(
name = "load",
code = r###"
IMPORT [STD]
WRITE "cfg.toml" "title = \"demo\""
LET $cfg: MAP<ANY> = LOAD_TOML("cfg.toml")
ASSERT_EQ $cfg.title "demo"
"###
)]
fn load_toml<P: ProcessManager>(
cx: &mut StepCtx<P>,
#[union(String, Path)]
path: Value,
) -> Result<Value> {
let text = super::args::string_or_path_text(&path)?;
super::args::load_toml_from_value(&[Value::string(text)], cx)
}
#[oxdock_func(rpn, returns = TypeTag::Any)]
#[example(
name = "load",
code = r###"
IMPORT [STD]
WRITE "data.json" "{\"a\": 1}"
LET $doc: MAP<ANY> = LOAD_JSON("data.json")
ASSERT_EQ $doc.a 1
"###
)]
fn load_json<P: ProcessManager>(
cx: &mut StepCtx<P>,
#[union(String, Path)]
path: Value,
) -> Result<Value> {
let text = super::args::string_or_path_text(&path)?;
super::args::load_json_from_value(&[Value::string(text)], cx)
}
#[oxdock_func(pure, returns = TypeTag::MapOf(std::sync::Arc::new(TypeTag::Any)))]
#[example(
name = "parse",
code = r###"
IMPORT [STD]
LET $doc: MAP<ANY> = PARSE_TOML("title = \"demo\"")
ASSERT_EQ $doc.title "demo"
"###
)]
fn parse_toml(
text: String,
) -> Result<Value> {
super::args::parse_toml_from_value(Value::string(text))
}
#[oxdock_func(pure, returns = TypeTag::Any)]
#[example(
name = "parse",
code = r###"
# Parse in-memory JSON text with the same conversion as file loading.
IMPORT [STD]
LET $body: STRING = "{\"a\": 1}"
LET $doc: ANY = PARSE_JSON($body)
ASSERT_EQ $doc.a 1
# Top-level arrays parse to LIST, which is why the return is ANY.
LET $items: LIST<ANY> = PARSE_JSON("[1, 2]")
ASSERT_EQ $items.0 1
ASSERT_EQ $items.1 2
"###
)]
fn parse_json(
text: String,
) -> Result<Value> {
super::args::parse_json_from_value(Value::string(text))
}
#[oxdock_func(returns = TypeTag::String)]
#[example(
name = "describe",
code = r###"
IMPORT [STD]
WRITE "entry.txt" "words"
LET $kind: STRING = PATH_TYPE("entry.txt")
ASSERT_EQ $kind "file"
LET $missing: STRING = PATH_TYPE("no-such-entry.txt")
ASSERT_EQ $missing "absent"
"###
)]
fn path_type<P: ProcessManager>(
cx: &mut StepCtx<P>,
#[union(String, Path)]
path: Value,
) -> Result<Value> {
let text = super::args::string_or_path_text(&path)?;
super::args::path_type_from_value(&[Value::string(text)], cx)
}
#[oxdock_func(returns = TypeTag::Bool)]
#[example(
name = "probe",
code = r###"
# An absent nested path is writable when its ancestor is.
IMPORT [STD]
LET $fresh: BOOL = PATH_WRITABLE("probe-check/nested/file.txt")
ASSERT_EQ $fresh true
# The empty string never aliases the working directory.
LET $empty: BOOL = PATH_WRITABLE("")
ASSERT_EQ $empty false
"###
)]
fn path_writable<P: ProcessManager>(
cx: &mut StepCtx<P>,
#[union(String, Path)]
path: Value,
) -> Result<Value> {
let text = super::args::string_or_path_text(&path)?;
super::args::path_writable_from_value(&[Value::string(text)], cx)
}
#[oxdock_func(returns = TypeTag::Bool)]
#[example(
name = "probe",
code = r###"
# Missing tools read false instead of failing the run.
IMPORT [STD]
LET $missing: BOOL = IS_EXEC("no-such-tool-xyz")
ASSERT_EQ $missing false
# Plain files are not executable on any platform.
WRITE tool.txt payload
LET $plain: BOOL = IS_EXEC("tool.txt")
ASSERT_EQ $plain false
"###
)]
fn is_exec<P: ProcessManager>(
cx: &mut StepCtx<P>,
#[union(String, Path)]
name: Value,
) -> Result<Value> {
let text = super::args::string_or_path_text(&name)?;
super::args::is_exec_from_value(&[Value::string(text)], cx)
}
#[oxdock_func(pure, returns = TypeTag::Bool)]
#[example(
name = "probe",
code = r###"
IMPORT [STD]
LET $m: MAP<ANY> = PARSE_JSON("{\"a\": 1}")
LET $hit: BOOL = HAS_KEY($m, "a")
ASSERT_EQ $hit true
LET $miss: BOOL = HAS_KEY($m, "b")
ASSERT_EQ $miss false
"###
)]
fn has_key(
map: BTreeMap<String, Value>,
key: String,
) -> Result<Value> {
super::args::has_key_from_value(Value::map(map), &key)
}
#[oxdock_func(pure, returns = TypeTag::MapOf(std::sync::Arc::new(TypeTag::Any)))]
#[example(
name = "insert",
code = r###"
IMPORT [STD]
LET $m: MAP<ANY> = MAP_SET({seed: 0}, "a", 1)
ASSERT_EQ $m.a 1
"###
)]
fn map_set(
map: BTreeMap<String, Value>,
key: String,
value: Value,
) -> Result<Value> {
super::args::map_set_from_value(Value::map(map), key, value)
}
#[oxdock_func(returns = TypeTag::Float, summary = "Draw a random FLOAT uniformly from [0, 1), optionally seeded.")]
#[example(
name = "seeded draw",
code = r###"
# A seed draws the same value on every run.
IMPORT [STD]
LET $one: FLOAT = RAND({seed: 7})
LET $two: FLOAT = RAND({seed: 7})
ASSERT_EQ $one $two
"###
)]
#[example(
name = "unseeded draw",
code = r###"
# Scale a unit draw into a backoff delay between 1 and 11 seconds.
IMPORT [STD]
LET $draw: FLOAT = RAND()
LET $delay: FLOAT = 1.0 + $draw * 10.0
IF $delay < 1.0 {
EXIT 1
}
IF $delay >= 11.0 {
EXIT 1
}
ECHO "retrying in {{ $delay }}s"
"###
)]
fn rand<P: ProcessManager>(
cx: &mut StepCtx<P>,
#[options("seed?: INT")]
#[optional]
options: BTreeMap<String, Value>,
) -> Result<Value> {
let _ = cx;
let seed = options
.get("seed")
.map(|value| value.as_i64().expect("check_options verified INT"));
super::args::rand_float_from_value(seed)
}
#[oxdock_func(pure, returns = TypeTag::String)]
#[example(
name = "encode",
code = r###"
# Encode text to standard base64.
IMPORT [STD]
LET $encoded: STRING = BASE64_ENCODE("hello")
ASSERT_EQ $encoded "aGVsbG8="
"###
)]
fn base64_encode(
text: String,
) -> Result<Value> {
super::args::base64_encode_from_value(Value::string(text))
}
#[oxdock_func(pure, returns = TypeTag::String)]
#[example(
name = "decode",
code = r###"
# Decode standard base64 back to text.
IMPORT [STD]
LET $decoded: STRING = BASE64_DECODE("aGVsbG8=")
ASSERT_EQ $decoded "hello"
"###
)]
fn base64_decode(
text: String,
) -> Result<Value> {
super::args::base64_decode_from_value(Value::string(text))
}
#[oxdock_func(pure, returns = TypeTag::String)]
#[example(
name = "encode",
code = r###"
IMPORT [STD]
LET $json: STRING = TO_JSON({a: 1})
ASSERT_EQ $json "{\"a\":1}\n"
"###
)]
fn to_json(
#[union(String, Int, Float, Bool, List, Map)]
value: Value,
) -> Result<Value> {
super::args::to_json_from_value(value)
}
#[oxdock_func(pure, returns = TypeTag::String)]
#[example(
name = "name the word",
code = r###"
IMPORT [STD]
LET $word: STRING = TYPE_OF(42)
ASSERT_EQ $word "INT"
"###
)]
fn type_of(
value: Value,
) -> Result<Value> {
super::args::type_of_from_value(value)
}
#[oxdock_func(pure, returns = TypeTag::MapOf(std::sync::Arc::new(TypeTag::Any)))]
#[example(
name = "merge",
code = r###"
IMPORT [STD]
LET $m: MAP<ANY> = MERGE_MAPS([{a: 1}, {b: 2}], "overwrite")
ASSERT_EQ $m.a 1
ASSERT_EQ $m.b 2
"###
)]
fn merge_maps(
maps: Vec<BTreeMap<String, Value>>,
#[values("fail_on_duplicate", "overwrite")]
policy: String,
) -> Result<Value> {
super::args::merge_maps_from_value(
Value::list(maps.into_iter().map(Value::map).collect()),
policy,
)
}
#[oxdock_func(returns = TypeTag::ListOf(std::sync::Arc::new(TypeTag::String)))]
#[example(
name = "list",
code = r###"
IMPORT [STD]
LET $names: LIST<STRING> = FUNCTIONS()
ASSERT_CONTAINS $names "STD::INT"
"###
)]
fn functions<P: ProcessManager>(cx: &mut StepCtx<P>) -> Result<Value> {
let mut names: Vec<String> = cx
.state
.list_functions()
.into_iter()
.map(|meta| meta.name)
.collect();
names.sort();
names.dedup();
Ok(Value::list(names.into_iter().map(Value::string).collect()))
}
#[oxdock_func(
returns = TypeTag::Record(std::sync::Arc::from([Field {
name: "name".to_string(),
ty: TypeTag::String,
docs: "Qualified function name.",
optional: false,
}, Field {
name: "module".to_string(),
ty: TypeTag::String,
docs: "Owning module.",
optional: false,
}, Field {
name: "kind".to_string(),
ty: TypeTag::String,
docs: "Origin label (`host` or `script`).",
optional: false,
}, Field {
name: "params".to_string(),
ty: TypeTag::ListOf(std::sync::Arc::new(TypeTag::MapOf(std::sync::Arc::new(
TypeTag::Any,
)))),
docs: "Parameter entries (empty for parameterless functions).",
optional: false,
}, Field {
name: "returns".to_string(),
ty: TypeTag::String,
docs: "Structural return shape (empty when untagged).",
optional: false,
}, Field {
name: "rpn".to_string(),
ty: TypeTag::Bool,
docs: "True when callable in RPN position.",
optional: false,
}, Field {
name: "summary".to_string(),
ty: TypeTag::String,
docs: "One-line description.",
optional: false,
}]))
summary = "Describe one function by qualified name."
)]
#[example(
name = "describe",
code = r###"
IMPORT [STD]
LET $info: MAP<ANY> = DESCRIBE("STD::INT")
ASSERT_EQ $info.name "STD::INT"
ASSERT_EQ $info.returns "INT"
"###
)]
fn describe<P: ProcessManager>(
cx: &mut StepCtx<P>,
name: String,
) -> Result<Value> {
if split_qualified(&name).is_none() && name != KEYWORD_INSPECT {
anyhow::bail!(
"unknown function `{name}`: DESCRIBE requires a qualified name (e.g. `STD::{name}`)"
);
}
cx.state
.describe_function(&name)
.ok_or_else(|| anyhow::anyhow!("unknown function {name}"))
}
#[oxdock_func(returns = TypeTag::ListOf(std::sync::Arc::new(TypeTag::String)))]
#[example(
name = "list",
code = r###"
IMPORT [STD]
LET $names: LIST<STRING> = TYPES()
ASSERT_CONTAINS $names "STRING"
"###
)]
fn types<P: ProcessManager>(cx: &mut StepCtx<P>) -> Result<Value> {
Ok(Value::list(
cx.state
.type_names()
.into_iter()
.map(Value::string)
.collect(),
))
}
#[oxdock_func(returns = TypeTag::Record(std::sync::Arc::from([Field {
name: "name".to_string(),
ty: TypeTag::String,
docs: "Queried type name.",
optional: false,
}, Field {
name: "summary".to_string(),
ty: TypeTag::String,
docs: "One-line description.",
optional: false,
}, Field {
name: "docs".to_string(),
ty: TypeTag::String,
docs: "Full documentation text.",
optional: false,
}])))]
#[example(
name = "describe",
code = r###"
IMPORT [STD]
LET $info: MAP<ANY> = TYPE_DESCRIBE("STRING")
ASSERT_EQ $info.name "STRING"
"###
)]
fn type_describe<P: ProcessManager>(
cx: &mut StepCtx<P>,
name: String,
) -> Result<Value> {
if let Some(descriptor) = cx.state.describe_type(&name) {
let mut map = BTreeMap::new();
map.insert(
"name".to_string(),
Value::string(descriptor.name.to_string()),
);
map.insert(
"summary".to_string(),
Value::string(descriptor.summary.to_string()),
);
map.insert(
"docs".to_string(),
Value::string(descriptor.docs.to_string()),
);
return Ok(Value::map(map));
}
if let Some(target) = cx.state.alias_target(&name) {
let mut map = BTreeMap::new();
map.insert("name".to_string(), Value::string(name.clone()));
map.insert(
"summary".to_string(),
Value::string(format!("Script type alias for {target}.")),
);
map.insert(
"docs".to_string(),
Value::string(format!(
"Declared with TYPE {name} = {target}. The alias expands structurally at every declaration."
)),
);
return Ok(Value::map(map));
}
Err(anyhow::anyhow!("unknown type {name}"))
}
#[oxdock_func(returns = TypeTag::Bool)]
#[example(
name = "null stdin",
code = r###"
# A null stdin is never a terminal, in every harness.
IMPORT [STD]
LET $term: BOOL = IS_TERMINAL("stdin")
ASSERT_EQ $term false
"###
)]
fn is_terminal<P: ProcessManager>(
cx: &mut StepCtx<P>,
#[values("stdin", "stdout", "stderr")]
stream_name: String,
) -> Result<Value> {
use std::io::IsTerminal;
let terminal = match stream_name.as_str() {
"stdin" => {
if cx.stdin_pipe.is_some() {
false
} else {
match &cx.stdin {
CommandStdin::Null => false,
#[cfg(not(miri))]
CommandStdin::OsPipe(_) => false,
CommandStdin::Stream(_) => false,
CommandStdin::Inherit => std::io::stdin().is_terminal(),
}
}
}
"stdout" => {
if cx.out_pipe.is_some() {
false
} else if cx.state.io.stdout().is_some() {
false
} else {
match &cx.out {
None => std::io::stdout().is_terminal(),
#[cfg(not(miri))]
Some(StreamHandle::Os(_)) => false,
Some(StreamHandle::Stream(_)) => std::io::stdout().is_terminal(),
}
}
}
"stderr" => {
if cx.state.io.stderr().is_some() {
false
} else {
match &cx.err {
None => std::io::stderr().is_terminal(),
#[cfg(not(miri))]
Some(StreamHandle::Os(_)) => false,
Some(StreamHandle::Stream(_)) => false,
}
}
}
_ => anyhow::bail!(
"IS_TERMINAL expects \"stdin\", \"stdout\", or \"stderr\", got {stream_name:?}"
),
};
Ok(Value::bool(terminal))
}
#[oxdock_func(returns = TypeTag::Semaphore)]
#[example(
name = "mint",
code = r###"
# Mint a semaphore admitting at most two holders.
IMPORT [STD]
LET $sem: SEMAPHORE = SEMAPHORE_NEW(2)
# A fresh semaphore reports its full cap as available.
LET $free: INT = SEMAPHORE_AVAILABLE($sem)
ASSERT_EQ $free 2
"###
)]
fn semaphore_new<P: ProcessManager>(
cx: &mut StepCtx<P>,
max: i64,
) -> Result<Value> {
let _ = cx;
if max <= 0 {
return Err(anyhow::anyhow!(
"SEMAPHORE_NEW() requires a positive max, got {max}"
));
}
Ok(Value::semaphore(max as usize))
}
#[oxdock_func(returns = TypeTag::Record(std::sync::Arc::from([Field {
name: "held".to_string(),
ty: TypeTag::Bool,
docs: "`true` with the permit under `permit`, `false` with no `permit` key.",
optional: false,
}, Field {
name: "permit".to_string(),
ty: TypeTag::Permit,
docs: "Permit handle; read only when `held`.",
optional: true,
}])))]
#[example(
name = "acquire",
code = r###"
# First acquire holds with a permit to bind.
IMPORT [STD]
LET $sem: SEMAPHORE = SEMAPHORE_NEW(1)
LET $first: MAP<held: BOOL, permit?: PERMIT> = SEMAPHORE_TRY_ACQUIRE($sem)
ASSERT_EQ $first.held true
LET $permit: PERMIT = $first.permit
# A capped semaphore answers held false with no permit key.
LET $second: MAP<held: BOOL, permit?: PERMIT> = SEMAPHORE_TRY_ACQUIRE($sem)
ASSERT_EQ $second.held false
LET $has_permit: BOOL = HAS_KEY($second, "permit")
ASSERT_EQ $has_permit false
"###
)]
fn semaphore_try_acquire<P: ProcessManager>(
cx: &mut StepCtx<P>,
sem: Arc<SemaphoreState>,
) -> Result<Value> {
let _ = cx;
let mut map = BTreeMap::new();
if sem.try_acquire() {
map.insert("held".to_string(), Value::bool(true));
map.insert("permit".to_string(), Value::permit(&sem));
} else {
map.insert("held".to_string(), Value::bool(false));
}
Ok(Value::map(map))
}
#[oxdock_func(pure, returns = TypeTag::Int)]
#[example(
name = "readout",
code = r###"
# Hold one permit on a cap of two.
IMPORT [STD]
LET $sem: SEMAPHORE = SEMAPHORE_NEW(2)
LET $acq: MAP<held: BOOL, permit?: PERMIT> = SEMAPHORE_TRY_ACQUIRE($sem)
ASSERT_EQ $acq.held true
# One held permit leaves exactly one free.
LET $free: INT = SEMAPHORE_AVAILABLE($sem)
ASSERT_EQ $free 1
"###
)]
fn semaphore_available(
sem: Arc<SemaphoreState>,
) -> Result<Value> {
Ok(Value::int(sem.available() as i64))
}
#[oxdock_func(
rpn,
returns = TypeTag::union(vec![TypeTag::String, TypeTag::Pipe]),
template = text_trim_string
)]
#[example(
name = "trim",
code = r###"
# Leading and trailing whitespace falls away.
IMPORT [STD]
LET $clean: STRING = TEXT_TRIM(" hello ")
ASSERT_EQ $clean "hello"
"###
)]
#[example(
name = "trim pipe mirrors",
code = r###"
# Pipe input mints pipe output with the same transform.
IMPORT [STD]
LET $cap: PIPE
WITH_IO [stdout=$cap] {
ECHO " hello "
}
LET $out: PIPE = TEXT_TRIM($cap)
WITH_IO [stdin=$out] READ_LINE $line
ASSERT_EQ $line "hello"
"###
)]
#[example(
name = "trim template",
code = r###"
# String inputs run inside placeholders too.
IMPORT [STD]
LET $msg: STRING = 'clean:{{ STD::TEXT_TRIM(" hello ") }}'
ASSERT_EQ $msg "clean:hello"
"###
)]
#[example(
name = "trim file template",
code = r###"
# EXPAND templates resolve the same registry.
IMPORT [STD]
WRITE "tpl.txt" "clean:\{{ STD::TEXT_TRIM(\" hello \") }}"
LET $out: STRING = EXPAND "tpl.txt"
ASSERT_EQ $out "clean:hello"
"###
)]
fn text_trim<P: ProcessManager>(
cx: &mut StepCtx<P>,
#[union(String, Pipe)]
text: Value,
) -> Result<Value> {
super::args::mirror_stream(cx, "TEXT_TRIM", &text, super::args::TextPump::Trim)
}
fn text_trim_string(text: Value) -> Result<Value> {
super::args::mirror_string("TEXT_TRIM", &text, super::args::TextPump::Trim)
}
#[oxdock_func(
rpn,
returns = TypeTag::union(vec![TypeTag::String, TypeTag::Pipe]),
template = text_trim_prefix_string
)]
#[example(
name = "strip prefix",
code = r###"
# A present prefix falls away; absence returns the input unchanged.
IMPORT [STD]
LET $bare: STRING = TEXT_TRIM_PREFIX("v1.2.3", "v")
ASSERT_EQ $bare "1.2.3"
# Missing prefixes pass through untouched.
LET $same: STRING = TEXT_TRIM_PREFIX("1.2.3", "v")
ASSERT_EQ $same "1.2.3"
"###
)]
#[example(
name = "strip prefix pipe mirrors",
code = r###"
# Pipe input mints pipe output with the same strip.
IMPORT [STD]
LET $cap: PIPE
WITH_IO [stdout=$cap] {
ECHO "v1.2.3"
}
LET $trimmed: PIPE = TEXT_TRIM($cap)
LET $out: PIPE = TEXT_TRIM_PREFIX($trimmed, "v")
WITH_IO [stdin=$out] READ_LINE $line
ASSERT_EQ $line "1.2.3"
"###
)]
#[example(
name = "strip prefix template",
code = r###"
# String inputs run inside placeholders too.
IMPORT [STD]
LET $msg: STRING = 'bare:{{ STD::TEXT_TRIM_PREFIX("v1.2.3", "v") }}'
ASSERT_EQ $msg "bare:1.2.3"
"###
)]
fn text_trim_prefix<P: ProcessManager>(
cx: &mut StepCtx<P>,
#[union(String, Pipe)]
text: Value,
prefix: String,
) -> Result<Value> {
super::args::mirror_stream(
cx,
"TEXT_TRIM_PREFIX",
&text,
super::args::TextPump::TrimPrefix(prefix),
)
}
fn text_trim_prefix_string(text: Value, prefix: String) -> Result<Value> {
super::args::mirror_string(
"TEXT_TRIM_PREFIX",
&text,
super::args::TextPump::TrimPrefix(prefix),
)
}
#[oxdock_func(
rpn,
returns = TypeTag::union(vec![TypeTag::String, TypeTag::Pipe]),
template = text_trim_suffix_string
)]
#[example(
name = "strip suffix",
code = r###"
# A present suffix falls away; absence returns the input unchanged.
IMPORT [STD]
LET $bare: STRING = TEXT_TRIM_SUFFIX("archive.tar.gz", ".gz")
ASSERT_EQ $bare "archive.tar"
# Missing suffixes pass through untouched.
LET $same: STRING = TEXT_TRIM_SUFFIX("archive.tar", ".gz")
ASSERT_EQ $same "archive.tar"
"###
)]
#[example(
name = "strip suffix pipe mirrors",
code = r###"
# Pipe input mints pipe output with the same strip.
IMPORT [STD]
LET $cap: PIPE
WITH_IO [stdout=$cap] {
ECHO "archive.tar.gz"
}
LET $trimmed: PIPE = TEXT_TRIM($cap)
LET $out: PIPE = TEXT_TRIM_SUFFIX($trimmed, ".gz")
WITH_IO [stdin=$out] READ_LINE $line
ASSERT_EQ $line "archive.tar"
"###
)]
#[example(
name = "strip suffix template",
code = r###"
# String inputs run inside placeholders too.
IMPORT [STD]
LET $msg: STRING = 'bare:{{ STD::TEXT_TRIM_SUFFIX("archive.tar.gz", ".gz") }}'
ASSERT_EQ $msg "bare:archive.tar"
"###
)]
fn text_trim_suffix<P: ProcessManager>(
cx: &mut StepCtx<P>,
#[union(String, Pipe)]
text: Value,
suffix: String,
) -> Result<Value> {
super::args::mirror_stream(
cx,
"TEXT_TRIM_SUFFIX",
&text,
super::args::TextPump::TrimSuffix(suffix),
)
}
fn text_trim_suffix_string(text: Value, suffix: String) -> Result<Value> {
super::args::mirror_string(
"TEXT_TRIM_SUFFIX",
&text,
super::args::TextPump::TrimSuffix(suffix),
)
}
#[oxdock_func(
rpn,
returns = TypeTag::union(vec![TypeTag::String, TypeTag::Pipe]),
template = text_to_upper_string
)]
#[example(
name = "uppercase",
code = r###"
# Text folds to uppercase.
IMPORT [STD]
LET $loud: STRING = TEXT_TO_UPPER("hello")
ASSERT_EQ $loud "HELLO"
"###
)]
#[example(
name = "uppercase pipe mirrors",
code = r###"
# Pipe input mints pipe output with the same fold.
IMPORT [STD]
LET $cap: PIPE
WITH_IO [stdout=$cap] {
ECHO "hello"
}
LET $out: PIPE = TEXT_TO_UPPER($cap)
WITH_IO [stdin=$out] READ_LINE $line
ASSERT_EQ $line "HELLO"
"###
)]
#[example(
name = "uppercase template",
code = r###"
# String inputs run inside placeholders too.
IMPORT [STD]
LET $msg: STRING = 'loud:{{ STD::TEXT_TO_UPPER("hello") }}'
ASSERT_EQ $msg "loud:HELLO"
"###
)]
fn text_to_upper<P: ProcessManager>(
cx: &mut StepCtx<P>,
#[union(String, Pipe)]
text: Value,
) -> Result<Value> {
super::args::mirror_stream(cx, "TEXT_TO_UPPER", &text, super::args::TextPump::ToUpper)
}
fn text_to_upper_string(text: Value) -> Result<Value> {
super::args::mirror_string("TEXT_TO_UPPER", &text, super::args::TextPump::ToUpper)
}
#[oxdock_func(
rpn,
returns = TypeTag::union(vec![TypeTag::String, TypeTag::Pipe]),
template = text_to_lower_string
)]
#[example(
name = "lowercase",
code = r###"
# Text folds to lowercase.
IMPORT [STD]
LET $quiet: STRING = TEXT_TO_LOWER("HELLO")
ASSERT_EQ $quiet "hello"
"###
)]
#[example(
name = "lowercase pipe mirrors",
code = r###"
# Pipe input mints pipe output with the same fold.
IMPORT [STD]
LET $cap: PIPE
WITH_IO [stdout=$cap] {
ECHO "HELLO"
}
LET $out: PIPE = TEXT_TO_LOWER($cap)
WITH_IO [stdin=$out] READ_LINE $line
ASSERT_EQ $line "hello"
"###
)]
#[example(
name = "lowercase template",
code = r###"
# String inputs run inside placeholders too.
IMPORT [STD]
LET $msg: STRING = 'quiet:{{ STD::TEXT_TO_LOWER("HELLO") }}'
ASSERT_EQ $msg "quiet:hello"
"###
)]
fn text_to_lower<P: ProcessManager>(
cx: &mut StepCtx<P>,
#[union(String, Pipe)]
text: Value,
) -> Result<Value> {
super::args::mirror_stream(cx, "TEXT_TO_LOWER", &text, super::args::TextPump::ToLower)
}
fn text_to_lower_string(text: Value) -> Result<Value> {
super::args::mirror_string("TEXT_TO_LOWER", &text, super::args::TextPump::ToLower)
}
#[oxdock_func(
rpn,
returns = TypeTag::union(vec![TypeTag::String, TypeTag::Pipe]),
template = text_replace_string
)]
#[example(
name = "replace",
code = r###"
# Every literal occurrence rewrites in place.
IMPORT [STD]
LET $fixed: STRING = TEXT_REPLACE("hello world", "world", "there")
ASSERT_EQ $fixed "hello there"
"###
)]
#[example(
name = "replace pipe mirrors",
code = r###"
# Pipe input mints pipe output with the same rewrite.
IMPORT [STD]
LET $cap: PIPE
WITH_IO [stdout=$cap] {
ECHO "hello world"
}
LET $trimmed: PIPE = TEXT_TRIM($cap)
LET $out: PIPE = TEXT_REPLACE($trimmed, "world", "there")
WITH_IO [stdin=$out] READ_LINE $line
ASSERT_EQ $line "hello there"
"###
)]
#[example(
name = "replace template",
code = r###"
# String inputs run inside placeholders too.
IMPORT [STD]
LET $msg: STRING = 'fixed:{{ STD::TEXT_REPLACE("hello world", "world", "there") }}'
ASSERT_EQ $msg "fixed:hello there"
"###
)]
fn text_replace<P: ProcessManager>(
cx: &mut StepCtx<P>,
#[union(String, Pipe)]
text: Value,
from: String,
to: String,
) -> Result<Value> {
super::args::mirror_stream(
cx,
"TEXT_REPLACE",
&text,
super::args::TextPump::ReplaceLiteral { from, to },
)
}
fn text_replace_string(text: Value, from: String, to: String) -> Result<Value> {
super::args::mirror_string(
"TEXT_REPLACE",
&text,
super::args::TextPump::ReplaceLiteral { from, to },
)
}
#[oxdock_func(
rpn,
returns = TypeTag::union(vec![TypeTag::String, TypeTag::Pipe]),
template = text_replace_regex_string
)]
#[example(
name = "replace with captures",
code = r###"
# Capture groups expand in the replacement.
IMPORT [STD]
LET $swapped: STRING = TEXT_REPLACE_REGEX("2026-10-13", "(\\d+)-(\\d+)-(\\d+)", "$3/$2/$1")
ASSERT_EQ $swapped "13/10/2026"
"###
)]
#[example(
name = "replace regex pipe mirrors",
code = r###"
# Pipe input mints pipe output with the same rewrite.
IMPORT [STD]
LET $cap: PIPE
WITH_IO [stdout=$cap] {
ECHO "2026-10-13"
}
LET $trimmed: PIPE = TEXT_TRIM($cap)
LET $out: PIPE = TEXT_REPLACE_REGEX($trimmed, "(\\d+)-(\\d+)-(\\d+)", "$3/$2/$1")
WITH_IO [stdin=$out] READ_LINE $line
ASSERT_EQ $line "13/10/2026"
"###
)]
#[example(
name = "replace regex template",
code = r###"
# String inputs run inside placeholders too.
IMPORT [STD]
LET $msg: STRING = 'swapped:{{ STD::TEXT_REPLACE_REGEX("2026-10-13", "(\\d+)-(\\d+)-(\\d+)", "$3/$2/$1") }}'
ASSERT_EQ $msg "swapped:13/10/2026"
"###
)]
fn text_replace_regex<P: ProcessManager>(
cx: &mut StepCtx<P>,
#[union(String, Pipe)]
text: Value,
pattern: String,
replacement: String,
) -> Result<Value> {
if let Some(s) = text.as_str() {
let out =
super::args::text_replace_regex_str(s, &pattern, &replacement, "TEXT_REPLACE_REGEX")?;
return Ok(Value::string(out));
}
let content = super::args::read_pipe_eager(cx, "TEXT_REPLACE_REGEX", &text)?;
let out = super::args::text_replace_regex_str(
&content,
&pattern,
&replacement,
"TEXT_REPLACE_REGEX",
)?;
super::args::write_completed_pipe(cx, "TEXT_REPLACE_REGEX", out)
}
fn text_replace_regex_string(text: Value, pattern: String, replacement: String) -> Result<Value> {
let Some(s) = text.as_str() else {
return Err(super::args::pipe_needs_step_io("TEXT_REPLACE_REGEX"));
};
let out = super::args::text_replace_regex_str(s, &pattern, &replacement, "TEXT_REPLACE_REGEX")?;
Ok(Value::string(out))
}
#[oxdock_func(pure, returns = TypeTag::ListOf(std::sync::Arc::new(TypeTag::String)))]
#[example(
name = "split",
code = r###"
# Delimiters split into list items.
IMPORT [STD]
LET $parts: LIST<STRING> = TEXT_SPLIT("a,b,c", ",")
ASSERT_EQ $parts.0 "a"
ASSERT_EQ $parts.1 "b"
ASSERT_EQ $parts.2 "c"
"###
)]
fn text_split(
text: String,
delimiter: String,
) -> Result<Value> {
Ok(super::args::text_list_value(super::args::text_split_str(
&text, &delimiter,
)))
}
#[oxdock_func(pure, returns = TypeTag::String)]
#[example(
name = "join",
code = r###"
# List items join with the separator between them.
IMPORT [STD]
LET $joined: STRING = TEXT_JOIN(["a", "b", "c"], ",")
ASSERT_EQ $joined "a,b,c"
"###
)]
fn text_join(
elements: Vec<String>,
delimiter: String,
) -> Result<Value> {
Ok(Value::string(elements.join(delimiter.as_str())))
}
#[oxdock_func(pure, returns = TypeTag::ListOf(std::sync::Arc::new(TypeTag::String)))]
#[example(
name = "lines",
code = r###"
# Newlines split into line items.
IMPORT [STD]
LET $lines: LIST<STRING> = TEXT_LINES("one\ntwo\nthree")
ASSERT_EQ $lines.0 "one"
ASSERT_EQ $lines.1 "two"
ASSERT_EQ $lines.2 "three"
# Windows endings split the same way after normalization.
LET $crlf: LIST<STRING> = TEXT_LINES("one\r\ntwo\r\nthree")
ASSERT_EQ $crlf.0 "one"
ASSERT_EQ $crlf.1 "two"
ASSERT_EQ $crlf.2 "three"
# Old Mac endings split too, and mixed endings split together because the input normalizes to LF before splitting.
LET $mixed: LIST<STRING> = TEXT_LINES("a\r\nb\nc\rd")
ASSERT_EQ $mixed.0 "a"
ASSERT_EQ $mixed.1 "b"
ASSERT_EQ $mixed.2 "c"
ASSERT_EQ $mixed.3 "d"
"###
)]
fn text_lines(
text: String,
) -> Result<Value> {
Ok(super::args::text_list_value(super::args::text_lines_str(
&text,
)))
}
#[oxdock_func(pure, returns = TypeTag::ListOf(std::sync::Arc::new(TypeTag::String)))]
#[example(
name = "fields",
code = r###"
# Runs of whitespace separate fields; empties never appear.
IMPORT [STD]
LET $fields: LIST<STRING> = TEXT_FIELDS(" abc def ")
ASSERT_EQ $fields.0 "abc"
ASSERT_EQ $fields.1 "def"
"###
)]
fn text_fields(
text: String,
) -> Result<Value> {
Ok(super::args::text_list_value(super::args::text_fields_str(
&text,
)))
}
#[oxdock_func(pure, returns = TypeTag::ListOf(std::sync::Arc::new(TypeTag::String)))]
#[example(
name = "line range",
code = r###"
# Lines 2 through 3 come back as a list, 1-based inclusive.
IMPORT [STD]
LET $window: LIST<STRING> = TEXT_LINES_RANGE("one\ntwo\nthree\nfour", 2, 3)
ASSERT_EQ $window.0 "two"
ASSERT_EQ $window.1 "three"
# Ranges past the end clamp instead of failing.
LET $tail: LIST<STRING> = TEXT_LINES_RANGE("one\ntwo", 2, 9)
ASSERT_EQ $tail.0 "two"
"###
)]
fn text_lines_range(
text: String,
from: i64,
to: i64,
) -> Result<Value> {
Ok(super::args::text_list_value(
super::args::text_lines_range_str(&text, from, to, "TEXT_LINES_RANGE")?,
))
}
#[oxdock_func(pure, returns = TypeTag::Bool)]
#[example(
name = "match",
code = r###"
# Regex matches report true; misses report false.
IMPORT [STD]
LET $hit: BOOL = TEXT_MATCH("abc123", "\\d+")
ASSERT_EQ $hit true
# A miss is false, not a failure.
LET $miss: BOOL = TEXT_MATCH("abc", "^\\d+$")
ASSERT_EQ $miss false
"###
)]
fn text_match(
text: String,
pattern: String,
) -> Result<Value> {
let re = super::args::text_compile_pattern("TEXT_MATCH", &pattern)?;
Ok(Value::bool(re.is_match(&text)))
}
#[oxdock_func(pure, returns = TypeTag::ListOf(std::sync::Arc::new(TypeTag::String)))]
#[example(
name = "extract",
code = r###"
# Index zero is the full match; groups follow in order.
IMPORT [STD]
LET $caps: LIST<STRING> = TEXT_EXTRACT("abc123", "([a-z]+)(\\d+)")
ASSERT_EQ $caps.0 "abc123"
ASSERT_EQ $caps.1 "abc"
ASSERT_EQ $caps.2 "123"
"###
)]
fn text_extract(
text: String,
pattern: String,
) -> Result<Value> {
let groups = super::args::text_extract_str(&text, &pattern, "TEXT_EXTRACT")?;
Ok(super::args::text_list_value(groups))
}
#[oxdock_func(pure, returns = TypeTag::Bool)]
#[example(
name = "starts with",
code = r###"
# Present prefixes report true; misses report false.
IMPORT [STD]
LET $hit: BOOL = TEXT_STARTS_WITH("linux-musl-x64", "linux")
ASSERT_EQ $hit true
# A miss is false, not a failure.
LET $miss: BOOL = TEXT_STARTS_WITH("macos-arm", "linux")
ASSERT_EQ $miss false
"###
)]
fn text_starts_with(
text: String,
prefix: String,
) -> Result<Value> {
Ok(Value::bool(text.starts_with(prefix.as_str())))
}
#[oxdock_func(pure, returns = TypeTag::Bool)]
#[example(
name = "ends with",
code = r###"
# Present suffixes report true; misses report false.
IMPORT [STD]
LET $hit: BOOL = TEXT_ENDS_WITH("x86_64-linux-musl", "linux-musl")
ASSERT_EQ $hit true
# A miss is false, not a failure.
LET $miss: BOOL = TEXT_ENDS_WITH("x86_64-linux-gnu", "linux-musl")
ASSERT_EQ $miss false
"###
)]
fn text_ends_with(
text: String,
suffix: String,
) -> Result<Value> {
Ok(Value::bool(text.ends_with(suffix.as_str())))
}
#[oxdock_func(pure, returns = TypeTag::Bool)]
#[example(
name = "contains",
code = r###"
# Present needles report true; misses report false.
IMPORT [STD]
LET $hit: BOOL = TEXT_CONTAINS("x86_64-linux-musl", "musl")
ASSERT_EQ $hit true
# A miss is false, not a failure.
LET $miss: BOOL = TEXT_CONTAINS("x86_64-linux-gnu", "musl")
ASSERT_EQ $miss false
"###
)]
fn text_contains(
text: String,
needle: String,
) -> Result<Value> {
Ok(Value::bool(text.contains(needle.as_str())))
}
#[oxdock_func(pure, returns = TypeTag::Int)]
#[example(
name = "length",
code = r###"
# Plain text counts one per character.
IMPORT [STD]
LET $n: INT = TEXT_LENGTH("hello")
ASSERT_EQ $n 5
# Multibyte characters count once each, not per byte.
LET $wide: INT = TEXT_LENGTH("héllo")
ASSERT_EQ $wide 5
"###
)]
fn text_length(
text: String,
) -> Result<Value> {
Ok(Value::int(text.chars().count() as i64))
}
#[oxdock_func(pure, returns = TypeTag::Int)]
#[example(
name = "count",
code = r###"
# Every non-overlapping occurrence counts once.
IMPORT [STD]
LET $n: INT = TEXT_COUNT("hello world", "o")
ASSERT_EQ $n 2
# Overlapping candidates never double count.
LET $once: INT = TEXT_COUNT("aaa", "aa")
ASSERT_EQ $once 1
"###
)]
fn text_count(
text: String,
needle: String,
) -> Result<Value> {
let n = super::args::text_count_str(&text, &needle, "TEXT_COUNT")?;
Ok(Value::int(n))
}
#[oxdock_func(pure, returns = TypeTag::Int)]
#[example(
name = "count matches",
code = r###"
# One line matches once no matter how many times it hits.
IMPORT [STD]
LET $n: INT = TEXT_COUNT_MATCHES("test a: test\ntest b: nope\ntest c: test", ": test")
ASSERT_EQ $n 2
# A miss binds zero, never an error.
LET $none: INT = TEXT_COUNT_MATCHES("test b: nope", ": test$")
ASSERT_EQ $none 0
"###
)]
fn text_count_matches(
text: String,
pattern: String,
) -> Result<Value> {
let n = super::args::text_count_matches_str(&text, &pattern, "TEXT_COUNT_MATCHES")?;
Ok(Value::int(n))
}
#[oxdock_func(pure, returns = TypeTag::ListOf(std::sync::Arc::new(TypeTag::String)))]
#[example(
name = "grep",
code = r###"
# Matching lines come back as a list, in order.
IMPORT [STD]
LET $hits: LIST<STRING> = TEXT_GREP("one\ntwo\none more", "^one")
ASSERT_EQ $hits.0 "one"
ASSERT_EQ $hits.1 "one more"
# A miss binds an empty list, never an error.
LET $miss: LIST<STRING> = TEXT_GREP("one\ntwo", "^z")
LET $joined: STRING = TEXT_JOIN($miss, ",")
ASSERT_EQ $joined ""
# The (?i) flag ignores case, so uppercase lines match too.
LET $folded: LIST<STRING> = TEXT_GREP("One\ntwo\nONE more", "(?i)^one")
ASSERT_EQ $folded.0 "One"
ASSERT_EQ $folded.1 "ONE more"
"###
)]
#[example(
name = "grep invert",
code = r###"
# The invert option keeps every non-matching line instead.
IMPORT [STD]
LET $kept: LIST<STRING> = TEXT_GREP("one\ntwo\none more", "^one", {invert: true})
ASSERT_EQ $kept.0 "two"
# A pattern matching every line binds an empty list, never an error.
LET $none: LIST<STRING> = TEXT_GREP("one\ntwo", "^", {invert: true})
LET $joined: STRING = TEXT_JOIN($none, ",")
ASSERT_EQ $joined ""
# Options ride into placeholders as MAP literals. Placeholder
# strings unescape only backslash and quote, so multiline text
# arrives through a variable.
LET $two_lines: STRING = "one\ntwo"
LET $msg: STRING = 'kept:{{ STD::TEXT_GREP($two_lines, "^one", {invert: true}) }}'
ASSERT_EQ $msg "kept:two"
"###
)]
fn text_grep(
text: String,
pattern: String,
#[options("invert?: BOOL")]
#[optional]
options: BTreeMap<String, Value>,
) -> Result<Value> {
let invert = super::args::text_invert_flag(&options);
Ok(super::args::text_list_value(super::args::text_grep_str(
&text,
&pattern,
invert,
"TEXT_GREP",
)?))
}
#[oxdock_func(returns = TypeTag::Bool)]
#[example(
name = "drain to end",
code = r###"
# Capture two lines, then drain to end of stream with no sentinel line.
IMPORT [STD]
LET $cap: PIPE
WITH_IO [stdout=$cap] {
ECHO "one"
ECHO "two"
}
# The loop exits on close, and the count proves both lines drained.
LET $n: INT = 0
WHILE !EOF($cap) {
WITH_IO [stdin=$cap] READ_LINE $line
$n = $n + 1
}
ASSERT_EQ $n 2
"###
)]
fn eof<P: ProcessManager>(
cx: &mut StepCtx<P>,
pipe: PipeHandle,
) -> Result<Value> {
let info = cx.state.io.inspect_pipe(&pipe);
Ok(Value::bool(info.closed && info.buffered == 0))
}
fn meta_to_value(meta: &FuncMeta) -> Value {
let mut map = BTreeMap::new();
map.insert("name".to_string(), Value::string(meta.name.clone()));
map.insert("module".to_string(), Value::string(meta.module.clone()));
map.insert(
"kind".to_string(),
Value::string(meta.kind.label().to_string()),
);
let params = match &meta.params {
Some(params) => Value::list(
params
.iter()
.map(|p| {
let mut entry = BTreeMap::new();
entry.insert("name".to_string(), Value::string(p.name.clone()));
entry.insert(
"param_type".to_string(),
Value::string(
p.param_type
.as_ref()
.map(render_structural)
.unwrap_or_default(),
),
);
entry.insert(
"allowed".to_string(),
Value::string(
p.allowed
.map(|values| values.join(", "))
.unwrap_or_default(),
),
);
entry.insert("docs".to_string(), Value::string(String::new()));
entry.insert("optional".to_string(), Value::bool(p.optional));
entry.insert(
"options".to_string(),
match p.options {
Some(keys) => Value::list(
keys.iter()
.map(|key| {
let mut option = BTreeMap::new();
option.insert(
"name".to_string(),
Value::string(key.name.to_string()),
);
option.insert(
"value_type".to_string(),
Value::string(render_structural(&key.value)),
);
option.insert(
"required".to_string(),
Value::bool(key.required),
);
option.insert(
"default".to_string(),
Value::string(
key.default.unwrap_or_default().to_string(),
),
);
Value::map(option)
})
.collect(),
),
None => Value::string(String::new()),
},
);
Value::map(entry)
})
.collect(),
),
None => Value::list(Vec::new()),
};
map.insert("params".to_string(), params);
map.insert(
"returns".to_string(),
Value::string(
meta.returns
.as_ref()
.map(render_structural)
.unwrap_or_default(),
),
);
map.insert("rpn".to_string(), Value::bool(meta.rpn));
map.insert(
"summary".to_string(),
Value::string(meta.summary.to_string()),
);
Value::map(map)
}
#[derive(Debug, Clone)]
pub struct RecordSchema {
pub name: &'static str,
pub fields: std::sync::Arc<[Field]>,
}
#[derive(Clone)]
pub struct HostModule<P: ProcessManager> {
pub name: String,
pub funcs: Vec<HostRegistration<P>>,
pub types: Vec<&'static TypeDescriptor>,
pub record_schemas: Vec<RecordSchema>,
pub states: Vec<ModuleState>,
}
#[derive(Clone)]
pub struct ModuleState {
id: std::any::TypeId,
state: std::sync::Arc<dyn std::any::Any + Send + Sync>,
}
pub type PreRunInspector = dyn Fn(&[Step]) -> Result<()> + Send + Sync;
#[derive(Clone)]
pub struct PreRunCheck {
check: Arc<PreRunInspector>,
}
impl PreRunCheck {
pub fn new(check: impl Fn(&[Step]) -> Result<()> + Send + Sync + 'static) -> Self {
Self {
check: Arc::new(check),
}
}
pub fn run(&self, steps: &[Step]) -> Result<()> {
(self.check)(steps)
}
}
impl<P: ProcessManager> HostModule<P> {
pub fn with_state<T: Send + Sync + 'static>(mut self, state: std::sync::Arc<T>) -> Self {
self.states.push(ModuleState {
id: std::any::TypeId::of::<T>(),
state,
});
self
}
pub fn check_pre_run(&self, steps: &[Step]) -> Result<()> {
for state in &self.states {
if let Some(check) = state.state.downcast_ref::<PreRunCheck>() {
check.run(steps)?;
}
}
Ok(())
}
}
impl<P: ProcessManager> ExecState<P> {
pub fn register_module(&mut self, module: HostModule<P>) {
for registration in module.funcs {
match registration {
HostRegistration::Stateful { name, meta, func } => {
self.functions.register_host(&module.name, name, meta, func);
}
HostRegistration::Pure { name, meta, func } => {
self.functions
.register_pure_host(&module.name, name, meta, func);
}
HostRegistration::Both {
name,
meta,
func,
template,
} => {
self.functions
.register_both_host(&module.name, name, meta, func, template);
}
}
}
for descriptor in module.types {
self.register_type(descriptor);
}
for schema in module.record_schemas {
self.register_record_schema(schema.name, schema.fields);
}
for state in module.states {
self.module_states
.insert((module.name.clone(), state.id), state.state);
}
}
pub fn list_functions(&self) -> Vec<FuncMeta> {
let mut out: Vec<FuncMeta> = self.functions.entries_metas().into_iter().collect();
if !out.iter().any(|m| m.name == KEYWORD_INSPECT) {
out.push(FuncMeta {
name: KEYWORD_INSPECT.to_string(),
module: STD_MODULE_NAME.to_string(),
kind: FuncKind::HostCtx,
params: None,
returns: Some(TypeTag::Map),
rpn: false,
summary: "Inspect a variable binding.",
docs: "INSPECT($var): dedicated AST node taking a variable, not a value.",
examples: &[],
});
}
out.sort_by(|a, b| a.name.cmp(&b.name));
out
}
pub fn describe_function(&self, name: &str) -> Option<Value> {
if let Some(meta) = self.functions.meta(name) {
return Some(meta_to_value(&meta));
}
if name == KEYWORD_INSPECT {
return Some(meta_to_value(&FuncMeta {
name: KEYWORD_INSPECT.to_string(),
module: STD_MODULE_NAME.to_string(),
kind: FuncKind::HostCtx,
params: None,
returns: Some(TypeTag::Map),
rpn: false,
summary: "Inspect a variable binding.",
docs: "INSPECT($var): dedicated AST node taking a variable, not a value.",
examples: &[],
}));
}
None
}
pub(super) fn clone_native_pure(&self, name: &str) -> Option<PureFn> {
self.functions.clone_pure_fn(name)
}
pub fn pure_function_table(&self) -> Arc<PureTable> {
self.functions.pure_shared()
}
pub(super) fn clone_native_ctx(&self, name: &str) -> Option<NativeFn<P>> {
self.functions.clone_ctx_fn(name)
}
pub(super) fn native_meta(&self, name: &str) -> Option<FuncMeta> {
self.functions.meta(name)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn describe_entry_renders_union_spellings() {
let union = oxdock_parser::union_tag!(String, Pipe);
let meta = FuncMeta {
name: "T::F".to_string(),
module: "T".to_string(),
kind: FuncKind::HostPure,
params: Some(vec![FuncParam {
name: "text".to_string(),
param_type: Some(union.clone()),
allowed: None,
docs: "",
options: None,
optional: false,
}]),
returns: Some(union),
rpn: false,
summary: "test-only",
docs: "test-only",
examples: &[],
};
let value = meta_to_value(&meta);
let map = value.as_map().expect("describe value is a map");
let params = map
.get("params")
.expect("params key")
.as_list()
.expect("params is a list");
let first = params[0].as_map().expect("entry is a map");
assert_eq!(
first.get("param_type").and_then(Value::as_str),
Some("STRING|PIPE"),
"param renders the set"
);
assert_eq!(
map.get("returns").and_then(Value::as_str),
Some("STRING|PIPE"),
"returns render the set"
);
}
#[test]
fn describe_entry_carries_optional_flag() {
let meta = FuncMeta {
name: "T::F".to_string(),
module: "T".to_string(),
kind: FuncKind::HostPure,
params: Some(vec![
FuncParam {
name: "url".to_string(),
param_type: Some(TypeTag::String),
allowed: None,
docs: "",
options: None,
optional: false,
},
FuncParam {
name: "options".to_string(),
param_type: Some(TypeTag::Map),
allowed: None,
docs: "",
options: None,
optional: true,
},
]),
returns: Some(TypeTag::String),
rpn: false,
summary: "test-only",
docs: "test-only",
examples: &[],
};
let value = meta_to_value(&meta);
let map = value.as_map().expect("describe value is a map");
let params = map
.get("params")
.expect("params key")
.as_list()
.expect("params is a list");
assert_eq!(params.len(), 2);
let first = params[0].as_map().expect("entry is a map");
let second = params[1].as_map().expect("entry is a map");
assert_eq!(
first.get("optional").and_then(Value::as_bool),
Some(false),
"required param must read false"
);
assert_eq!(
second.get("optional").and_then(Value::as_bool),
Some(true),
"omittable param must read true"
);
}
#[test]
fn template_twins_mirror_string_paths_and_refuse_pipes() {
let pipe = Value::pipe_fresh();
type TwinCase = (&'static str, Box<dyn Fn() -> Result<Value>>);
let cases: Vec<TwinCase> = vec![
(
"TEXT_TRIM",
Box::new(|| text_trim_string(Value::string(" hello ".to_string()))),
),
(
"TEXT_TRIM_PREFIX",
Box::new(|| {
text_trim_prefix_string(Value::string("v1".to_string()), "v".to_string())
}),
),
(
"TEXT_TRIM_SUFFIX",
Box::new(|| {
text_trim_suffix_string(Value::string("a.gz".to_string()), "x".to_string())
}),
),
(
"TEXT_TO_UPPER",
Box::new(|| text_to_upper_string(Value::string("hi".to_string()))),
),
(
"TEXT_TO_LOWER",
Box::new(|| text_to_lower_string(Value::string("HI".to_string()))),
),
(
"TEXT_REPLACE",
Box::new(|| {
text_replace_string(
Value::string("ab".to_string()),
"b".to_string(),
"c".to_string(),
)
}),
),
(
"TEXT_REPLACE_REGEX",
Box::new(|| {
text_replace_regex_string(
Value::string("ab".to_string()),
"b".to_string(),
"c".to_string(),
)
}),
),
];
assert_eq!(cases.len(), 7, "every mirror op needs a twin row");
for (name, run) in &cases {
let value = run().unwrap_or_else(|e| panic!("{name} twin must answer strings: {e:#}"));
assert!(
value.as_str().is_some(),
"{name} twin must answer STRING, got {}",
value.type_name()
);
}
assert_eq!(
text_trim_string(Value::string(" hello ".to_string()))
.expect("trim twin")
.as_str(),
Some("hello")
);
let pipe_cases: Vec<(&str, Result<Value>)> = vec![
("TEXT_TRIM", text_trim_string(pipe.clone())),
(
"TEXT_TRIM_PREFIX",
text_trim_prefix_string(pipe.clone(), "v".to_string()),
),
(
"TEXT_TRIM_SUFFIX",
text_trim_suffix_string(pipe.clone(), ".gz".to_string()),
),
("TEXT_TO_UPPER", text_to_upper_string(pipe.clone())),
("TEXT_TO_LOWER", text_to_lower_string(pipe.clone())),
(
"TEXT_REPLACE",
text_replace_string(pipe.clone(), "a".to_string(), "b".to_string()),
),
(
"TEXT_REPLACE_REGEX",
text_replace_regex_string(pipe.clone(), "a".to_string(), "b".to_string()),
),
];
for (name, result) in pipe_cases {
let err = result.expect_err(&format!("{name} twin must refuse PIPE"));
assert!(
format!("{err:#}").contains("step IO"),
"{name} refusal names the missing context: {err:#}"
);
}
}
}