1
//! Account commands over the client-form natives.
2

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

            
7
use crate::CommandError;
8
use crate::eval::{escape_str, plist_field};
9
use crate::forms::map_references;
10
use crate::transaction::{eval_text, submit_call};
11

            
12
/// Merge the `from` account of `values` into its `into` account; the id of
13
/// the account that remains.
14
///
15
/// # Errors
16
/// [`CommandError::Argument`] for an unknown or ambiguous pick, or the
17
/// server's refusal.
18
18
pub async fn merge(session: &mut Session, values: &FormValues) -> Result<String, CommandError> {
19
18
    let mapped = map_references(session, &ACCOUNT_MERGE, values).await?;
20
14
    eval_text(session, &submit_call(&ACCOUNT_MERGE, &mapped)).await
21
18
}
22

            
23
/// The `(merge-preview …)` call for the merge `values` describe.
24
#[must_use]
25
145
pub fn merge_preview_call(values: &FormValues) -> String {
26
145
    format!(
27
        "(merge-preview {})",
28
145
        escape_str(&write_payload(&ACCOUNT_MERGE, values))
29
    )
30
145
}
31

            
32
/// The count of splits a `merge-preview` reply says would move.
33
#[must_use]
34
98
pub fn merge_preview_splits(preview: &Value) -> Option<u64> {
35
98
    plist_field(preview, ":splits")?.parse().ok()
36
98
}
37

            
38
#[cfg(test)]
39
mod tests;