Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate futures;
- use futures::{ prelude::*, future, IntoFuture, Poll };
- #[derive(Debug)]
- enum IMAPError {
- IO(std::io::Error),
- BAD(String),
- NO(String),
- }
- impl std::fmt::Display for IMAPError {
- fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
- write!(fmt, "IMAPError")
- }
- }
- impl std::error::Error for IMAPError {
- fn description(&self) -> &str {
- "IMAPError"
- }
- }
- #[derive(Debug)]
- enum IMAPClient {
- NotAuthenticated,
- Authenticated,
- }
- enum EitherF<A,B,T,E>
- where A: Future<Item=T,Error=E>,
- B: Future<Item=T,Error=E>
- {
- Left(A),
- Right(B),
- }
- impl<A,B,T,E> Future for EitherF<A,B,T,E>
- where A: Future<Item=T,Error=E>,
- B: Future<Item=T,Error=E>
- {
- type Item = T;
- type Error = E;
- fn poll(&mut self, ctx: &mut futures::task::Context<'_>) -> futures::Poll<T,E>{
- match self {
- EitherF::Left(f) => f.poll(ctx),
- EitherF::Right(f) => f.poll(ctx)
- }
- }
- }
- impl IMAPClient {
- fn connect(_host: &str, _port: u32) -> impl Future<Item=IMAPClient, Error=IMAPError> {
- let client = IMAPClient::NotAuthenticated;
- future::ok(client)
- }
- fn login(self, _user: &str, _pass: &str) -> impl Future<Item=IMAPClient, Error=IMAPError> {
- let client = IMAPClient::Authenticated;
- future::ok(client)
- }
- fn id(self) -> impl Future<Item=IMAPClient, Error=IMAPError> {
- future::ok(self)
- }
- }
- fn main() {
- let app = IMAPClient::connect("mail.example.com", 993)
- .and_then(|client|{
- println!("Connected: {:?}", client);
- match client {
- IMAPClient::NotAuthenticated => EitherF::Left(client.login("user", "password")),
- IMAPClient::Authenticated => EitherF::Right(future::ok(client)),
- }
- })
- .and_then(|client|{
- println!("Logged in: {:?}", client);
- future::ok(client)
- })
- .map(|_|())
- .map_err(|err|{
- println!("Error={}", err);
- ()
- });
- let mut pool = futures::executor::ThreadPool::new();
- pool.run(app).unwrap();
- }
Add Comment
Please, Sign In to add comment