Skip to main content

tui/app/
convert.rs

1//! Commodity-convert submit and reply handling for `App`.
2
3use cli_core::eval::parse_scalar_reply;
4use cli_core::transaction::submit_call;
5use forms::{CONVERT, FormValues};
6
7use crate::app::App;
8use crate::route::RouteCtx;
9
10impl App {
11    /// Submit the `convert` form; its ratio reply lands on the status line.
12    ///
13    /// Returns `None` only when there is no eval worker or it has stopped.
14    pub fn submit_commodity_convert(
15        &mut self,
16        values: &FormValues,
17        from_label: &str,
18        to_label: &str,
19    ) -> Option<i64> {
20        if self.console_eval.is_none() {
21            self.status = "console not connected".to_string();
22            return None;
23        }
24        let ctx = RouteCtx::ConvertQuery {
25            amount_str: values.text("amount").unwrap_or_default().to_string(),
26            from_label: from_label.to_string(),
27            to_label: to_label.to_string(),
28        };
29        let id = self.dispatch_app_eval(ctx, submit_call(&CONVERT, values));
30        if id.is_none() {
31            self.status = crate::tabs::nms_eval::UNAVAILABLE.to_string();
32        }
33        id
34    }
35
36    /// Handle a `convert` reply by formatting a status message.
37    pub(super) fn deliver_convert_reply(
38        &mut self,
39        wire: &str,
40        amount_str: &str,
41        from_label: &str,
42        to_label: &str,
43    ) {
44        match parse_scalar_reply(wire) {
45            Ok(result) => {
46                self.status = format!("{amount_str} {from_label} = {result} {to_label}");
47            }
48            Err(e) => self.status = format!("convert failed: {e}"),
49        }
50    }
51}