use std::fmt;
#[derive(Clone)]
pub struct Args {
pub endpoint: Option<String>,
pub command: String,
pub args: Vec<String>,
pub with: Vec<String>,
pub stdin: Vec<u8>,
}
impl<const N: usize> From<[&'static str; N]> for Args {
fn from(value: [&str; N]) -> Self {
let command = value[0];
let with = value[1..].iter().map(|s| s.to_string()).collect();
let mut args = Args::new(command);
args.with = with;
args
}
}
impl Args {
pub fn new(command: impl ToString) -> Self {
Self {
endpoint: None,
command: command.to_string(),
args: vec![],
with: vec![],
stdin: vec![],
}
}
pub fn endpoint(mut self, endpoint: impl ToString) -> Self {
self.endpoint = Some(endpoint.to_string());
self
}
pub fn program_stdin(mut self, bytes: impl Into<Vec<u8>>) -> Self {
self.with.push("-".to_string());
self.stdin = bytes.into();
self
}
}
macro_rules! impl_args {
(
flags: $($flag:tt),+;
values: $($value:tt),+;
) => {
impl Args {
$(
pub fn $flag(mut self, value: impl fmt::Display) -> Self {
self.args.push(format!("--{flag}={value}", flag = stringify!($flag).replace("_", "-")));
self
}
)*
$(
pub fn $value(mut self, value: impl ToString) -> Self {
self.with.push(value.to_string());
self
}
)*
}
};
}
impl_args!(
flags: payload, gas_limit;
values:
message_id,
address,
action,
destination,
amount,
flag;
);