use std::path::PathBuf;
use std::time::Duration;
use super::spec::{parse_duration, ChildError, ChildOptions, ChildSpec};
use super::RemoteChild;
#[derive(clap::Args, Debug, Clone)]
#[command(next_help_heading = "Children")]
pub struct ChildArgs {
#[arg(long = "child", value_name = "NAME=stdio:CMD | NAME=ws://…", env = "PLEXUS_CHILDREN", value_delimiter = ';')]
pub child: Vec<ChildSpec>,
#[arg(long = "child-timeout", value_name = "DURATION", default_value = "5s", value_parser = parse_duration, hide_short_help = true)]
pub child_timeout: Duration,
#[arg(long = "child-max-calls", value_name = "N", default_value_t = 1024, hide_short_help = true)]
pub child_max_calls: usize,
#[arg(long = "child-env", value_name = "KEY", hide_short_help = true)]
pub child_env: Vec<String>,
#[arg(long = "child-key", value_name = "PATH", hide_short_help = true)]
pub child_key: Option<PathBuf>,
#[arg(long = "child-max-depth", value_name = "N", default_value_t = 8, hide_short_help = true)]
pub child_max_depth: usize,
#[arg(long = "child-schema-cache", value_name = "N", default_value_t = 1024, hide_short_help = true)]
pub child_schema_cache: usize,
#[arg(long = "child-legacy-upcalls", hide_short_help = true)]
pub child_legacy_upcalls: bool,
}
impl Default for ChildArgs {
fn default() -> Self {
let d = ChildOptions::default();
Self {
child: Vec::new(),
child_timeout: d.timeout,
child_max_calls: d.max_calls,
child_env: Vec::new(),
child_key: None,
child_max_depth: d.max_depth,
child_schema_cache: d.schema_cache,
child_legacy_upcalls: d.legacy_upcalls,
}
}
}
impl ChildArgs {
pub fn options(&self, service: &str) -> ChildOptions {
ChildOptions {
timeout: self.child_timeout,
max_calls: self.child_max_calls,
env: self.child_env.clone(),
key: self.child_key.clone(),
max_depth: self.child_max_depth,
schema_cache: self.child_schema_cache,
legacy_upcalls: self.child_legacy_upcalls,
..ChildOptions::for_service(service)
}
}
pub async fn connect_all_in(&self, service: &str, config: &crate::plexus::config::Config) -> Result<Vec<RemoteChild>, ChildError> {
let given = self.options(service);
given.seed(config);
let opts = given.configured(config);
let mut out = Vec::with_capacity(self.child.len());
for spec in &self.child {
out.push(RemoteChild::connect(spec.clone(), opts.clone()).await?);
}
Ok(out)
}
pub async fn connect_all(&self, service: &str) -> Result<Vec<RemoteChild>, ChildError> {
let opts = self.options(service);
let mut out = Vec::with_capacity(self.child.len());
for spec in &self.child {
out.push(RemoteChild::connect(spec.clone(), opts.clone()).await?);
}
Ok(out)
}
}
#[cfg(test)]
mod tests {
use super::*;
use clap::Parser;
#[derive(Parser)]
struct Cli {
#[command(flatten)]
children: ChildArgs,
}
#[test]
fn the_flags_read() {
let c = Cli::try_parse_from(["svc", "--child", "echo=stdio:python3 -I x.py", "--child", "far=ws://127.0.0.1:4480", "--child-timeout", "250ms", "--child-max-calls", "8", "--child-env", "RUST_LOG", "--child-key", "/k"]).unwrap();
assert_eq!(c.children.child.len(), 2);
assert_eq!(c.children.child[1], ChildSpec::Ws { name: "far".into(), url: "ws://127.0.0.1:4480".into() });
let o = c.children.options("menger");
assert_eq!((o.timeout, o.max_calls, o.principal.as_str(), o.env.clone()), (Duration::from_millis(250), 8, "service:menger", vec!["RUST_LOG".to_string()]));
assert_eq!(o.key.as_deref(), Some(std::path::Path::new("/k")));
let d = Cli::try_parse_from(["svc"]).unwrap().children;
let o = ChildArgs::default();
assert_eq!((o.child_timeout, o.child_max_calls, o.child_max_depth, o.child_schema_cache), (d.child_timeout, d.child_max_calls, d.child_max_depth, d.child_schema_cache), "Default is the flags' defaults");
assert!(d.child.is_empty());
assert_eq!((d.child_timeout, d.child_max_calls, d.child_max_depth), (Duration::from_secs(5), 1024, 8));
let deep = Cli::try_parse_from(["svc", "--child-max-depth", "3"]).unwrap().children;
assert_eq!(deep.options("menger").max_depth, 3);
assert_eq!(d.options("menger").schema_cache, 1024);
let few = Cli::try_parse_from(["svc", "--child-schema-cache", "16"]).unwrap().children;
assert_eq!(few.options("menger").schema_cache, 16);
let e = Cli::try_parse_from(["svc", "--child", "a.b=stdio:x"]).err().unwrap().to_string();
assert!(e.contains("dotted"), "{e}");
}
}