mirror of
https://github.com/pimalaya/himalaya.git
synced 2026-06-17 21:37:55 +08:00
38 lines
1.2 KiB
Rust
38 lines
1.2 KiB
Rust
use anyhow::{bail, Result};
|
|
use clap::Parser;
|
|
use io_imap::coroutines::subscribe::*;
|
|
use io_stream::runtimes::std::handle;
|
|
use pimalaya_toolbox::terminal::printer::{Message, Printer};
|
|
|
|
use crate::imap::{account::ImapAccount, mailbox::arg::MailboxNameArg};
|
|
|
|
/// Subscribe to the given mailbox.
|
|
///
|
|
/// This command subscribes to a mailbox, making it appear in the
|
|
/// list of subscribed mailboxes.
|
|
#[derive(Debug, Parser)]
|
|
pub struct SubscribeMailboxCommand {
|
|
#[command(flatten)]
|
|
pub mailbox: MailboxNameArg,
|
|
}
|
|
|
|
impl SubscribeMailboxCommand {
|
|
pub fn execute(self, printer: &mut impl Printer, account: ImapAccount) -> Result<()> {
|
|
let mut imap = account.new_imap_session()?;
|
|
let mailbox = self.mailbox.name.try_into()?;
|
|
|
|
let mut arg = None;
|
|
let mut coroutine = ImapSubscribe::new(imap.context, mailbox);
|
|
|
|
loop {
|
|
match coroutine.resume(arg.take()) {
|
|
ImapSubscribeResult::Io { io } => arg = Some(handle(&mut imap.stream, io)?),
|
|
ImapSubscribeResult::Ok { .. } => break,
|
|
ImapSubscribeResult::Err { err, .. } => bail!(err),
|
|
}
|
|
}
|
|
|
|
printer.out(Message::new("Mailbox successfully subscribed"))
|
|
}
|
|
}
|