Skip to content

Commit 30acc4e

Browse files
More code gen
1 parent 56317bf commit 30acc4e

File tree

7 files changed

+301
-23
lines changed

7 files changed

+301
-23
lines changed

Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,3 @@ git = "https://github.com/carllerche/mio"
2727

2828
[dependencies.bytes]
2929
git = "https://github.com/carllerche/bytes"
30-
31-
[dependencies.thrust_parser]
32-
path = "./thrust-parser"

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ use std::string;
2121

2222
mod util;
2323
mod event_loop;
24-
mod reactor;
24+
pub mod reactor;
2525
pub mod protocol;
2626
pub mod binary_protocol;
2727
// mod service;
2828
mod pipeline;
2929
mod runner;
30-
mod dispatcher;
30+
pub mod dispatcher;
3131
mod result;
3232
mod transport;
3333

src/protocol.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl convert::From<i8> for ThriftType {
6565
13 => ThriftType::Map,
6666
14 => ThriftType::Set,
6767
15 => ThriftType::List,
68-
_ => panic!("Unexpected value")
68+
e => panic!("Unexpected value: {}", e)
6969
}
7070
}
7171
}
@@ -163,7 +163,7 @@ pub struct ThriftMessage {
163163
pub ty: ThriftMessageType,
164164
pub seq: i16
165165
}
166-
166+
#[derive(Debug)]
167167
pub struct ThriftField {
168168
pub name: Option<String>,
169169
pub ty: ThriftType,

thrust-macros-test/src/lib.rs

Lines changed: 97 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,112 @@
44
extern crate thrust;
55
extern crate tangle;
66

7+
use thrust::binary_protocol::*;
8+
use thrust::protocol::*;
9+
use std::io::{Cursor, Read, Write};
10+
711
thrust!("
812
namespace rust foobar1
913
10-
struct Flocker {
11-
1: required i64 fo;
14+
service FlockDb {
15+
map<string, byte> query(1: string voodoo, 2: i32 mission_control);
1216
}
17+
");
1318

14-
enum Foo {
15-
HELLO,
16-
Foobar
17-
}
19+
#[test]
20+
fn args_deserialize_gen() {
21+
let mut buf = Vec::new();
22+
23+
{
24+
let mut se = BinarySerializer::new(&mut buf);
25+
let args = foobar1::FlockDb_query_Args {
26+
voodoo: "Hello".to_string(),
27+
mission_control: 500
28+
};
1829

19-
struct Message {
20-
1: required i32 foobar;
30+
args.serialize(&mut se).unwrap();
2131
}
2232

23-
service FlockDb {
24-
map<string, byte> query(1: string voodoo);
33+
let mut rd = Cursor::new(buf);
34+
let mut de = BinaryDeserializer::new(rd);
35+
let args = foobar1::FlockDb_query_Args::deserialize(&mut de).unwrap();
36+
assert_eq!(&*args.voodoo, "Hello");
37+
assert_eq!(args.mission_control, 500);
38+
}
39+
40+
#[test]
41+
fn manual_args_deserialize() {
42+
let mut buf = Vec::new();
43+
44+
{
45+
let mut se = BinarySerializer::new(&mut buf);
46+
se.write_struct_begin("FlockDb_query_Args").unwrap();
47+
48+
se.write_field_begin("voodoo", ThriftType::String, 1).unwrap();
49+
"Hello".to_string().serialize(&mut se);
50+
se.write_field_stop();
51+
se.write_field_end();
52+
53+
se.write_field_begin("mission_control", ThriftType::I32, 2).unwrap();
54+
let i: i32 = 500;
55+
i.serialize(&mut se);
56+
se.write_field_stop();
57+
se.write_field_end();
58+
59+
se.write_struct_end();
2560
}
26-
");
61+
62+
let mut rd = Cursor::new(buf);
63+
let mut de = BinaryDeserializer::new(rd);
64+
let msg = de.read_struct_begin().unwrap();
65+
let voodoo = de.read_field_begin().unwrap();
66+
assert!(voodoo.name.is_none());
67+
assert_eq!(voodoo.seq, 1);
68+
assert_eq!(voodoo.ty, ThriftType::String);
69+
assert_eq!(&*de.deserialize_str().unwrap(), "Hello");
70+
de.read_field_begin().unwrap();
71+
let mission = de.read_field_begin().unwrap();
72+
assert_eq!(mission.seq, 2);
73+
assert_eq!(mission.ty, ThriftType::I32);
74+
assert_eq!(de.deserialize_i32().unwrap(), 500);
75+
}
2776

2877
#[test]
29-
fn compile() {
30-
let m = foobar1::Foo::HELLO;
31-
println!("{:?}", m);
78+
fn serialize() {
79+
let mut buf = Vec::new();
80+
let mut comp = Vec::new();
81+
82+
{
83+
let mut se = BinarySerializer::new(&mut buf);
84+
let args = foobar1::FlockDb_query_Args {
85+
voodoo: "Hello".to_string(),
86+
mission_control: 500
87+
};
88+
89+
args.serialize(&mut se).unwrap();
90+
}
91+
92+
{
93+
let mut se = BinarySerializer::new(&mut comp);
94+
se.write_struct_begin("FlockDb_query_Args").unwrap();
95+
96+
se.write_field_begin("voodoo", ThriftType::String, 1).unwrap();
97+
"Hello".to_string().serialize(&mut se);
98+
se.write_field_stop();
99+
se.write_field_end();
100+
101+
se.write_field_begin("mission_control", ThriftType::I32, 2).unwrap();
102+
let i: i32 = 500;
103+
i.serialize(&mut se);
104+
se.write_field_stop();
105+
se.write_field_end();
106+
107+
se.write_struct_end();
108+
}
109+
110+
assert_eq!(buf.len(), comp.len());
111+
112+
for i in 0..buf.len() {
113+
assert_eq!(buf[i], comp[i]);
114+
}
32115
}

thrust-macros/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,13 @@ impl<'a, 'x> Compiler<'a, 'x> {
9292

9393
Ok(quote_item!(self.cx, pub mod $module {
9494
#![allow(dead_code, unused_imports)]
95-
use thrust::protocol::Error;
95+
use thrust::protocol::{Error, ThriftType};
96+
use thrust::{ThrustResult, ThrustError};
97+
use thrust::dispatcher::{self, Dispatcher};
98+
use thrust::reactor::Message;
99+
use std::thread::JoinHandle;
100+
use std::net::SocketAddr;
101+
use std::sync::mpsc::{Sender, Receiver};
96102
use tangle::{Future, Async};
97103
use std::collections::{HashMap, HashSet};
98104
use thrust::protocol::{ThriftDeserializer, ThriftSerializer};

thrust-parser/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ version = "0.1.0"
44
authors = ["Daniel Fagnan <dnfagnan@gmail.com>"]
55

66
[dependencies]
7+
8+
[dependencies.thrust]
9+
path = ".."

0 commit comments

Comments
 (0)