Skip to main content

cli_core/
flat.rs

1//! Flat forms (create, edit, convert, config) over `submit-form` and `load-form`.
2
3use forms::{Form, FormValues, escape_str};
4use nomiscript::Value;
5use rpc::Session;
6
7use crate::CommandError;
8use crate::eval::{next_id, render_error_to_command};
9use crate::forms::map_references;
10use crate::render::{WireValue, parse_wire};
11use crate::transaction::{apply_form, eval_text, parse_loaded_for, submit_call};
12
13/// What `submit-form` answered.
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub enum Reply {
16    /// The id of the stored entity.
17    Entity(String),
18    /// A mutation without an entity to report.
19    Done,
20    /// A query's value, as printed.
21    Value(String),
22}
23
24/// A submit's reply and the notices given with it.
25#[derive(Debug, Clone, PartialEq, Eq)]
26pub struct Submitted {
27    pub reply: Reply,
28    pub notices: Vec<String>,
29}
30
31fn reply_of(value: Value) -> Reply {
32    match value {
33        Value::String(id) => Reply::Entity(id),
34        Value::T => Reply::Done,
35        other => Reply::Value(nomiscript::format_value_display(&other)),
36    }
37}
38
39async fn send(
40    session: &mut Session,
41    form: &Form,
42    values: &FormValues,
43) -> Result<Submitted, CommandError> {
44    let frame = format!("(:id {} :form {})", next_id(), submit_call(form, values));
45    let wire = session.handle_form(&frame).await;
46    let WireValue::Value(value) = parse_wire(&wire).map_err(render_error_to_command)?;
47    Ok(Submitted {
48        reply: reply_of(value),
49        notices: rpc::parse_notices(&wire),
50    })
51}
52
53/// Submit `values` against the create, query or config `form`.
54///
55/// # Errors
56/// [`CommandError::Argument`] for an unknown or ambiguous reference,
57/// [`CommandError::FormInvalid`] with one line per rejected field, or the
58/// server error.
59pub async fn submit(
60    session: &mut Session,
61    form: &Form,
62    values: &FormValues,
63) -> Result<Submitted, CommandError> {
64    let mapped = map_references(session, form, values).await?;
65    send(session, form, &mapped).await
66}
67
68/// The stored values of entity `id` in the edit `form`.
69///
70/// # Errors
71/// The server's refusal of the load, or [`CommandError::Command`] for a malformed payload.
72pub async fn load_values(
73    session: &mut Session,
74    form: &Form,
75    id: &str,
76) -> Result<FormValues, CommandError> {
77    let call = format!("(load-form {} {})", escape_str(form.name), escape_str(id));
78    parse_loaded_for(form, &eval_text(session, &call).await?)
79}
80
81/// Load entity `id`, apply `overrides` over it and submit the edit `form`.
82///
83/// # Errors
84/// As [`submit`], plus the load's refusal.
85pub async fn edit(
86    session: &mut Session,
87    form: &Form,
88    id: &str,
89    overrides: &FormValues,
90) -> Result<Submitted, CommandError> {
91    let loaded = load_values(session, form, id).await?;
92    let mapped = map_references(session, form, overrides).await?;
93    send(session, form, &apply_form(form, &loaded, &mapped, &[])?).await
94}
95
96#[cfg(test)]
97mod tests;