1
//! Commodity-convert submit and reply handling for `App`.
2

            
3
use cli_core::eval::parse_scalar_reply;
4
use cli_core::transaction::submit_call;
5
use forms::{CONVERT, FormValues};
6

            
7
use crate::app::App;
8
use crate::route::RouteCtx;
9

            
10
impl 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
5
    pub fn submit_commodity_convert(
15
5
        &mut self,
16
5
        values: &FormValues,
17
5
        from_label: &str,
18
5
        to_label: &str,
19
5
    ) -> Option<i64> {
20
5
        if self.console_eval.is_none() {
21
1
            self.status = "console not connected".to_string();
22
1
            return None;
23
4
        }
24
4
        let ctx = RouteCtx::ConvertQuery {
25
4
            amount_str: values.text("amount").unwrap_or_default().to_string(),
26
4
            from_label: from_label.to_string(),
27
4
            to_label: to_label.to_string(),
28
4
        };
29
4
        let id = self.dispatch_app_eval(ctx, submit_call(&CONVERT, values));
30
4
        if id.is_none() {
31
            self.status = crate::tabs::nms_eval::UNAVAILABLE.to_string();
32
4
        }
33
4
        id
34
5
    }
35

            
36
    /// Handle a `convert` reply by formatting a status message.
37
2
    pub(super) fn deliver_convert_reply(
38
2
        &mut self,
39
2
        wire: &str,
40
2
        amount_str: &str,
41
2
        from_label: &str,
42
2
        to_label: &str,
43
2
    ) {
44
2
        match parse_scalar_reply(wire) {
45
            Ok(result) => {
46
1
                self.status = format!("{amount_str} {from_label} = {result} {to_label}");
47
            }
48
1
            Err(e) => self.status = format!("convert failed: {e}"),
49
        }
50
2
    }
51
}