-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(imap-tasks): Introduce
Resolver
(#172)
* feat: init Scheduler::run_task fn * ci: fix check * feat: ensure that Scheduler is Send * fix: removed task utilities from Scheduler * fix: adjust code after review * feat: move run task to a dedicated Resolver * fix: adjust resolver-related namings
- Loading branch information
Showing
6 changed files
with
168 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use imap_flow::{ | ||
client::{ClientFlow, ClientFlowOptions}, | ||
stream::Stream, | ||
}; | ||
use tasks::{ | ||
resolver::Resolver, | ||
tasks::{authenticate::AuthenticateTask, capability::CapabilityTask, logout::LogoutTask}, | ||
}; | ||
use tokio::net::TcpStream; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let stream = TcpStream::connect("127.0.0.1:12345").await.unwrap(); | ||
let mut stream = Stream::insecure(stream); | ||
let client = ClientFlow::new(ClientFlowOptions::default()); | ||
let mut resolver = Resolver::new(client); | ||
|
||
let capability = stream | ||
.progress(resolver.resolve(CapabilityTask::new())) | ||
.await | ||
.unwrap() | ||
.unwrap(); | ||
|
||
println!("pre-auth capability: {capability:?}"); | ||
|
||
let capability = stream | ||
.progress(resolver.resolve(AuthenticateTask::plain("alice", "pa²²w0rd", true))) | ||
.await | ||
.unwrap() | ||
.unwrap(); | ||
|
||
println!("maybe post-auth capability: {capability:?}"); | ||
|
||
let capability = stream | ||
.progress(resolver.resolve(CapabilityTask::new())) | ||
.await | ||
.unwrap() | ||
.unwrap(); | ||
|
||
println!("post-auth capability: {capability:?}"); | ||
|
||
stream | ||
.progress(resolver.resolve(LogoutTask::default())) | ||
.await | ||
.unwrap() | ||
.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use imap_flow::{client::ClientFlow, Flow, FlowInterrupt}; | ||
use imap_types::response::{Response, Status}; | ||
use tracing::warn; | ||
|
||
use crate::{Scheduler, SchedulerError, SchedulerEvent, Task, TaskHandle}; | ||
|
||
/// The resolver is a scheduler than manages one task at a time. | ||
pub struct Resolver { | ||
scheduler: Scheduler, | ||
} | ||
|
||
impl Flow for Resolver { | ||
type Event = SchedulerEvent; | ||
type Error = SchedulerError; | ||
|
||
fn enqueue_input(&mut self, bytes: &[u8]) { | ||
self.scheduler.enqueue_input(bytes); | ||
} | ||
|
||
fn progress(&mut self) -> Result<Self::Event, FlowInterrupt<Self::Error>> { | ||
self.scheduler.progress() | ||
} | ||
} | ||
|
||
impl Resolver { | ||
/// Create a new resolver. | ||
pub fn new(flow: ClientFlow) -> Self { | ||
Self { | ||
scheduler: Scheduler::new(flow), | ||
} | ||
} | ||
|
||
/// Enqueue a [`Task`] for immediate resolution. | ||
pub fn resolve<T: Task>(&mut self, task: T) -> ResolvingTask<T> { | ||
let handle = self.scheduler.enqueue_task(task); | ||
|
||
ResolvingTask { | ||
resolver: self, | ||
handle, | ||
} | ||
} | ||
} | ||
|
||
pub struct ResolvingTask<'a, T: Task> { | ||
resolver: &'a mut Resolver, | ||
handle: TaskHandle<T>, | ||
} | ||
|
||
impl<T: Task> Flow for ResolvingTask<'_, T> { | ||
type Event = T::Output; | ||
type Error = SchedulerError; | ||
|
||
fn enqueue_input(&mut self, bytes: &[u8]) { | ||
self.resolver.enqueue_input(bytes); | ||
} | ||
|
||
fn progress(&mut self) -> Result<Self::Event, FlowInterrupt<Self::Error>> { | ||
loop { | ||
match self.resolver.progress()? { | ||
SchedulerEvent::TaskFinished(mut token) => { | ||
if let Some(output) = self.handle.resolve(&mut token) { | ||
break Ok(output); | ||
} else { | ||
warn!(?token, "received unexpected task token") | ||
} | ||
} | ||
SchedulerEvent::Unsolicited(unsolicited) => { | ||
if let Response::Status(Status::Bye(bye)) = unsolicited { | ||
let err = SchedulerError::UnexpectedByeResponse(bye); | ||
break Err(FlowInterrupt::Error(err)); | ||
} else { | ||
warn!(?unsolicited, "received unsolicited"); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use static_assertions::assert_impl_all; | ||
|
||
use super::Resolver; | ||
|
||
assert_impl_all!(Resolver: Send); | ||
} |