1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use std::io::Read;
use chrono_tz::Tz;
use crate::binary::ReadEx;
use crate::errors::DriverError;
use crate::errors::Error;
use crate::errors::Result;
use crate::protocols::HelloRequest;
use crate::protocols::Packet;
use crate::protocols::QueryRequest;
use crate::protocols::{self};
use crate::types::Block;
pub struct Parser<T> {
reader: T,
tz: Tz,
}
impl<T: Read> Parser<T> {
pub(crate) fn new(reader: T, tz: Tz) -> Parser<T> {
Self { reader, tz }
}
pub(crate) fn parse_packet(
&mut self,
hello: &Option<HelloRequest>,
compress: bool,
) -> Result<Packet> {
let packet = self.reader.read_uvarint()?;
match packet {
protocols::CLIENT_PING => Ok(Packet::Ping),
protocols::CLIENT_CANCEL => Ok(Packet::Cancel),
protocols::CLIENT_DATA | protocols::CLIENT_SCALAR => {
Ok(self.parse_data(packet == protocols::CLIENT_SCALAR, compress)?)
}
protocols::CLIENT_QUERY => Ok(self.parse_query(hello, compress)?),
protocols::CLIENT_HELLO => Ok(self.parse_hello()?),
_ => Err(Error::Driver(DriverError::UnknownPacket { packet })),
}
}
fn parse_hello(&mut self) -> Result<Packet> {
Ok(Packet::Hello(HelloRequest::read_from(&mut self.reader)?))
}
fn parse_query(&mut self, hello: &Option<HelloRequest>, _compress: bool) -> Result<Packet> {
match hello {
Some(ref hello) => {
let query = QueryRequest::read_from(&mut self.reader, hello)?;
Ok(Packet::Query(query))
}
_ => Err(Error::Driver(DriverError::UnexpectedPacket)),
}
}
fn parse_data(&mut self, _scalar: bool, compress: bool) -> Result<Packet> {
let _temporary_table = self.reader.read_string()?;
let block = Block::load(&mut self.reader, self.tz, compress)?;
Ok(Packet::Data(block))
}
}