Lines
100 %
Functions
Branches
//! The free-text suggestions of a text cell, shared by every client.
use forms::{Scope, Suggest};
use server::error::ServerError;
use server::user::User;
use uuid::Uuid;
/// What a text cell asks to be suggested.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Suggestion {
TagNames(Scope),
/// The values of the tag of this scope with this name.
TagValues(Scope, String),
}
impl Suggestion {
/// The request `suggest` makes; a values source needs the tag name and a
/// names source takes none.
///
/// # Errors
/// The message naming the misplaced or missing tag name.
pub fn of(suggest: Suggest, tag: Option<&str>) -> Result<Self, String> {
match (suggest, tag) {
(Suggest::TagNames(scope), None) => Ok(Self::TagNames(scope)),
(Suggest::TagValues(scope), Some(tag)) => Ok(Self::TagValues(scope, tag.to_string())),
(Suggest::TagNames(_), Some(_)) => Err(format!("{} takes no tag name", suggest.name())),
(Suggest::TagValues(_), None) => {
Err(format!("{0} needs '{0}:<tag name>'", suggest.name()))
/// The user's strings for `request`.
/// The server's failure to list them.
pub async fn suggestions(user_id: Uuid, request: &Suggestion) -> Result<Vec<String>, ServerError> {
let user = User { id: user_id };
match request {
Suggestion::TagNames(Scope::Transaction) => user.list_transaction_tag_names().await,
Suggestion::TagNames(Scope::Split) => user.list_split_tag_names().await,
Suggestion::TagNames(Scope::Account) => user.list_account_tag_names().await,
Suggestion::TagNames(Scope::Commodity) => user.list_commodity_tag_names().await,
Suggestion::TagValues(Scope::Transaction, tag) => {
user.list_transaction_tag_values(tag).await
Suggestion::TagValues(Scope::Split, tag) => user.list_split_tag_values(tag).await,
Suggestion::TagValues(Scope::Account, tag) => user.list_account_tag_values(tag).await,
Suggestion::TagValues(Scope::Commodity, tag) => user.list_commodity_tag_values(tag).await,