Files
himalaya/src/jmap/mailbox/update.rs
T
Clément DOUIN 6ae09790aa chore: cargo fmt
2026-05-20 23:48:27 +02:00

94 lines
2.8 KiB
Rust

// This file is part of Himalaya, a CLI to manage emails.
//
// Copyright (C) 2022-2026 soywod <pimalaya.org@posteo.net>
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use std::collections::BTreeMap;
use anyhow::{Result, bail};
use clap::Parser;
use io_jmap::rfc8621::{mailbox::MailboxUpdate, mailbox_set::JmapMailboxSetArgs};
use pimalaya_cli::printer::{Message, Printer};
use crate::jmap::{client::JmapClient, error::format_set_error, mailbox::query::RoleArg};
/// Update a JMAP mailbox.
#[derive(Debug, Parser)]
pub struct JmapMailboxUpdateCommand {
/// The ID of the mailbox to update.
#[arg(value_name = "ID")]
pub id: String,
/// New display name.
#[arg(long)]
pub name: Option<String>,
/// New parent mailbox ID.
#[arg(long, value_name = "ID")]
pub parent_id: Option<String>,
/// New role.
#[arg(long, value_name = "ROLE")]
pub role: Option<RoleArg>,
/// New sort order.
#[arg(long, value_name = "N")]
pub sort_order: Option<u32>,
/// Subscribe to the mailbox.
#[arg(long, conflicts_with = "unsubscribe")]
pub subscribe: bool,
/// Unsubscribe from the mailbox.
#[arg(long, conflicts_with = "subscribe")]
pub unsubscribe: bool,
}
impl JmapMailboxUpdateCommand {
pub fn execute(self, printer: &mut impl Printer, mut client: JmapClient) -> Result<()> {
let is_subscribed = if self.subscribe {
Some(true)
} else if self.unsubscribe {
Some(false)
} else {
None
};
let patch = MailboxUpdate {
name: self.name,
parent_id: self.parent_id,
role: self.role.map(Into::into),
sort_order: self.sort_order,
is_subscribed,
};
let mut update = BTreeMap::new();
update.insert(self.id.clone(), patch);
let mut args = JmapMailboxSetArgs::default();
args.update = Some(update);
let output = client.mailbox_set(args)?;
if let Some(err) = output.not_updated.get(&self.id) {
let mut msg = format!("Update JMAP mailbox `{}` error", self.id);
msg.push_str(&format_set_error(err));
bail!(msg);
}
printer.out(Message::new("Mailbox successfully updated"))
}
}