//! The command `verbs`, and every service's verbs as commands: `p2p` alone //! says what the p2p service can do, `p2p send contact=Anna text="Hello"` //! does it. The terminal starts this for a line that begins with a //! service's name (`pinnacle_ui::command`). The arguments are checked //! against the service's own description before anything is sent, and the //! call is the message the agent's gatekeeper sends for the same verb. #![no_std] #![no_main] extern crate alloc; use alloc::string::String; use alloc::vec::Vec; use pinnacle_json::Value; use pinnacle_rt::output::{Output, Record}; use pinnacle_ui::verbs; #[unsafe(no_mangle)] fn main(argument: u64) -> u64 { let out = Output::start(argument); // args[0] is "verbs"; then a service, a verb, and its arguments. let words: Vec<&str> = out.args.iter().skip(1).map(String::as_str).collect(); let Some(&service) = words.first() else { out.send(&Record::Table { columns: alloc::vec!["Service".into(), "What it is for".into()], rows: verbs::SERVICES .iter() .map(|(s, about)| alloc::vec![String::from(*s), String::from(*about)]) .collect(), }); return out.status(0, ""); }; let Some((channel, description)) = verbs::describe(service) else { return out.status(1, &alloc::format!("“{service}” does not say what it can do")); }; let Some(&name) = words.get(1) else { out.text(&description.about); out.send(&Record::Table { columns: alloc::vec!["Verb".into(), "Takes".into(), "Does".into(), "Its effect".into()], rows: description .verbs .iter() .map(|v| { let takes = v .params .iter() .map(|p| alloc::format!("{}={}", p.name, p.kind.name())) .collect::>() .join(" "); let mut effect = String::from(v.effect.describe()); if let Some(inverse) = &v.inverse { effect += &alloc::format!(", undone by {inverse}"); } alloc::vec![v.name.clone(), takes, v.about.clone(), effect] }) .collect(), }); return out.status(0, ""); }; let Some(verb) = description.verb(name) else { return out.status( 1, &alloc::format!("{service} has no verb “{name}”: {service} lists them"), ); }; let args = match verbs::parse_args(verb, &words[2..].join(" ")) { Ok(args) => args, Err(why) => return out.status(1, &why), }; let result = verbs::call(&channel, name, args, Vec::new()); match &result.value { Value::Array(items) => out.send(&Record::Table { columns: alloc::vec![String::new()], rows: items .iter() .map(|i| alloc::vec![i.as_str().map_or_else(|| alloc::format!("{i}"), String::from)]) .collect(), }), _ => out.text(&result.text()), }; out.status(u64::from(!result.ok), "") }