@@ -20,9 +20,9 @@ mod errors;
2020
2121use errors:: * ;
2222use futures:: { future, Future } ;
23- use intecture_api:: remote:: { Executable , Runnable } ;
2423use intecture_api:: host:: local:: Local ;
2524use intecture_api:: host:: remote:: { JsonLineProto , LineMessage } ;
25+ use intecture_api:: remote:: { Executable , Request } ;
2626use std:: fs:: File ;
2727use std:: io:: { self , Read } ;
2828use std:: net:: SocketAddr ;
@@ -40,7 +40,7 @@ pub struct Api {
4040impl Service for Api {
4141 type Request = LineMessage ;
4242 type Response = LineMessage ;
43- type Error = io :: Error ;
43+ type Error = Error ;
4444 type Future = Box < Future < Item = Self :: Response , Error = Self :: Error > > ;
4545
4646 fn call ( & self , req : Self :: Request ) -> Self :: Future {
@@ -49,15 +49,9 @@ impl Service for Api {
4949 Message :: WithoutBody ( req) => req,
5050 } ;
5151
52- let runnable : Runnable = match serde_json:: from_value ( req) . chain_err ( || "Received invalid Runnable " ) {
52+ let request : Request = match serde_json:: from_value ( req) . chain_err ( || "Received invalid Request " ) {
5353 Ok ( r) => r,
54- Err ( e) => return Box :: new (
55- future:: err (
56- io:: Error :: new (
57- // @todo Can't wrap 'e' as error_chain Error doesn't derive Sync.
58- // Waiting for https://github.com/rust-lang-nursery/error-chain/pull/163
59- io:: ErrorKind :: Other , e. description ( )
60- ) ) ) ,
54+ Err ( e) => return Box :: new ( future:: err ( e) )
6155 } ;
6256
6357 // XXX Danger zone! If we're running multiple threads, this `unwrap()`
@@ -66,21 +60,25 @@ impl Service for Api {
6660 // only safe for the current thread.
6761 // See https://github.com/alexcrichton/tokio-process/issues/23
6862 let handle = self . remote . handle ( ) . unwrap ( ) ;
69- Box :: new ( runnable. exec ( & self . host , & handle)
70- // @todo Can't wrap 'e' as error_chain Error doesn't derive Sync.
71- // Waiting for https://github.com/rust-lang-nursery/error-chain/pull/163
72- . map_err ( |e| io:: Error :: new ( io:: ErrorKind :: Other , e. description ( ) ) )
73- . and_then ( |ser| match serde_json:: to_value ( ser) . chain_err ( || "Could not serialize result" ) {
74- Ok ( v) => future:: ok ( Message :: WithoutBody ( v) ) ,
75- Err ( e) => future:: err ( io:: Error :: new ( io:: ErrorKind :: Other , e. description ( ) ) ) ,
63+ Box :: new ( request. exec ( & self . host , & handle)
64+ . chain_err ( || "Failed to execute Request" )
65+ . and_then ( |mut msg| {
66+ let body = msg. take_body ( ) ;
67+ match serde_json:: to_value ( msg. into_inner ( ) ) . chain_err ( || "Could not serialize result" ) {
68+ Ok ( v) => match body {
69+ Some ( b) => future:: ok ( Message :: WithBody ( v, b) ) ,
70+ None => future:: ok ( Message :: WithoutBody ( v) ) ,
71+ } ,
72+ Err ( e) => future:: err ( e) ,
73+ }
7674 } ) )
7775 }
7876}
7977
8078impl NewService for Api {
8179 type Request = LineMessage ;
8280 type Response = LineMessage ;
83- type Error = io :: Error ;
81+ type Error = Error ;
8482 type Instance = Api ;
8583 fn new_service ( & self ) -> io:: Result < Self :: Instance > {
8684 Ok ( Api {
0 commit comments