1
//! Reads a `load-form` reply back into the form's raw values.
2

            
3
use forms::FormValues;
4

            
5
use crate::transaction::parse_loaded_for;
6
use nomiscript::Value;
7

            
8
use crate::render::{RenderError, WireValue, parse_wire};
9

            
10
/// Parse the wire reply of a `load-form` call for `form` into [`FormValues`].
11
///
12
/// # Errors
13
/// The server's refusal message, or a message when the reply is not a payload.
14
1014
pub fn parse_loaded_reply(form: &forms::Form, wire: &str) -> Result<FormValues, String> {
15
1014
    match parse_wire(wire) {
16
        Ok(WireValue::Value(Value::String(text))) => {
17
904
            parse_loaded_for(form, &text).map_err(|e| e.to_string())
18
        }
19
        Ok(_) => Err("the reply is not a form payload".to_string()),
20
97
        Err(RenderError::Server(reply)) => Err(reply.message),
21
13
        Err(other) => Err(other.to_string()),
22
    }
23
1014
}
24

            
25
#[cfg(test)]
26
mod tests;