@@ -9,23 +9,23 @@ pub struct ClientHandshake<'a> {
99 username : & ' a [ u8 ] ,
1010}
1111
12- named ! (
13- pub client_handshake< ClientHandshake > ,
14- do_parse! (
15- cap : apply! ( nom:: le_u32 , ) >>
16- maxps : apply! ( nom:: le_u32 , ) >>
17- collation : take! ( 1 ) >>
18- take! ( 23 ) >>
19- username : take_until! ( & b" \0 " [ .. ] ) >>
20- tag! ( b" \0 " ) >> //rustfmt
21- ( ClientHandshake {
12+ pub fn client_handshake ( i : & [ u8 ] ) -> nom :: IResult < & [ u8 ] , ClientHandshake > {
13+ let ( i , cap ) = nom :: number :: complete :: le_u32 ( i ) ? ;
14+ let ( i , maxps ) = nom :: number :: complete :: le_u32 ( i ) ? ;
15+ let ( i , collation ) = nom:: bytes :: complete :: take ( 1u8 ) ( i ) ? ;
16+ let ( i , _ ) = nom:: bytes :: complete :: take ( 23u8 ) ( i ) ? ;
17+ let ( i , username ) = nom :: bytes :: complete :: take_until ( & b" \0 " [ .. ] ) ( i ) ? ;
18+ let ( i , _ ) = nom :: bytes :: complete :: tag ( b" \0 " ) ( i ) ? ;
19+ Ok ( (
20+ i ,
21+ ClientHandshake {
2222 capabilities : CapabilityFlags :: from_bits_truncate ( cap) ,
2323 maxps,
2424 collation : u16:: from ( collation[ 0 ] ) ,
2525 username,
26- } )
27- )
28- ) ;
26+ } ,
27+ ) )
28+ }
2929
3030#[ derive( Debug , PartialEq , Eq ) ]
3131pub enum Command < ' a > {
@@ -47,48 +47,63 @@ pub enum Command<'a> {
4747 Quit ,
4848}
4949
50- named ! (
51- execute<Command >,
52- do_parse!(
53- stmt: apply!( nom:: le_u32, ) >>
54- _flags: take!( 1 ) >>
55- _iterations: apply!( nom:: le_u32, ) >>
56- rest: apply!( nom:: rest, ) >> //rustfmt
57- ( Command :: Execute {
58- stmt,
59- params: rest,
60- } )
61- )
62- ) ;
50+ pub fn execute ( i : & [ u8 ] ) -> nom:: IResult < & [ u8 ] , Command > {
51+ let ( i, stmt) = nom:: number:: complete:: le_u32 ( i) ?;
52+ let ( i, _flags) = nom:: bytes:: complete:: take ( 1u8 ) ( i) ?;
53+ let ( i, _iterations) = nom:: number:: complete:: le_u32 ( i) ?;
54+ Ok ( ( & [ ] , Command :: Execute { stmt, params : i } ) )
55+ }
6356
64- named ! (
65- send_long_data<Command >,
66- do_parse!(
67- stmt: apply!( nom:: le_u32, ) >>
68- param: apply!( nom:: le_u16, ) >>
69- data: apply!( nom:: rest, ) >> // rustfmt
70- ( Command :: SendLongData {
57+ pub fn send_long_data ( i : & [ u8 ] ) -> nom:: IResult < & [ u8 ] , Command > {
58+ let ( i, stmt) = nom:: number:: complete:: le_u32 ( i) ?;
59+ let ( i, param) = nom:: number:: complete:: le_u16 ( i) ?;
60+ Ok ( (
61+ & [ ] ,
62+ Command :: SendLongData {
7163 stmt,
7264 param,
73- data,
74- } )
75- )
76- ) ;
65+ data : i ,
66+ } ,
67+ ) )
68+ }
7769
78- named ! (
79- pub parse<Command >,
80- alt!(
81- preceded!( tag!( & [ CommandByte :: COM_QUERY as u8 ] ) , apply!( nom:: rest, ) ) => { |sql| Command :: Query ( sql) } |
82- preceded!( tag!( & [ CommandByte :: COM_FIELD_LIST as u8 ] ) , apply!( nom:: rest, ) ) => { |filter| Command :: ListFields ( filter) } |
83- preceded!( tag!( & [ CommandByte :: COM_INIT_DB as u8 ] ) , apply!( nom:: rest, ) ) => { |db| Command :: Init ( db) } |
84- preceded!( tag!( & [ CommandByte :: COM_STMT_PREPARE as u8 ] ) , apply!( nom:: rest, ) ) => { |sql| Command :: Prepare ( sql) } |
85- preceded!( tag!( & [ CommandByte :: COM_STMT_EXECUTE as u8 ] ) , execute) |
86- preceded!( tag!( & [ CommandByte :: COM_STMT_SEND_LONG_DATA as u8 ] ) , send_long_data) |
87- preceded!( tag!( & [ CommandByte :: COM_STMT_CLOSE as u8 ] ) , apply!( nom:: le_u32, ) ) => { |stmt| Command :: Close ( stmt) } |
88- tag!( & [ CommandByte :: COM_QUIT as u8 ] ) => { |_| Command :: Quit } |
89- tag!( & [ CommandByte :: COM_PING as u8 ] ) => { |_| Command :: Ping }
90- )
91- ) ;
70+ pub fn parse ( i : & [ u8 ] ) -> nom:: IResult < & [ u8 ] , Command > {
71+ use nom:: bytes:: complete:: tag;
72+ use nom:: combinator:: { map, rest} ;
73+ use nom:: sequence:: preceded;
74+ nom:: branch:: alt ( (
75+ map (
76+ preceded ( tag ( & [ CommandByte :: COM_QUERY as u8 ] ) , rest) ,
77+ Command :: Query ,
78+ ) ,
79+ map (
80+ preceded ( tag ( & [ CommandByte :: COM_FIELD_LIST as u8 ] ) , rest) ,
81+ Command :: ListFields ,
82+ ) ,
83+ map (
84+ preceded ( tag ( & [ CommandByte :: COM_INIT_DB as u8 ] ) , rest) ,
85+ Command :: Init ,
86+ ) ,
87+ map (
88+ preceded ( tag ( & [ CommandByte :: COM_STMT_PREPARE as u8 ] ) , rest) ,
89+ Command :: Prepare ,
90+ ) ,
91+ preceded ( tag ( & [ CommandByte :: COM_STMT_EXECUTE as u8 ] ) , execute) ,
92+ preceded (
93+ tag ( & [ CommandByte :: COM_STMT_SEND_LONG_DATA as u8 ] ) ,
94+ send_long_data,
95+ ) ,
96+ map (
97+ preceded (
98+ tag ( & [ CommandByte :: COM_STMT_CLOSE as u8 ] ) ,
99+ nom:: number:: complete:: le_u32,
100+ ) ,
101+ Command :: Close ,
102+ ) ,
103+ map ( tag ( & [ CommandByte :: COM_QUIT as u8 ] ) , |_| Command :: Quit ) ,
104+ map ( tag ( & [ CommandByte :: COM_PING as u8 ] ) , |_| Command :: Ping ) ,
105+ ) ) ( i)
106+ }
92107
93108#[ cfg( test) ]
94109mod tests {
0 commit comments