Skip to main content

wekan_cli/commands/
custom_fields.rs

1mod create;
2mod delete;
3mod get;
4mod list;
5mod update;
6use crate::{
7    client::WekanClientFactory, command_result::CommandSuccess, credentials::CredentialStore,
8    error::AppError, input::ConfirmationProvider,
9};
10use clap::{Args, Subcommand};
11#[derive(Debug, Args)]
12pub struct CustomFieldArgs {
13    #[command(subcommand)]
14    pub command: CustomFieldCommand,
15}
16#[derive(Debug, Subcommand)]
17pub enum CustomFieldCommand {
18    /// List board custom-field definitions.
19    List(list::ListArgs),
20    /// Get board custom-field definitions.
21    Get(get::GetArgs),
22    /// Create board custom-field definitions.
23    Create(create::CreateArgs),
24    /// Update board custom-field definitions.
25    Update(update::UpdateArgs),
26    /// Permanently delete board custom-field definitions.
27    Delete(delete::DeleteArgs),
28}
29pub(crate) async fn dispatch(
30    command: CustomFieldCommand,
31    factory: &WekanClientFactory,
32    store: &dyn CredentialStore,
33    confirmation: &dyn ConfirmationProvider,
34) -> Result<CommandSuccess, AppError> {
35    match command {
36        CustomFieldCommand::List(args) => list::execute(args, factory, store).await,
37        CustomFieldCommand::Get(args) => get::execute(args, factory, store).await,
38        CustomFieldCommand::Create(args) => create::execute(args, factory, store).await,
39        CustomFieldCommand::Update(args) => update::execute(args, factory, store).await,
40        CustomFieldCommand::Delete(args) => {
41            delete::execute(args, factory, store, confirmation).await
42        }
43    }
44}
45pub(crate) fn non_empty(value: &str) -> Result<String, String> {
46    let value = crate::client::trim_javascript_whitespace(value);
47    if value.is_empty() {
48        Err("value must not be empty or whitespace".to_owned())
49    } else {
50        crate::client::custom_fields::validate_custom_field_identifier(value)
51            .map_err(str::to_owned)?;
52        Ok(value.to_owned())
53    }
54}
55fn field_type(value: &str) -> Result<crate::client::custom_fields::CustomFieldType, String> {
56    serde_json::from_value(serde_json::Value::String(value.to_owned())).map_err(|_| {
57        "expected text, number, date, dropdown, checkbox, currency or stringtemplate".to_owned()
58    })
59}
60fn settings(value: &str) -> Result<crate::client::custom_fields::CustomFieldSettings, String> {
61    let value: serde_json::Value =
62        serde_json::from_str(value).map_err(|_| "expected a settings JSON object".to_owned())?;
63    if !value.is_object() {
64        return Err("expected a settings JSON object".to_owned());
65    }
66    let settings: crate::client::custom_fields::CustomFieldSettings = serde_json::from_value(value)
67        .map_err(|_| {
68            "expected custom-field settings with only the known, correctly typed properties"
69                .to_owned()
70        })?;
71    settings
72        .validate_dropdown_item_ids()
73        .map_err(str::to_owned)?;
74    Ok(settings)
75}
76pub(crate) fn exact_json(value: &str) -> Result<serde_json::Value, String> {
77    serde_json::from_str(value).map_err(|_| "expected a JSON value".to_owned())
78}
79fn type_name(value: crate::client::custom_fields::CustomFieldType) -> String {
80    use crate::client::custom_fields::CustomFieldType::*;
81    match value {
82        Text => "text",
83        Number => "number",
84        Date => "date",
85        Dropdown => "dropdown",
86        Checkbox => "checkbox",
87        Currency => "currency",
88        Stringtemplate => "stringtemplate",
89    }
90    .to_owned()
91}
92fn detail(
93    value: crate::client::custom_fields::CustomFieldDocument,
94) -> crate::command_result::CustomFieldDetail {
95    use crate::command_result::{CustomFieldDetail, CustomFieldDropdownItem, CustomFieldSettings};
96    CustomFieldDetail {
97        custom_field_id: value.custom_field_id,
98        board_ids: value.board_ids,
99        name: value.name,
100        field_type: type_name(value.field_type),
101        settings: CustomFieldSettings {
102            currency_code: value.settings.currency_code.flatten(),
103            dropdown_items: value.settings.dropdown_items.flatten().map(|items| {
104                items
105                    .into_iter()
106                    .map(|item| CustomFieldDropdownItem {
107                        item_id: item.item_id,
108                        name: item.name,
109                    })
110                    .collect()
111            }),
112            stringtemplate_format: value.settings.stringtemplate_format.flatten(),
113            stringtemplate_separator: value.settings.stringtemplate_separator.flatten(),
114        },
115        show_on_card: value.show_on_card,
116        automatically_on_card: value.automatically_on_card,
117        always_on_card: value.always_on_card,
118        show_label_on_mini_card: value.show_label_on_mini_card,
119        show_sum_at_top_of_list: value.show_sum_at_top_of_list,
120        created_at: value.created_at,
121        modified_at: value.modified_at,
122    }
123}
124
125#[cfg(test)]
126mod tests;