Skip to main content

rpc/forms/
suggest.rs

1//! The free-text suggestions of a text cell, shared by every client.
2
3use forms::{Scope, Suggest};
4use server::error::ServerError;
5use server::user::User;
6use uuid::Uuid;
7
8/// What a text cell asks to be suggested.
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum Suggestion {
11    TagNames(Scope),
12    /// The values of the tag of this scope with this name.
13    TagValues(Scope, String),
14}
15
16impl Suggestion {
17    /// The request `suggest` makes; a values source needs the tag name and a
18    /// names source takes none.
19    ///
20    /// # Errors
21    /// The message naming the misplaced or missing tag name.
22    pub fn of(suggest: Suggest, tag: Option<&str>) -> Result<Self, String> {
23        match (suggest, tag) {
24            (Suggest::TagNames(scope), None) => Ok(Self::TagNames(scope)),
25            (Suggest::TagValues(scope), Some(tag)) => Ok(Self::TagValues(scope, tag.to_string())),
26            (Suggest::TagNames(_), Some(_)) => Err(format!("{} takes no tag name", suggest.name())),
27            (Suggest::TagValues(_), None) => {
28                Err(format!("{0} needs '{0}:<tag name>'", suggest.name()))
29            }
30        }
31    }
32}
33
34/// The user's strings for `request`.
35///
36/// # Errors
37/// The server's failure to list them.
38pub async fn suggestions(user_id: Uuid, request: &Suggestion) -> Result<Vec<String>, ServerError> {
39    let user = User { id: user_id };
40    match request {
41        Suggestion::TagNames(Scope::Transaction) => user.list_transaction_tag_names().await,
42        Suggestion::TagNames(Scope::Split) => user.list_split_tag_names().await,
43        Suggestion::TagNames(Scope::Account) => user.list_account_tag_names().await,
44        Suggestion::TagNames(Scope::Commodity) => user.list_commodity_tag_names().await,
45        Suggestion::TagValues(Scope::Transaction, tag) => {
46            user.list_transaction_tag_values(tag).await
47        }
48        Suggestion::TagValues(Scope::Split, tag) => user.list_split_tag_values(tag).await,
49        Suggestion::TagValues(Scope::Account, tag) => user.list_account_tag_values(tag).await,
50        Suggestion::TagValues(Scope::Commodity, tag) => user.list_commodity_tag_values(tag).await,
51    }
52}