use bingle_core::api::bingle_api::StartOptions;
use bingle_core::util::cli_utils::parse_start_options_from_args;
const HANDLE_FROM_STATE_FILE: &str = "__bingle_chat_handle_from_state_file__";
#[derive(Debug)]
pub struct ChatArgs {
pub opts: StartOptions,
pub to: Option<String>,
pub to_id: Option<String>,
pub state_file: Option<String>,
pub no_retries: bool,
}
pub fn parse_chat_args(args: Vec<String>) -> Result<ChatArgs, String> {
let mut to: Option<String> = None;
let mut to_id: Option<String> = None;
let mut state_file: Option<String> = None;
let mut no_retries = false;
let mut rest: Vec<String> = Vec::with_capacity(args.len());
let mut it = args.into_iter();
while let Some(arg) = it.next() {
match arg.as_str() {
"--to" => {
to = Some(it.next().ok_or("--to requires a <handle> value")?);
}
"--to-id" => {
to_id = Some(it.next().ok_or("--to-id requires an <id> value")?);
}
"--state_file" | "--state-file" => {
state_file = Some(it.next().ok_or("--state_file requires a <file> value")?);
}
"--no-retries" | "--no-retry" => {
no_retries = true;
}
"--debug" | "--info" | "--warn" => {}
_ => rest.push(arg),
}
}
if to.is_some() && to_id.is_some() {
return Err("--to and --to-id are mutually exclusive".to_string());
}
let opts = match parse_start_options_from_args(rest.clone()) {
Ok(o) => o,
Err(e) if state_file.is_some() && e.starts_with("Missing handle") => {
let mut with_placeholder = rest;
with_placeholder.push(HANDLE_FROM_STATE_FILE.to_string());
let mut o = parse_start_options_from_args(with_placeholder)?;
o.handle = String::new();
o
}
Err(e) => return Err(e),
};
Ok(ChatArgs {
opts,
to,
to_id,
state_file,
no_retries,
})
}