Guest User

Untitled

a guest
Apr 10th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. extern crate futures;
  2.  
  3. use futures::{ prelude::*, future, IntoFuture, Poll };
  4.  
  5. #[derive(Debug)]
  6. enum IMAPError {
  7. IO(std::io::Error),
  8. BAD(String),
  9. NO(String),
  10. }
  11.  
  12. impl std::fmt::Display for IMAPError {
  13. fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
  14. write!(fmt, "IMAPError")
  15. }
  16. }
  17.  
  18. impl std::error::Error for IMAPError {
  19. fn description(&self) -> &str {
  20. "IMAPError"
  21. }
  22. }
  23.  
  24. #[derive(Debug)]
  25. enum IMAPClient {
  26. NotAuthenticated,
  27. Authenticated,
  28. }
  29.  
  30. enum EitherF<A,B,T,E>
  31. where A: Future<Item=T,Error=E>,
  32. B: Future<Item=T,Error=E>
  33. {
  34. Left(A),
  35. Right(B),
  36. }
  37.  
  38. impl<A,B,T,E> Future for EitherF<A,B,T,E>
  39. where A: Future<Item=T,Error=E>,
  40. B: Future<Item=T,Error=E>
  41. {
  42. type Item = T;
  43. type Error = E;
  44. fn poll(&mut self, ctx: &mut futures::task::Context<'_>) -> futures::Poll<T,E>{
  45. match self {
  46. EitherF::Left(f) => f.poll(ctx),
  47. EitherF::Right(f) => f.poll(ctx)
  48. }
  49. }
  50. }
  51.  
  52. impl IMAPClient {
  53. fn connect(_host: &str, _port: u32) -> impl Future<Item=IMAPClient, Error=IMAPError> {
  54. let client = IMAPClient::NotAuthenticated;
  55. future::ok(client)
  56. }
  57. fn login(self, _user: &str, _pass: &str) -> impl Future<Item=IMAPClient, Error=IMAPError> {
  58. let client = IMAPClient::Authenticated;
  59. future::ok(client)
  60. }
  61. fn id(self) -> impl Future<Item=IMAPClient, Error=IMAPError> {
  62. future::ok(self)
  63. }
  64. }
  65.  
  66. fn main() {
  67. let app = IMAPClient::connect("mail.example.com", 993)
  68. .and_then(|client|{
  69. println!("Connected: {:?}", client);
  70. match client {
  71. IMAPClient::NotAuthenticated => EitherF::Left(client.login("user", "password")),
  72. IMAPClient::Authenticated => EitherF::Right(future::ok(client)),
  73. }
  74. })
  75. .and_then(|client|{
  76. println!("Logged in: {:?}", client);
  77. future::ok(client)
  78. })
  79. .map(|_|())
  80. .map_err(|err|{
  81. println!("Error={}", err);
  82. ()
  83. });
  84.  
  85. let mut pool = futures::executor::ThreadPool::new();
  86. pool.run(app).unwrap();
  87. }
Add Comment
Please, Sign In to add comment