diff options
Diffstat (limited to 'mail/examples/send_mail')
-rw-r--r-- | mail/examples/send_mail/cli.rs | 48 | ||||
-rw-r--r-- | mail/examples/send_mail/main.rs | 50 |
2 files changed, 44 insertions, 54 deletions
diff --git a/mail/examples/send_mail/cli.rs b/mail/examples/send_mail/cli.rs index e2c14d6..535cfe9 100644 --- a/mail/examples/send_mail/cli.rs +++ b/mail/examples/send_mail/cli.rs @@ -1,28 +1,22 @@ -use std::io::{ - self, - StdinLock, StdoutLock, - BufReader, BufRead, - Write -}; +use std::io::{self, BufRead, BufReader, StdinLock, StdoutLock, Write}; use std::str::FromStr; use rpassword::read_password_with_reader; - use mail::{ - Mailbox, Email, HeaderTryFrom, + header_components::{Phrase, Unstructured}, smtp::misc::Domain, - header_components::{Phrase, Unstructured} + Email, HeaderTryFrom, Mailbox, }; - pub struct ClDialog<'a> { stdin: BufReader<StdinLock<'a>>, - stdout: StdoutLock<'a> + stdout: StdoutLock<'a>, } pub fn with_dialog<FN, R>(func: FN) -> R - where FN: for<'a> FnOnce(ClDialog<'a>) -> R +where + FN: for<'a> FnOnce(ClDialog<'a>) -> R, { let stdin = io::stdin(); let stdout = io::stdout(); @@ -31,9 +25,11 @@ pub fn with_dialog<FN, R>(func: FN) -> R } impl<'a> ClDialog<'a> { - pub fn new(stdin: StdinLock<'a>, stdout: StdoutLock<'a>) -> Self { - ClDialog { stdin: BufReader::new(stdin), stdout } + ClDialog { + stdin: BufReader::new(stdin), + stdout, + } } pub fn stdout(&mut self) -> &mut StdoutLock<'a> { @@ -84,7 +80,7 @@ impl<'a> ClDialog<'a> { self.prompt("- Email Address")?; let email = self.read_email()?; self.prompt("- Display Name")?; - let display_name = self.read_opt_phrase()?; + let display_name = self.read_opt_phrase()?; Ok(Mailbox::from((display_name, email))) } @@ -92,7 +88,6 @@ impl<'a> ClDialog<'a> { read_password_with_reader(Some(&mut self.stdin)) } - pub fn read_mail_text_body(&mut self) -> Result<String, io::Error> { let mut buf = String::new(); while self.stdin.read_line(&mut buf)? != 0 { @@ -156,9 +151,7 @@ impl<'a> ClDialog<'a> { writeln!(self.stdout, "To/Recipient")?; let to = self.read_mailbox()?; self.prompt("Subject")?; - let subject = Unstructured - ::try_from(self.read_line()?.trim()) - .unwrap(); + let subject = Unstructured::try_from(self.read_line()?.trim()).unwrap(); writeln!(self.stdout, "Utf-8 text body [end with Ctrl-D]:")?; self.stdout.flush()?; @@ -167,7 +160,7 @@ impl<'a> ClDialog<'a> { from, to, subject, - text_body + text_body, }) } @@ -175,18 +168,16 @@ impl<'a> ClDialog<'a> { loop { let line = self.read_line()?; let line = line.trim(); - let valid = - match line { - "y" => true, - "n" => false, - _ => continue - }; + let valid = match line { + "y" => true, + "n" => false, + _ => continue, + }; return Ok(valid); } } } - /// POD #[derive(Debug)] pub struct AuthData { @@ -198,7 +189,7 @@ pub struct AuthData { #[derive(Debug)] pub struct MsaInfo { pub domain: Domain, - pub auth: AuthData + pub auth: AuthData, } /// POD @@ -209,4 +200,3 @@ pub struct SimpleMail { pub subject: Unstructured, pub text_body: String, } - diff --git a/mail/examples/send_mail/main.rs b/mail/examples/send_mail/main.rs index bccfde9..a9303ec 100644 --- a/mail/examples/send_mail/main.rs +++ b/mail/examples/send_mail/main.rs @@ -6,30 +6,25 @@ compile_error!("example `send_mail` requires feature `smtp`"); #[macro_use] extern crate mail; -extern crate rpassword; extern crate futures; -extern crate tokio; +extern crate rpassword; extern crate soft_ascii_string; +extern crate tokio; use std::io::{self, Write}; -use futures::{Stream, future}; +use futures::{future, Stream}; use soft_ascii_string::SoftAsciiString; use mail::{ - Mail, - HeaderTryFrom, - smtp::{ - send_batch as send_mail_batch, - MailRequest, - ConnectionConfig, - ConnectionBuilder, - auth::{Plain as AuthPlain} - }, - error::MailError, default_impl::simple_context, + error::MailError, header_components::Domain, - Context + smtp::{ + auth::Plain as AuthPlain, send_batch as send_mail_batch, ConnectionBuilder, + ConnectionConfig, MailRequest, + }, + Context, HeaderTryFrom, Mail, }; mod cli; @@ -53,7 +48,7 @@ fn main() { .for_each(|res| { match res { Ok(_) => println!("[mail send]"), - Err(err) => println!("[sending mail failed] {:?}", err) + Err(err) => println!("[sending mail failed] {:?}", err), } Ok(()) }) @@ -67,20 +62,28 @@ fn create_connection_config(msa_info: cli::MsaInfo) -> ConnectionConfig<AuthPlai ConnectionBuilder::new(domain) .expect("could not resolve domain/host name of MSA") - .auth(AuthPlain::from_username(auth.username, auth.password) - .expect("used \\0 in username or password")) + .auth( + AuthPlain::from_username(auth.username, auth.password) + .expect("used \\0 in username or password"), + ) .build() } -fn create_mail_requests(mails: Vec<cli::SimpleMail>, ctx: &impl Context) - -> Result<Vec<MailRequest>, MailError> -{ +fn create_mail_requests( + mails: Vec<cli::SimpleMail>, + ctx: &impl Context, +) -> Result<Vec<MailRequest>, MailError> { use mail::headers::*; let requests = mails .into_iter() .map(|simple_mail| { - let cli::SimpleMail { from, to, subject, text_body } = simple_mail; + let cli::SimpleMail { + from, + to, + subject, + text_body, + } = simple_mail; let mut mail = Mail::plain_text(text_body, ctx); mail.insert_headers(headers! { _From: [from], @@ -90,13 +93,11 @@ fn create_mail_requests(mails: Vec<cli::SimpleMail>, ctx: &impl Context) Ok(MailRequest::from(mail)) }) - .collect::<Result<Vec<_>,_>>(); + .collect::<Result<Vec<_>, _>>(); requests } - - fn read_data() -> Result<(cli::MsaInfo, Vec<cli::SimpleMail>), io::Error> { cli::with_dialog(|mut dialog| { let msa_info = dialog.read_msa_info()?; @@ -114,4 +115,3 @@ fn read_data() -> Result<(cli::MsaInfo, Vec<cli::SimpleMail>), io::Error> { Ok((msa_info, mails)) }) } - |