remove display cell trait

This commit is contained in:
Clément DOUIN 2021-01-16 15:39:26 +01:00
parent 04642859e8
commit c3d5559234
No known key found for this signature in database
GPG key ID: 69C9B9CFFDEE2DEF
3 changed files with 17 additions and 107 deletions

View file

@ -85,7 +85,7 @@ impl<'a> ImapConnector<'a> {
}
}
pub fn list_mboxes(&mut self) -> Result<Vec<Mbox<'_>>> {
pub fn list_mboxes(&mut self) -> Result<Vec<Mbox>> {
let mboxes = self
.sess
.list(Some(""), Some("*"))?

View file

@ -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<String>,
}
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<imap::types::NameAttribute<'a>>);
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::<Vec<_>>()
.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<table::Cell> {
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<Mbox<'a>> {
impl<'a> DisplayTable<'a, Mbox> for Vec<Mbox> {
fn cols() -> &'a [&'a str] {
&["delim", "name", "attributes"]
}
fn rows(&self) -> &Vec<Mbox<'a>> {
fn rows(&self) -> &Vec<Mbox> {
self
}
}

View file

@ -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<Cell>;
}