From 00e25246409b3eaff31933128a82f31c52d22b63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Sat, 26 Feb 2022 00:13:14 +0100 Subject: [PATCH] implement notmuch get_mboxes --- src/backends/notmuch/notmuch_backend.rs | 11 +++- src/backends/notmuch/notmuch_mbox.rs | 80 +++++++++++++++++++++++++ src/lib.rs | 3 + 3 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 src/backends/notmuch/notmuch_mbox.rs diff --git a/src/backends/notmuch/notmuch_backend.rs b/src/backends/notmuch/notmuch_backend.rs index dc8306a..dbfd4c2 100644 --- a/src/backends/notmuch/notmuch_backend.rs +++ b/src/backends/notmuch/notmuch_backend.rs @@ -3,7 +3,7 @@ use std::{convert::TryInto, fs}; use anyhow::{anyhow, Context, Result}; use crate::{ - backends::{Backend, NotmuchEnvelopes}, + backends::{Backend, NotmuchEnvelopes, NotmuchMbox, NotmuchMboxes}, config::{AccountConfig, NotmuchBackendConfig}, mbox::Mboxes, msg::{Envelopes, Msg}, @@ -39,7 +39,14 @@ impl<'a> Backend<'a> for NotmuchBackend<'a> { } fn get_mboxes(&mut self) -> Result> { - unimplemented!(); + let mut mboxes: Vec<_> = self + .account_config + .mailboxes + .iter() + .map(|(k, v)| NotmuchMbox::new(k, v)) + .collect(); + mboxes.sort_by(|a, b| b.name.partial_cmp(&a.name).unwrap()); + Ok(Box::new(NotmuchMboxes(mboxes))) } fn del_mbox(&mut self, _mbox: &str) -> Result<()> { diff --git a/src/backends/notmuch/notmuch_mbox.rs b/src/backends/notmuch/notmuch_mbox.rs new file mode 100644 index 0000000..6cde8b5 --- /dev/null +++ b/src/backends/notmuch/notmuch_mbox.rs @@ -0,0 +1,80 @@ +//! Notmuch mailbox module. +//! +//! This module provides Notmuch types and conversion utilities +//! related to the mailbox + +use anyhow::Result; +use std::{ + fmt::{self, Display}, + ops::Deref, +}; + +use crate::{ + mbox::Mboxes, + output::{PrintTable, PrintTableOpts, WriteColor}, + ui::{Cell, Row, Table}, +}; + +/// Represents a list of Notmuch mailboxes. +#[derive(Debug, Default, serde::Serialize)] +pub struct NotmuchMboxes(pub Vec); + +impl Deref for NotmuchMboxes { + type Target = Vec; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl PrintTable for NotmuchMboxes { + fn print_table(&self, writter: &mut dyn WriteColor, opts: PrintTableOpts) -> Result<()> { + writeln!(writter)?; + Table::print(writter, self, opts)?; + writeln!(writter)?; + Ok(()) + } +} + +impl Mboxes for NotmuchMboxes { + // +} + +/// Represents the notmuch virtual mailbox. +#[derive(Debug, Default, PartialEq, Eq, serde::Serialize)] +pub struct NotmuchMbox { + /// Represents the virtual mailbox name. + pub name: String, + + /// Represents the query associated to the virtual mailbox name. + pub query: String, +} + +impl NotmuchMbox { + pub fn new(name: &str, query: &str) -> Self { + Self { + name: name.into(), + query: query.into(), + } + } +} + +impl Display for NotmuchMbox { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.name) + } +} + +impl Table for NotmuchMbox { + fn head() -> Row { + Row::new() + .cell(Cell::new("NAME").bold().underline().white()) + .cell(Cell::new("QUERY").bold().underline().white()) + } + + fn row(&self) -> Row { + Row::new() + .cell(Cell::new(&self.name).white()) + .cell(Cell::new(&self.query).green()) + } +} diff --git a/src/lib.rs b/src/lib.rs index 898f536..5e9a09a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -81,6 +81,9 @@ pub mod backends { pub mod notmuch_backend; pub use notmuch_backend::*; + pub mod notmuch_mbox; + pub use notmuch_mbox::*; + pub mod notmuch_envelope; pub use notmuch_envelope::*; }