silver_rs/
lib.rs

1extern crate bytes;
2extern crate futures;
3extern crate httparse;
4extern crate tokio_codec;
5extern crate tokio_io;
6extern crate tokio_proto;
7extern crate tokio_service;
8
9mod request;
10mod response;
11
12use std::io;
13
14pub use request::Request;
15pub use response::Response;
16
17use bytes::BytesMut;
18use futures::future;
19use tokio_codec::{Decoder, Encoder};
20use tokio_io::codec::Framed;
21use tokio_io::{AsyncRead, AsyncWrite};
22use tokio_proto::pipeline::ServerProto;
23
24pub use tokio_proto::TcpServer as Server;
25pub use tokio_service::Service as Handler;
26
27pub type SilverResult = future::Ok<Response, io::Error>;
28
29pub struct Http;
30
31impl<T: AsyncRead + AsyncWrite + 'static> ServerProto<T> for Http {
32    type Request = Request;
33    type Response = Response;
34    type Transport = Framed<T, HttpCodec>;
35    type BindTransport = io::Result<Framed<T, HttpCodec>>;
36
37    fn bind_transport(&self, io: T) -> io::Result<Framed<T, HttpCodec>> {
38        Ok(io.framed(HttpCodec))
39    }
40}
41
42pub struct HttpCodec;
43
44impl Decoder for HttpCodec {
45    type Item = Request;
46    type Error = io::Error;
47
48    fn decode(&mut self, buf: &mut BytesMut) -> io::Result<Option<Request>> {
49        request::decode(buf)
50    }
51}
52
53impl Encoder for HttpCodec {
54    type Item = Response;
55    type Error = io::Error;
56
57    fn encode(&mut self, msg: Response, buf: &mut BytesMut) -> io::Result<()> {
58        response::encode(&msg, buf);
59        Ok(())
60    }
61}