(opts: Opts)
| 30 | } |
| 31 | |
| 32 | fn repl(opts: Opts) -> anyhow::Result<()> { |
| 33 | let mut replica = opts.replica.unwrap_or_else(|| "local".to_string()); |
| 34 | let offline = if opts.offline { |
| 35 | replica = "ic".to_string(); |
| 36 | let send_url = opts |
| 37 | .url |
| 38 | .unwrap_or_else(|| "https://qhmh2-niaaa-aaaab-qadta-cai.raw.icp0.io/?msg=".to_string()); |
| 39 | Some(match opts.format.as_deref() { |
| 40 | None | Some("json") => OfflineOutput::Json, |
| 41 | Some("ascii") => OfflineOutput::Ascii(send_url), |
| 42 | Some("png") => OfflineOutput::Png(send_url), |
| 43 | Some("png_no_url") => OfflineOutput::PngNoUrl, |
| 44 | Some("ascii_no_url") => OfflineOutput::AsciiNoUrl, |
| 45 | _ => unreachable!(), |
| 46 | }) |
| 47 | } else { |
| 48 | None |
| 49 | }; |
| 50 | let url = match replica.as_str() { |
| 51 | "local" => "http://localhost:4943/", |
| 52 | "ic" => "https://icp0.io", |
| 53 | url => url, |
| 54 | }; |
| 55 | println!("Ping {url}..."); |
| 56 | let agent = Agent::builder() |
| 57 | .with_url(url) |
| 58 | .with_max_tcp_error_retries(2) |
| 59 | .with_max_polling_time(std::time::Duration::from_secs(60 * 10)) |
| 60 | .build()?; |
| 61 | |
| 62 | println!("Canister REPL"); |
| 63 | let config = rustyline::Config::builder() |
| 64 | .history_ignore_space(true) |
| 65 | .completion_type(CompletionType::List) |
| 66 | .build(); |
| 67 | let h = MyHelper::new(agent, url.to_string(), offline, opts.verbose); |
| 68 | if let Some(file) = opts.send { |
| 69 | use crate::offline::{send_messages, Messages}; |
| 70 | let json = std::fs::read_to_string(file)?; |
| 71 | let msgs = serde_json::from_str::<Messages>(&json)?; |
| 72 | send_messages(&h, &msgs)?; |
| 73 | return Ok(()); |
| 74 | } |
| 75 | let mut rl = rustyline::Editor::with_config(config)?; |
| 76 | rl.set_helper(Some(h)); |
| 77 | let _ = rl.load_history("./.history"); |
| 78 | if let Some(file) = opts.config { |
| 79 | let config = std::fs::read_to_string(file)?; |
| 80 | rl.helper_mut().unwrap().config = config.parse::<candid_parser::configs::Configs>()?; |
| 81 | } |
| 82 | |
| 83 | let enter_repl = opts.script.is_none() || opts.interactive; |
| 84 | if let Some(file) = opts.script { |
| 85 | let cmd = Command::Load(exp::Exp::Text(file)); |
| 86 | let helper = rl.helper_mut().unwrap(); |
| 87 | cmd.run(helper)?; |
| 88 | if helper.func_env.0.contains_key("__main") { |
| 89 | let mut args = Vec::new(); |
no test coverage detected