1
//! Parsers for the form-related wire replies (commodity and form
2
//! options, loaded and carried values).
3

            
4
pub use carried::copy_carried;
5
pub use fill::commodity_fill;
6
pub use loaded::parse_loaded_reply;
7
pub use reference::{Fetched, map_references, map_values, resolve, sources_used};
8
pub use suggestions::parse_suggestions;
9

            
10
mod carried;
11
mod fill;
12
mod loaded;
13
mod reference;
14
mod suggestions;
15
#[cfg(test)]
16
pub mod tests;
17

            
18
/// One `form-options` entry: the entity id, its label and its bare-name alias.
19
#[derive(Debug, Clone, PartialEq, Eq)]
20
pub 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.
31
2095
pub fn parse_form_options(wire: &str) -> Vec<FormOption> {
32
    use crate::render::{WireValue, parse_wire};
33
    use nomiscript::Value;
34

            
35
2095
    match parse_wire(wire) {
36
2081
        Ok(WireValue::Value(Value::String(ref s))) => parse_option_list(s),
37
14
        _ => vec![],
38
    }
39
2095
}
40

            
41
/// Parse the unwrapped list text of a `form-options` reply.
42
///
43
/// Returns `[]` on any parse failure — never panics.
44
2378
pub 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
2378
    let Ok(list) = reparse_list(text) else {
50
        return vec![];
51
    };
52
2378
    list_to_vec(&list)
53
2378
        .unwrap_or_default()
54
2378
        .iter()
55
4636
        .filter_map(|e| {
56
4636
            let id = plist_field(e, ":id")?;
57
4636
            let label = plist_field(e, ":label")?;
58
            Some(FormOption {
59
4636
                alias: plist_field(e, ":alias").unwrap_or_else(|| label.clone()),
60
4636
                commodity: plist_field(e, ":commodity"),
61
4636
                label,
62
4636
                id,
63
            })
64
4636
        })
65
        .collect()
66
2378
}