Lines
94.59 %
Functions
32.69 %
Branches
100 %
//! Parsing of a data tab's reply: a list of plists, one per row.
use cli_core::render::{ErrorReply, RenderError, WireValue, parse_wire, reparse_list};
use scripting::nomiscript::{Value, format_value, list_to_vec};
/// The rows of a list reply, each built by `row`; `nil` is no rows. A string
/// value is the printed list a `form-options` reply carries.
pub fn parse_rows<T>(
wire: &str,
row: impl Fn(&Value) -> Result<T, String>,
) -> Result<Vec<T>, String> {
match parse_wire(wire) {
Err(RenderError::Server(ErrorReply { code, message, .. })) => {
Err(format!("[{code}] {message}"))
}
Err(e) => Err(e.to_string()),
Ok(WireValue::Value(Value::String(text))) => {
let printed = reparse_list(&text).map_err(|e| e.to_string())?;
rows_of(&printed, row)
Ok(WireValue::Value(value)) => rows_of(&value, row),
fn rows_of<T>(list: &Value, row: impl Fn(&Value) -> Result<T, String>) -> Result<Vec<T>, String> {
match list {
Value::Nil => Ok(Vec::new()),
value => list_to_vec(value)
.ok_or_else(|| format!("unexpected reply type: {}", format_value(value)))?
.iter()
.map(row)
.collect(),
/// The truth value after the `name` keyword: `t` or `nil`.
pub fn plist_flag(plist: &Value, name: &str) -> Option<bool> {
let items = list_to_vec(plist)?;
let pos = items
.position(|v| matches!(v, Value::Symbol(s) if s.eq_ignore_ascii_case(name)))?;
match items.get(pos + 1)? {
Value::T => Some(true),
Value::Nil => Some(false),
_ => None,
/// The string after the `name` keyword of `plist`.
pub fn plist_text(plist: &Value, name: &str) -> Option<String> {
Value::String(text) => Some(text.clone()),