Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ pub enum Commands {
#[arg()]
args: Vec<String>,

/// Keyword argument for the call. To enforce value is always a string, send value in quotes e.g."'1'" or '"true"'. (May be provided multiple times)
#[arg(short = 'k', long = "kwarg", value_name = "KEY=VALUE")]
kwargs: Vec<String>,

/// WAMP call option (May be provided multiple times)
#[arg(short = 'o', long = "option", value_name = "KEY=VALUE")]
options: Vec<String>,

/// Number of times to repeat the call per session
#[arg(long, default_value_t = 1)]
repeat: u32,
Expand Down
47 changes: 42 additions & 5 deletions src/commands/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,54 @@ use std::sync::Arc;
use tokio::sync::Semaphore;
use xconn::sync::CallRequest;

/// Builds a CallRequest from the procedure name and parsed arguments.
fn build_call_request(procedure: &str, args: &[String]) -> CallRequest {
let mut request = CallRequest::new(procedure);
for arg in args {
/// Parses a "key=value" string and returns the key and parsed value.
fn parse_key_value(input: &str) -> Option<(String, ParsedArg)> {
let parts: Vec<&str> = input.splitn(2, '=').collect();
if parts.len() == 2 {
Some((parts[0].to_string(), parse_arg(parts[1])))
} else {
None
}
}

/// Builds a CallRequest from the CallConfig.
fn build_call_request(config: &CallConfig) -> CallRequest {
let mut request = CallRequest::new(&config.procedure);

// Add positional arguments
for arg in &config.args {
request = match parse_arg(arg) {
ParsedArg::Integer(v) => request.arg(v),
ParsedArg::Float(v) => request.arg(v),
ParsedArg::Boolean(v) => request.arg(v),
ParsedArg::String(v) => request.arg(v),
};
}

// Add keyword arguments
for kwarg in &config.kwargs {
if let Some((key, value)) = parse_key_value(kwarg) {
request = match value {
ParsedArg::Integer(v) => request.kwarg(&key, v),
ParsedArg::Float(v) => request.kwarg(&key, v),
ParsedArg::Boolean(v) => request.kwarg(&key, v),
ParsedArg::String(v) => request.kwarg(&key, v),
};
}
}

// Add options
for opt in &config.options {
if let Some((key, value)) = parse_key_value(opt) {
request = match value {
ParsedArg::Integer(v) => request.option(&key, v),
ParsedArg::Float(v) => request.option(&key, v),
ParsedArg::Boolean(v) => request.option(&key, v),
ParsedArg::String(v) => request.option(&key, v),
};
}
}

request
}

Expand All @@ -33,7 +70,7 @@ async fn run_session(
};

for iteration in 0..call_config.repeat {
let request = build_call_request(&call_config.procedure, &call_config.args);
let request = build_call_request(&call_config);

match session.call(request).await {
Ok(result) => {
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ impl From<&Cli> for ConnectionConfig {
pub struct CallConfig {
pub procedure: String,
pub args: Vec<String>,
pub kwargs: Vec<String>,
pub options: Vec<String>,
pub repeat: u32,
pub parallel: u32,
pub concurrency: usize,
Expand Down
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Commands::Call {
procedure,
args,
kwargs,
options,
repeat,
parallel,
concurrency,
} => {
let call_config = CallConfig {
procedure,
args,
kwargs,
options,
repeat,
parallel,
concurrency,
Expand Down
Loading