Skip to main content

wekan_cli/commands/
cards.rs

1mod archive;
2mod create;
3mod delete;
4mod get;
5mod list;
6mod restore;
7mod update;
8
9#[cfg(test)]
10mod tests;
11
12use clap::{Args, Subcommand};
13
14use crate::{
15    client::WekanClientFactory, command_result::CommandSuccess, credentials::CredentialStore,
16    error::AppError, input::ConfirmationProvider,
17};
18
19#[derive(Debug, Args)]
20pub struct CardArgs {
21    #[command(subcommand)]
22    pub command: CardCommand,
23}
24
25#[derive(Debug, Subcommand)]
26pub enum CardCommand {
27    /// List the cards returned by Wekan for a board list.
28    List(list::ListArgs),
29
30    /// Show one card by board, list, and card ID.
31    Get(get::GetArgs),
32
33    /// Create a card in a board list.
34    Create(Box<create::CreateArgs>),
35
36    /// Update one or more editable card fields.
37    Update(Box<update::UpdateArgs>),
38
39    /// Archive an active card and its child cards.
40    Archive(archive::ArchiveArgs),
41
42    /// Restore an archived card and its child cards.
43    Restore(restore::RestoreArgs),
44
45    /// Permanently delete a card and its child resources.
46    Delete(delete::DeleteArgs),
47}
48
49pub(crate) async fn dispatch(
50    command: CardCommand,
51    client_factory: &WekanClientFactory,
52    credential_store: &dyn CredentialStore,
53    confirmation: &dyn ConfirmationProvider,
54) -> Result<CommandSuccess, AppError> {
55    match command {
56        CardCommand::List(args) => list::execute(args, client_factory, credential_store).await,
57        CardCommand::Get(args) => get::execute(args, client_factory, credential_store).await,
58        CardCommand::Create(args) => create::execute(*args, client_factory, credential_store).await,
59        CardCommand::Update(args) => update::execute(*args, client_factory, credential_store).await,
60        CardCommand::Archive(args) => {
61            archive::execute(args, client_factory, credential_store).await
62        }
63        CardCommand::Restore(args) => {
64            restore::execute(args, client_factory, credential_store).await
65        }
66        CardCommand::Delete(args) => {
67            delete::execute(args, client_factory, credential_store, confirmation).await
68        }
69    }
70}
71
72fn non_empty(value: &str) -> Result<String, String> {
73    trimmed_non_empty(value)
74}
75
76fn trimmed_non_empty(value: &str) -> Result<String, String> {
77    let value = value.trim();
78    if value.is_empty() {
79        Err("value must not be empty or whitespace".to_owned())
80    } else {
81        Ok(value.to_owned())
82    }
83}
84
85fn date_input(value: &str) -> Result<crate::client::cards::CardDateInput, String> {
86    use crate::client::cards::CardDateInput;
87    Ok(match value.trim().parse::<serde_json::Number>() {
88        Ok(number) => CardDateInput::EpochMilliseconds(number),
89        Err(_) => CardDateInput::Text(value.to_owned()),
90    })
91}