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

74 lines
2.4 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 anyhow::Result;
use clap::Parser;
use io_imap::types::{
IntoStatic,
flag::{Flag, StoreType},
};
use pimalaya_cli::printer::{Message, Printer};
use crate::imap::{
client::ImapClient,
mailbox::arg::{MailboxNameOptionalFlag, MailboxNoSelectFlag},
};
/// Add IMAP flag(s) to message(s).
///
/// This command adds the given flags to messages identified by the
/// given sequence set.
#[derive(Debug, Parser)]
pub struct ImapFlagAddCommand {
#[command(flatten)]
pub mailbox_name: MailboxNameOptionalFlag,
#[command(flatten)]
pub mailbox_no_select: MailboxNoSelectFlag,
/// The sequence set of messages (e.g., "1", "1,2,3", "1:*").
#[arg(value_name = "SEQUENCE")]
pub sequence_set: String,
/// The flags to add (e.g., "\\Seen", "\\Flagged").
#[arg(short, long, required = true, num_args = 1..)]
pub flag: Vec<String>,
/// Use sequence numbers instead of UIDs.
#[arg(long)]
pub seq: bool,
}
impl ImapFlagAddCommand {
pub fn execute(self, printer: &mut impl Printer, mut client: ImapClient) -> Result<()> {
let mailbox = self.mailbox_name.inner.try_into()?;
if !self.mailbox_no_select.inner {
client.select(mailbox)?;
}
let sequence_set = self.sequence_set.as_str().try_into()?;
let flags: Vec<Flag<'static>> = self
.flag
.iter()
.map(|f| Flag::try_from(f.as_str()).map(|flag| flag.into_static()))
.collect::<Result<_, _>>()?;
client.store(sequence_set, StoreType::Add, flags, !self.seq)?;
printer.out(Message::new("Flag(s) successfully added"))
}
}