cdrs_temp/query/
utils.rs

1use std::cell::RefCell;
2
3use crate::cluster::{GetCompressor, GetConnection};
4use crate::error;
5use crate::frame::parser::from_connection;
6use crate::frame::{Flag, Frame};
7use crate::transport::CDRSTransport;
8
9pub fn prepare_flags(with_tracing: bool, with_warnings: bool) -> Vec<Flag> {
10    let mut flags = vec![];
11
12    if with_tracing {
13        flags.push(Flag::Tracing);
14    }
15
16    if with_warnings {
17        flags.push(Flag::Warning);
18    }
19
20    flags
21}
22
23pub fn send_frame<S, T, M>(sender: &S, frame_bytes: Vec<u8>) -> error::Result<Frame>
24where
25    S: GetConnection<T, M> + GetCompressor<'static> + Sized,
26    T: CDRSTransport + 'static,
27    M: r2d2::ManageConnection<Connection = RefCell<T>, Error = error::Error> + Sized,
28{
29    let ref compression = sender.get_compressor();
30
31    sender
32        .get_connection()
33        .ok_or(error::Error::from("Unable to get transport"))
34        .and_then(|transport_cell| {
35            let write_res = transport_cell
36                .borrow_mut()
37                .write(frame_bytes.as_slice())
38                .map_err(error::Error::from);
39            write_res.map(|_| transport_cell)
40        })
41        .and_then(|transport_cell| from_connection(&transport_cell, compression))
42}
43
44#[cfg(test)]
45mod test {
46    use super::*;
47
48    #[test]
49    fn prepare_flags_test() {
50        assert_eq!(prepare_flags(true, false), vec![Flag::Tracing]);
51        assert_eq!(prepare_flags(false, true), vec![Flag::Warning]);
52        assert_eq!(
53            prepare_flags(true, true),
54            vec![Flag::Tracing, Flag::Warning]
55        );
56    }
57}