Lines
95 %
Functions
100 %
Branches
//! Parsers for the form-related wire replies (commodity and form
//! options, loaded and carried values).
pub use carried::copy_carried;
pub use fill::commodity_fill;
pub use loaded::parse_loaded_reply;
pub use reference::{Fetched, map_references, map_values, resolve, sources_used};
pub use suggestions::parse_suggestions;
mod carried;
mod fill;
mod loaded;
mod reference;
mod suggestions;
#[cfg(test)]
pub mod tests;
/// One `form-options` entry: the entity id, its label and its bare-name alias.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FormOption {
pub id: String,
pub label: String,
pub alias: String,
/// The only commodity an account holds, when it holds exactly one.
pub commodity: Option<String>,
}
/// Parse a `form-options` wire reply into its options.
///
/// Returns `[]` on any parse failure — never panics.
pub fn parse_form_options(wire: &str) -> Vec<FormOption> {
use crate::render::{WireValue, parse_wire};
use nomiscript::Value;
match parse_wire(wire) {
Ok(WireValue::Value(Value::String(ref s))) => parse_option_list(s),
_ => vec![],
/// Parse the unwrapped list text of a `form-options` reply.
pub fn parse_option_list(text: &str) -> Vec<FormOption> {
use crate::eval::plist_field;
use crate::render::reparse_list;
use nomiscript::list_to_vec;
let Ok(list) = reparse_list(text) else {
return vec![];
};
list_to_vec(&list)
.unwrap_or_default()
.iter()
.filter_map(|e| {
let id = plist_field(e, ":id")?;
let label = plist_field(e, ":label")?;
Some(FormOption {
alias: plist_field(e, ":alias").unwrap_or_else(|| label.clone()),
commodity: plist_field(e, ":commodity"),
label,
id,
})
.collect()