From 1e4d5eb8f1fe1d7abe726572b9ee063d91f72a98 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Thu, 9 Jun 2022 10:43:38 +0200 Subject: Compiling with a MPSC channel --- src/command.rs | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) (limited to 'src/command.rs') diff --git a/src/command.rs b/src/command.rs index 9b2f12d..4c2ec41 100644 --- a/src/command.rs +++ b/src/command.rs @@ -12,15 +12,14 @@ use crate::mailstore::Mailstore; use crate::mailbox::Mailbox; use crate::service::Session; -pub struct Command { +pub struct Command<'a> { tag: Tag, - mailstore: Arc, - session: Arc>, + session: &'a mut Session, } -impl Command { - pub fn new(tag: Tag, mailstore: Arc, session: Arc>) -> Self { - Self { tag, mailstore, session } +impl<'a> Command<'a> { + pub fn new(tag: Tag, session: &'a mut Session) -> Self { + Self { tag, session } } pub async fn capability(&self) -> Result { @@ -31,24 +30,19 @@ impl Command { Ok(r) } - pub async fn login(&self, username: AString, password: AString) -> Result { + pub async fn login(&mut self, username: AString, password: AString) -> Result { let (u, p) = match (String::try_from(username), String::try_from(password)) { (Ok(u), Ok(p)) => (u, p), _ => return Response::bad("Invalid characters"), }; tracing::debug!(user = %u, "command.login"); - let creds = match self.mailstore.login_provider.login(&u, &p).await { + let creds = match self.session.mailstore.login_provider.login(&u, &p).await { Err(_) => return Response::no("[AUTHENTICATIONFAILED] Authentication failed."), Ok(c) => c, }; - let mut session = match self.session.lock() { - Err(_) => return Response::bad("[AUTHENTICATIONFAILED] Unable to acquire lock on session."), - Ok(s) => s, - }; - session.creds = Some(creds); - drop(session); + self.session.creds = Some(creds); Response::ok("Logged in") } @@ -61,16 +55,10 @@ impl Command { Response::bad("Not implemented") } - pub async fn select(&self, mailbox: MailboxCodec) -> Result { - - let mut session = match self.session.lock() { - Err(_) => return Response::no("[SELECTFAILED] Unable to acquire lock on session."), - Ok(s) => s, - }; + pub async fn select(&mut self, mailbox: MailboxCodec) -> Result { - let mb = Mailbox::new(session.creds.as_ref().unwrap(), "TestMailbox".to_string()).unwrap(); - session.selected = Some(mb); - drop(session); + let mb = Mailbox::new(self.session.creds.as_ref().unwrap(), "TestMailbox".to_string()).unwrap(); + self.session.selected = Some(mb); Response::bad("Not implemented") } -- cgit v1.2.3