diff --git a/src/imap.rs b/src/imap.rs index 6a4f741..3acfa97 100644 --- a/src/imap.rs +++ b/src/imap.rs @@ -85,7 +85,7 @@ impl<'a> ImapConnector<'a> { } } - pub fn list_mboxes(&mut self) -> Result>> { + pub fn list_mboxes(&mut self) -> Result> { let mboxes = self .sess .list(Some(""), Some("*"))? diff --git a/src/mbox.rs b/src/mbox.rs index dd34543..ef01450 100644 --- a/src/mbox.rs +++ b/src/mbox.rs @@ -1,120 +1,39 @@ use imap; -use crate::table::{self, DisplayCell, DisplayRow, DisplayTable}; +use crate::table::{self, DisplayRow, DisplayTable}; -pub struct Delim(String); - -impl Delim { - pub fn from_name(name: &imap::types::Name) -> Self { - Self(name.delimiter().unwrap_or("/").to_string()) - } +pub struct Mbox { + pub delim: String, + pub name: String, + pub attributes: Vec, } -impl DisplayCell for Delim { - fn styles(&self) -> &[table::Style] { - &[table::BLUE] - } - - fn value(&self) -> String { - self.0.to_owned() - } -} - -pub struct Name(String); - -impl Name { - pub fn from_name(name: &imap::types::Name) -> Self { - Self(name.name().to_string()) - } -} - -impl DisplayCell for Name { - fn styles(&self) -> &[table::Style] { - &[table::GREEN] - } - - fn value(&self) -> String { - self.0.to_owned() - } -} - -pub struct Attributes<'a>(Vec>); - -impl Attributes<'_> { - pub fn from_name(name: &imap::types::Name) -> Self { - let attrs = name.attributes().iter().fold(vec![], |mut attrs, attr| { - use imap::types::NameAttribute::*; - - match attr { - NoInferiors => attrs.push(NoInferiors), - NoSelect => attrs.push(NoSelect), - Marked => attrs.push(Marked), - Unmarked => attrs.push(Unmarked), - _ => (), - }; - - attrs - }); - - Self(attrs) - } -} - -impl DisplayCell for Attributes<'_> { - fn styles(&self) -> &[table::Style] { - &[table::YELLOW] - } - - fn value(&self) -> String { - use imap::types::NameAttribute::*; - - self.0 - .iter() - .map(|attr| match attr { - NoInferiors => vec!["no inferiors"], - NoSelect => vec!["no select"], - Marked => vec!["marked"], - Unmarked => vec!["unmarked"], - _ => vec![], - }) - .collect::>() - .concat() - .join(", ") - } -} - -pub struct Mbox<'a> { - pub delim: Delim, - pub name: Name, - pub attributes: Attributes<'a>, -} - -impl Mbox<'_> { +impl Mbox { pub fn from_name(name: &imap::types::Name) -> Self { Self { - delim: Delim::from_name(name), - name: Name::from_name(name), - attributes: Attributes::from_name(name), + delim: name.delimiter().unwrap_or_default().to_owned(), + name: name.name().to_owned(), + attributes: vec![], // TODO: set attributes } } } -impl<'a> DisplayRow for Mbox<'a> { +impl DisplayRow for Mbox { fn to_row(&self) -> Vec { vec![ - self.delim.to_cell(), - self.name.to_cell(), - self.attributes.to_cell(), + table::Cell::new(&[table::BLUE], &self.delim), + table::Cell::new(&[table::GREEN], &self.name), + table::Cell::new(&[table::YELLOW], &self.attributes.join(", ")), ] } } -impl<'a> DisplayTable<'a, Mbox<'a>> for Vec> { +impl<'a> DisplayTable<'a, Mbox> for Vec { fn cols() -> &'a [&'a str] { &["delim", "name", "attributes"] } - fn rows(&self) -> &Vec> { + fn rows(&self) -> &Vec { self } } diff --git a/src/table.rs b/src/table.rs index 79154fa..e247d04 100644 --- a/src/table.rs +++ b/src/table.rs @@ -36,7 +36,7 @@ pub struct Cell { } impl Cell { - pub fn new<'a>(styles: &'a [Style], value: &'a str) -> Cell { + pub fn new(styles: &[Style], value: &str) -> Cell { Cell { styles: styles.to_vec(), value: value.to_string(), @@ -65,15 +65,6 @@ impl Cell { } } -pub trait DisplayCell { - fn styles(&self) -> &[Style]; - fn value(&self) -> String; - - fn to_cell(&self) -> Cell { - Cell::new(self.styles(), &self.value()) - } -} - pub trait DisplayRow { fn to_row(&self) -> Vec; }