Skip to content

Commit 36e4210

Browse files
Commit
1 parent 7b097b7 commit 36e4210

File tree

2 files changed

+104
-19
lines changed

2 files changed

+104
-19
lines changed

thrust-macros-test/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
extern crate thrust;
55
extern crate tangle;
66

7+
use thrust::reactor::Reactor;
78
use thrust::binary_protocol::*;
89
use thrust::protocol::*;
10+
use std::net::SocketAddr;
911
use std::io::{Cursor, Read, Write};
1012

1113
thrust!("
@@ -16,6 +18,13 @@ thrust!("
1618
}
1719
");
1820

21+
#[test]
22+
fn client() {
23+
// let addr = "127.0.0.1:5656".parse().unwrap();
24+
// let rpc = foobar1::FlockDbClient::new(addr);
25+
// Reactor::run();
26+
}
27+
1928
#[test]
2029
fn args_deserialize_gen() {
2130
let mut buf = Vec::new();

thrust-parser/src/lib.rs

Lines changed: 95 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -154,32 +154,108 @@ impl Service {
154154
}).unwrap()
155155
}
156156

157+
pub fn client_method_body(&self, cx: &mut ExtCtxt, method: &ServiceMethod) -> P<ast::Expr> {
158+
let method_name = method.ident.clone();
159+
let struct_name_str = format!("{}_{}_Args", self.ident.clone(), method.ident.clone());
160+
161+
quote_expr!(cx, {
162+
use std::io::Cursor;
163+
let (res, future) = Future::<(ThriftMessage, BinaryDeserializer<Cursor<Vec<u8>>>)>::channel();
164+
let mut buf = Vec::new();
165+
166+
{
167+
let mut se = BinarySerializer::new(&mut buf);
168+
let args = FlockDb_query_Args {
169+
voodoo: voodoo,
170+
mission_control: mission_control
171+
};
172+
173+
args.serialize(&mut se);
174+
}
175+
176+
self.dispatcher.send(Incoming::Call($method_name.to_string(), buf, res)).unwrap();
177+
future.and_then(move |(msg, de)| {
178+
Async::Ok("foobar".to_string())
179+
})
180+
})
181+
}
182+
157183
pub fn generate_client_service_impl(&self, cx: &mut ExtCtxt) -> P<ast::Item> {
158184
let mut ident = token::str_to_ident(&self.ident.clone());
159185
let struct_ident = token::str_to_ident(&format!("{}Client", self.ident.clone()));
186+
let mut methods = Vec::new();
187+
let span = cx.call_site();
160188

161-
quote_item!(cx, impl $ident for $struct_ident {
162-
fn query(&mut self, voodoo: String, mission_control: i32) -> Future<String> {
163-
use std::io::Cursor;
164-
let (res, future) = Future::<(ThriftMessage, BinaryDeserializer<Cursor<Vec<u8>>>)>::channel();
165-
let mut buf = Vec::new();
189+
// Generate a Rust method for each method.
190+
for method in self.methods.iter() {
191+
let method_ident = token::str_to_ident(&method.ident);
192+
let self_ident = token::str_to_ident("self");
193+
let mut inputs = vec![
194+
ast::Arg::new_self(span, ast::Mutability::Immutable, self_ident.clone())
195+
];
196+
let ty = method.ty.to_ast(cx);
166197

167-
{
168-
let mut se = BinarySerializer::new(&mut buf);
169-
let args = FlockDb_query_Args {
170-
voodoo: voodoo,
171-
mission_control: mission_control
172-
};
198+
for arg in method.args.iter() {
199+
let arg_ident = token::str_to_ident(&arg.ident);
200+
let arg_ty = arg.ty.to_ast(cx);
201+
inputs.push(
202+
cx.arg(span, arg_ident, arg_ty)
203+
);
204+
}
173205

174-
args.serialize(&mut se);
175-
}
206+
let method_body = self.client_method_body(cx, method);
207+
let method_node = ast::ImplItemKind::Method(
208+
ast::MethodSig {
209+
unsafety: ast::Unsafety::Normal,
210+
constness: ast::Constness::NotConst,
211+
abi: syntax::abi::Abi::Rust,
212+
decl: P(ast::FnDecl {
213+
inputs: inputs,
214+
output: ast::FunctionRetTy::Ty(quote_ty!(cx, Future<$ty>)),
215+
variadic: false
216+
}),
217+
generics: ast::Generics::default(),
218+
explicit_self: ast::ExplicitSelf {
219+
node: ast::SelfKind::Region(None, ast::Mutability::Mutable, self_ident),
220+
span: span
221+
}
222+
},
223+
cx.block(span, Vec::new(), Some(method_body))
224+
);
176225

177-
self.dispatcher.send(Incoming::Call("foobar123".to_string(), buf, res)).unwrap();
178-
future.and_then(move |(msg, de)| {
179-
Async::Ok("foobar".to_string())
180-
})
181-
}
182-
}).unwrap()
226+
let mut item = ast::ImplItem {
227+
id: ast::DUMMY_NODE_ID,
228+
ident: method_ident,
229+
vis: ast::Visibility::Inherited,
230+
defaultness: ast::Defaultness::Final,
231+
attrs: Vec::new(),
232+
node: method_node,
233+
span: span
234+
};
235+
236+
methods.push(item);
237+
}
238+
239+
let impl_item = ast::ItemKind::Impl(
240+
ast::Unsafety::Normal,
241+
ast::ImplPolarity::Positive,
242+
ast::Generics::default(),
243+
Some(ast::TraitRef {
244+
path: ast::Path::from_ident(span, vec![struct_ident.clone()]),
245+
ref_id: ast::DUMMY_NODE_ID
246+
}), // TraitRef
247+
cx.ty_ident(span, struct_ident),
248+
methods
249+
);
250+
251+
P(ast::Item {
252+
ident: token::str_to_ident(&self.ident.clone()),
253+
attrs: Vec::new(),
254+
id: ast::DUMMY_NODE_ID,
255+
node: impl_item,
256+
vis: ast::Visibility::Inherit,
257+
span: span
258+
})
183259
}
184260
}
185261

0 commit comments

Comments
 (0)