Lines
100 %
Functions
Branches
//! Flat forms (create, edit, convert, config) over `submit-form` and `load-form`.
use forms::{Form, FormValues, escape_str};
use nomiscript::Value;
use rpc::Session;
use crate::CommandError;
use crate::eval::{next_id, render_error_to_command};
use crate::forms::map_references;
use crate::render::{WireValue, parse_wire};
use crate::transaction::{apply_form, eval_text, parse_loaded_for, submit_call};
/// What `submit-form` answered.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Reply {
/// The id of the stored entity.
Entity(String),
/// A mutation without an entity to report.
Done,
/// A query's value, as printed.
Value(String),
}
/// A submit's reply and the notices given with it.
pub struct Submitted {
pub reply: Reply,
pub notices: Vec<String>,
fn reply_of(value: Value) -> Reply {
match value {
Value::String(id) => Reply::Entity(id),
Value::T => Reply::Done,
other => Reply::Value(nomiscript::format_value_display(&other)),
async fn send(
session: &mut Session,
form: &Form,
values: &FormValues,
) -> Result<Submitted, CommandError> {
let frame = format!("(:id {} :form {})", next_id(), submit_call(form, values));
let wire = session.handle_form(&frame).await;
let WireValue::Value(value) = parse_wire(&wire).map_err(render_error_to_command)?;
Ok(Submitted {
reply: reply_of(value),
notices: rpc::parse_notices(&wire),
})
/// Submit `values` against the create, query or config `form`.
///
/// # Errors
/// [`CommandError::Argument`] for an unknown or ambiguous reference,
/// [`CommandError::FormInvalid`] with one line per rejected field, or the
/// server error.
pub async fn submit(
let mapped = map_references(session, form, values).await?;
send(session, form, &mapped).await
/// The stored values of entity `id` in the edit `form`.
/// The server's refusal of the load, or [`CommandError::Command`] for a malformed payload.
pub async fn load_values(
id: &str,
) -> Result<FormValues, CommandError> {
let call = format!("(load-form {} {})", escape_str(form.name), escape_str(id));
parse_loaded_for(form, &eval_text(session, &call).await?)
/// Load entity `id`, apply `overrides` over it and submit the edit `form`.
/// As [`submit`], plus the load's refusal.
pub async fn edit(
overrides: &FormValues,
let loaded = load_values(session, form, id).await?;
let mapped = map_references(session, form, overrides).await?;
send(session, form, &apply_form(form, &loaded, &mapped, &[])?).await
#[cfg(test)]
mod tests;