Skip to main content

cli_core/
forms.rs

1//! Parsers for the form-related wire replies (commodity and form
2//! options, loaded and carried values).
3
4pub use carried::copy_carried;
5pub use fill::commodity_fill;
6pub use loaded::parse_loaded_reply;
7pub use reference::{Fetched, map_references, map_values, resolve, sources_used};
8pub use suggestions::parse_suggestions;
9
10mod carried;
11mod fill;
12mod loaded;
13mod reference;
14mod suggestions;
15#[cfg(test)]
16pub mod tests;
17
18/// One `form-options` entry: the entity id, its label and its bare-name alias.
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct FormOption {
21    pub id: String,
22    pub label: String,
23    pub alias: String,
24    /// The only commodity an account holds, when it holds exactly one.
25    pub commodity: Option<String>,
26}
27
28/// Parse a `form-options` wire reply into its options.
29///
30/// Returns `[]` on any parse failure — never panics.
31pub fn parse_form_options(wire: &str) -> Vec<FormOption> {
32    use crate::render::{WireValue, parse_wire};
33    use nomiscript::Value;
34
35    match parse_wire(wire) {
36        Ok(WireValue::Value(Value::String(ref s))) => parse_option_list(s),
37        _ => vec![],
38    }
39}
40
41/// Parse the unwrapped list text of a `form-options` reply.
42///
43/// Returns `[]` on any parse failure — never panics.
44pub fn parse_option_list(text: &str) -> Vec<FormOption> {
45    use crate::eval::plist_field;
46    use crate::render::reparse_list;
47    use nomiscript::list_to_vec;
48
49    let Ok(list) = reparse_list(text) else {
50        return vec![];
51    };
52    list_to_vec(&list)
53        .unwrap_or_default()
54        .iter()
55        .filter_map(|e| {
56            let id = plist_field(e, ":id")?;
57            let label = plist_field(e, ":label")?;
58            Some(FormOption {
59                alias: plist_field(e, ":alias").unwrap_or_else(|| label.clone()),
60                commodity: plist_field(e, ":commodity"),
61                label,
62                id,
63            })
64        })
65        .collect()
66}