1
//! Flat forms (create, edit, convert, config) over `submit-form` and `load-form`.
2

            
3
use forms::{Form, FormValues, escape_str};
4
use nomiscript::Value;
5
use rpc::Session;
6

            
7
use crate::CommandError;
8
use crate::eval::{next_id, render_error_to_command};
9
use crate::forms::map_references;
10
use crate::render::{WireValue, parse_wire};
11
use crate::transaction::{apply_form, eval_text, parse_loaded_for, submit_call};
12

            
13
/// What `submit-form` answered.
14
#[derive(Debug, Clone, PartialEq, Eq)]
15
pub 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)]
26
pub struct Submitted {
27
    pub reply: Reply,
28
    pub notices: Vec<String>,
29
}
30

            
31
247
fn reply_of(value: Value) -> Reply {
32
247
    match value {
33
245
        Value::String(id) => Reply::Entity(id),
34
1
        Value::T => Reply::Done,
35
1
        other => Reply::Value(nomiscript::format_value_display(&other)),
36
    }
37
247
}
38

            
39
307
async fn send(
40
307
    session: &mut Session,
41
307
    form: &Form,
42
307
    values: &FormValues,
43
307
) -> Result<Submitted, CommandError> {
44
307
    let frame = format!("(:id {} :form {})", next_id(), submit_call(form, values));
45
307
    let wire = session.handle_form(&frame).await;
46
307
    let WireValue::Value(value) = parse_wire(&wire).map_err(render_error_to_command)?;
47
247
    Ok(Submitted {
48
247
        reply: reply_of(value),
49
247
        notices: rpc::parse_notices(&wire),
50
247
    })
51
307
}
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.
59
234
pub async fn submit(
60
234
    session: &mut Session,
61
234
    form: &Form,
62
234
    values: &FormValues,
63
234
) -> Result<Submitted, CommandError> {
64
234
    let mapped = map_references(session, form, values).await?;
65
232
    send(session, form, &mapped).await
66
234
}
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.
72
104
pub async fn load_values(
73
104
    session: &mut Session,
74
104
    form: &Form,
75
104
    id: &str,
76
104
) -> Result<FormValues, CommandError> {
77
104
    let call = format!("(load-form {} {})", escape_str(form.name), escape_str(id));
78
104
    parse_loaded_for(form, &eval_text(session, &call).await?)
79
104
}
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.
85
76
pub async fn edit(
86
76
    session: &mut Session,
87
76
    form: &Form,
88
76
    id: &str,
89
76
    overrides: &FormValues,
90
76
) -> Result<Submitted, CommandError> {
91
76
    let loaded = load_values(session, form, id).await?;
92
75
    let mapped = map_references(session, form, overrides).await?;
93
75
    send(session, form, &apply_form(form, &loaded, &mapped, &[])?).await
94
76
}
95

            
96
#[cfg(test)]
97
mod tests;