clean process and account modules (#340)

This commit is contained in:
Clément DOUIN
2022-06-27 01:13:55 +02:00
parent a5c4fdaac6
commit c0e002ea1b
27 changed files with 251 additions and 216 deletions
+4 -4
View File
@@ -3,7 +3,7 @@
//! This module gathers all account actions triggered by the CLI.
use anyhow::Result;
use himalaya_lib::account::{AccountConfig, DeserializedConfig};
use himalaya_lib::account::{Account, DeserializedConfig};
use log::{info, trace};
use crate::{
@@ -15,7 +15,7 @@ use crate::{
pub fn list<'a, P: PrinterService>(
max_width: Option<usize>,
config: &DeserializedConfig,
account_config: &AccountConfig,
account_config: &Account,
printer: &mut P,
) -> Result<()> {
info!(">> account list handler");
@@ -38,7 +38,7 @@ pub fn list<'a, P: PrinterService>(
#[cfg(test)]
mod tests {
use himalaya_lib::account::{
AccountConfig, DeserializedAccountConfig, DeserializedConfig, DeserializedImapAccountConfig,
Account, DeserializedAccountConfig, DeserializedConfig, DeserializedImapAccountConfig,
};
use std::{collections::HashMap, fmt::Debug, io, iter::FromIterator};
use termcolor::ColorSpec;
@@ -122,7 +122,7 @@ mod tests {
..DeserializedConfig::default()
};
let account_config = AccountConfig::default();
let account_config = Account::default();
let mut printer = PrinterServiceTest::default();
assert!(list(None, &config, &account_config, &mut printer).is_ok());
+3 -3
View File
@@ -1,6 +1,6 @@
use anyhow::{Context, Result};
use himalaya_lib::{
account::{AccountConfig, BackendConfig, DeserializedConfig, DEFAULT_INBOX_FOLDER},
account::{Account, BackendConfig, DeserializedConfig, DEFAULT_INBOX_FOLDER},
backend::Backend,
};
use std::{convert::TryFrom, env};
@@ -58,7 +58,7 @@ fn main() -> Result<()> {
if raw_args.len() > 1 && raw_args[1].starts_with("mailto:") {
let config = DeserializedConfig::from_opt_path(None)?;
let (account_config, backend_config) =
AccountConfig::from_config_and_opt_account_name(&config, None)?;
Account::from_config_and_opt_account_name(&config, None)?;
let mut printer = StdoutPrinter::from(OutputFmt::Plain);
let url = Url::parse(&raw_args[1])?;
let mut smtp = LettreService::from(&account_config);
@@ -114,7 +114,7 @@ fn main() -> Result<()> {
// Init entities and services.
let config = DeserializedConfig::from_opt_path(m.value_of("config"))?;
let (account_config, backend_config) =
AccountConfig::from_config_and_opt_account_name(&config, m.value_of("account"))?;
Account::from_config_and_opt_account_name(&config, m.value_of("account"))?;
let mbox = m
.value_of("mbox-source")
.or_else(|| account_config.mailboxes.get("inbox").map(|s| s.as_str()))
+3 -3
View File
@@ -3,7 +3,7 @@
//! This module gathers all mailbox actions triggered by the CLI.
use anyhow::Result;
use himalaya_lib::{account::AccountConfig, backend::Backend};
use himalaya_lib::{account::Account, backend::Backend};
use log::{info, trace};
use crate::output::{PrintTableOpts, PrinterService};
@@ -11,7 +11,7 @@ use crate::output::{PrintTableOpts, PrinterService};
/// Lists all mailboxes.
pub fn list<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
max_width: Option<usize>,
config: &AccountConfig,
config: &Account,
printer: &mut P,
backend: Box<&'a mut B>,
) -> Result<()> {
@@ -170,7 +170,7 @@ mod tests {
}
}
let config = AccountConfig::default();
let config = Account::default();
let mut printer = PrinterServiceTest::default();
let mut backend = TestBackend {};
let backend = Box::new(&mut backend);
+11 -11
View File
@@ -5,7 +5,7 @@
use anyhow::{Context, Result};
use atty::Stream;
use himalaya_lib::{
account::{AccountConfig, DEFAULT_SENT_FOLDER},
account::{Account, DEFAULT_SENT_FOLDER},
backend::Backend,
msg::{Msg, Part, Parts, TextPlainPart, TplOverride},
};
@@ -28,7 +28,7 @@ use crate::{
pub fn attachments<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
seq: &str,
mbox: &str,
config: &AccountConfig,
config: &Account,
printer: &mut P,
backend: Box<&'a mut B>,
) -> Result<()> {
@@ -92,7 +92,7 @@ pub fn forward<'a, P: PrinterService, B: Backend<'a> + ?Sized, S: SmtpService>(
attachments_paths: Vec<&str>,
encrypt: bool,
mbox: &str,
config: &AccountConfig,
config: &Account,
printer: &mut P,
backend: Box<&'a mut B>,
smtp: &mut S,
@@ -112,7 +112,7 @@ pub fn list<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
page_size: Option<usize>,
page: usize,
mbox: &str,
config: &AccountConfig,
config: &Account,
printer: &mut P,
imap: Box<&'a mut B>,
) -> Result<()> {
@@ -134,7 +134,7 @@ pub fn list<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
/// [mailto]: https://en.wikipedia.org/wiki/Mailto
pub fn mailto<'a, P: PrinterService, B: Backend<'a> + ?Sized, S: SmtpService>(
url: &Url,
config: &AccountConfig,
config: &Account,
printer: &mut P,
backend: Box<&'a mut B>,
smtp: &mut S,
@@ -212,7 +212,7 @@ pub fn read<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
raw: bool,
headers: Vec<&str>,
mbox: &str,
config: &AccountConfig,
config: &Account,
printer: &mut P,
backend: Box<&'a mut B>,
) -> Result<()> {
@@ -233,7 +233,7 @@ pub fn reply<'a, P: PrinterService, B: Backend<'a> + ?Sized, S: SmtpService>(
attachments_paths: Vec<&str>,
encrypt: bool,
mbox: &str,
config: &AccountConfig,
config: &Account,
printer: &mut P,
backend: Box<&'a mut B>,
smtp: &mut S,
@@ -285,7 +285,7 @@ pub fn search<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
page_size: Option<usize>,
page: usize,
mbox: &str,
config: &AccountConfig,
config: &Account,
printer: &mut P,
backend: Box<&'a mut B>,
) -> Result<()> {
@@ -310,7 +310,7 @@ pub fn sort<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
page_size: Option<usize>,
page: usize,
mbox: &str,
config: &AccountConfig,
config: &Account,
printer: &mut P,
backend: Box<&'a mut B>,
) -> Result<()> {
@@ -330,7 +330,7 @@ pub fn sort<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
/// Send a raw message.
pub fn send<'a, P: PrinterService, B: Backend<'a> + ?Sized, S: SmtpService>(
raw_msg: &str,
config: &AccountConfig,
config: &Account,
printer: &mut P,
backend: Box<&mut B>,
smtp: &mut S,
@@ -371,7 +371,7 @@ pub fn write<'a, P: PrinterService, B: Backend<'a> + ?Sized, S: SmtpService>(
tpl: TplOverride,
attachments_paths: Vec<&str>,
encrypt: bool,
config: &AccountConfig,
config: &Account,
printer: &mut P,
backend: Box<&'a mut B>,
smtp: &mut S,
+6 -6
View File
@@ -5,7 +5,7 @@
use anyhow::Result;
use atty::Stream;
use himalaya_lib::{
account::AccountConfig,
account::Account,
backend::Backend,
msg::{Msg, TplOverride},
};
@@ -16,7 +16,7 @@ use crate::{output::PrinterService, smtp::SmtpService};
/// Generate a new message template.
pub fn new<'a, P: PrinterService>(
opts: TplOverride<'a>,
account: &'a AccountConfig,
account: &'a Account,
printer: &'a mut P,
) -> Result<()> {
let tpl = Msg::default().to_tpl(opts, account)?;
@@ -29,7 +29,7 @@ pub fn reply<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
all: bool,
opts: TplOverride<'a>,
mbox: &str,
config: &'a AccountConfig,
config: &'a Account,
printer: &'a mut P,
backend: Box<&'a mut B>,
) -> Result<()> {
@@ -45,7 +45,7 @@ pub fn forward<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
seq: &str,
opts: TplOverride<'a>,
mbox: &str,
config: &'a AccountConfig,
config: &'a Account,
printer: &'a mut P,
backend: Box<&'a mut B>,
) -> Result<()> {
@@ -59,7 +59,7 @@ pub fn forward<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
/// Saves a message based on a template.
pub fn save<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
mbox: &str,
config: &AccountConfig,
config: &Account,
attachments_paths: Vec<&str>,
tpl: &str,
printer: &mut P,
@@ -84,7 +84,7 @@ pub fn save<'a, P: PrinterService, B: Backend<'a> + ?Sized>(
/// Sends a message based on a template.
pub fn send<'a, P: PrinterService, B: Backend<'a> + ?Sized, S: SmtpService>(
mbox: &str,
account: &AccountConfig,
account: &Account,
attachments_paths: Vec<&str>,
tpl: &str,
printer: &mut P,
+2 -2
View File
@@ -1,5 +1,5 @@
use anyhow::Result;
use himalaya_lib::account::Format;
use himalaya_lib::account::TextPlainFormat;
use std::io;
use termcolor::{self, StandardStream};
@@ -12,6 +12,6 @@ pub trait PrintTable {
}
pub struct PrintTableOpts<'a> {
pub format: &'a Format,
pub format: &'a TextPlainFormat,
pub max_width: Option<usize>,
}
+6 -6
View File
@@ -1,5 +1,5 @@
use anyhow::{Context, Result};
use himalaya_lib::{account::AccountConfig, msg::Msg};
use himalaya_lib::{account::Account, msg::Msg};
use lettre::{
self,
transport::smtp::{
@@ -13,11 +13,11 @@ use std::convert::TryInto;
use crate::output::pipe_cmd;
pub trait SmtpService {
fn send(&mut self, account: &AccountConfig, msg: &Msg) -> Result<Vec<u8>>;
fn send(&mut self, account: &Account, msg: &Msg) -> Result<Vec<u8>>;
}
pub struct LettreService<'a> {
account: &'a AccountConfig,
account: &'a Account,
transport: Option<SmtpTransport>,
}
@@ -56,7 +56,7 @@ impl LettreService<'_> {
}
impl SmtpService for LettreService<'_> {
fn send(&mut self, account: &AccountConfig, msg: &Msg) -> Result<Vec<u8>> {
fn send(&mut self, account: &Account, msg: &Msg) -> Result<Vec<u8>> {
let mut raw_msg = msg.into_sendable_msg(account)?.formatted();
let envelope: lettre::address::Envelope =
@@ -76,8 +76,8 @@ impl SmtpService for LettreService<'_> {
}
}
impl<'a> From<&'a AccountConfig> for LettreService<'a> {
fn from(account: &'a AccountConfig) -> Self {
impl<'a> From<&'a Account> for LettreService<'a> {
fn from(account: &'a Account) -> Self {
Self {
account,
transport: None,
+3 -3
View File
@@ -1,6 +1,6 @@
use anyhow::{Context, Result};
use himalaya_lib::{
account::{AccountConfig, DEFAULT_DRAFT_FOLDER, DEFAULT_SENT_FOLDER},
account::{Account, DEFAULT_DRAFT_FOLDER, DEFAULT_SENT_FOLDER},
backend::Backend,
msg::{local_draft_path, remove_local_draft, Msg, TplOverride},
};
@@ -39,7 +39,7 @@ pub fn open_with_draft() -> Result<String> {
open_with_tpl(tpl)
}
fn _edit_msg_with_editor(msg: &Msg, tpl: TplOverride, account: &AccountConfig) -> Result<Msg> {
fn _edit_msg_with_editor(msg: &Msg, tpl: TplOverride, account: &Account) -> Result<Msg> {
let tpl = msg.to_tpl(tpl, account)?;
let tpl = open_with_tpl(tpl)?;
Msg::from_tpl(&tpl).context("cannot parse message from template")
@@ -48,7 +48,7 @@ fn _edit_msg_with_editor(msg: &Msg, tpl: TplOverride, account: &AccountConfig) -
pub fn edit_msg_with_editor<'a, P: PrinterService, B: Backend<'a> + ?Sized, S: SmtpService>(
mut msg: Msg,
tpl: TplOverride,
account: &AccountConfig,
account: &Account,
printer: &mut P,
backend: Box<&'a mut B>,
smtp: &mut S,
+5 -5
View File
@@ -5,7 +5,7 @@
//! [builder design pattern]: https://refactoring.guru/design-patterns/builder
use anyhow::{Context, Result};
use himalaya_lib::account::Format;
use himalaya_lib::account::TextPlainFormat;
use log::trace;
use termcolor::{Color, ColorSpec};
use terminal_size;
@@ -169,11 +169,11 @@ where
/// Writes the table to the writer.
fn print(writer: &mut dyn WriteColor, items: &[Self], opts: PrintTableOpts) -> Result<()> {
let is_format_flowed = matches!(opts.format, Format::Flowed);
let is_format_flowed = matches!(opts.format, TextPlainFormat::Flowed);
let max_width = match opts.format {
Format::Fixed(width) => opts.max_width.unwrap_or(*width),
Format::Flowed => 0,
Format::Auto => opts
TextPlainFormat::Fixed(width) => opts.max_width.unwrap_or(*width),
TextPlainFormat::Flowed => 0,
TextPlainFormat::Auto => opts
.max_width
.or_else(|| terminal_size::terminal_size().map(|(w, _)| w.0 as usize))
.unwrap_or(DEFAULT_TERM_WIDTH),