cassandra_protocol/query/
query_flags.rs

1use bitflags::bitflags;
2use std::io::{Cursor, Read};
3
4use crate::error;
5use crate::frame::{FromCursor, Serialize, Version};
6use crate::types::INT_LEN;
7
8bitflags! {
9    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
10    pub struct QueryFlags: u32 {
11        /// Indicates that Query Params contain value.
12        const VALUE = 0x001;
13        /// Indicates that Query Params does not contain metadata.
14        const SKIP_METADATA = 0x002;
15        /// Indicates that Query Params contain page size.
16        const PAGE_SIZE = 0x004;
17        /// Indicates that Query Params contain paging state.
18        const WITH_PAGING_STATE = 0x008;
19        /// Indicates that Query Params contain serial consistency.
20        const WITH_SERIAL_CONSISTENCY = 0x010;
21        /// Indicates that Query Params contain default timestamp.
22        const WITH_DEFAULT_TIMESTAMP = 0x020;
23        /// Indicates that Query Params values are named ones.
24        const WITH_NAMES_FOR_VALUES = 0x040;
25        /// Indicates that Query Params contain keyspace name.
26        const WITH_KEYSPACE = 0x080;
27        /// Indicates that Query Params contain "now" in seconds.
28        const WITH_NOW_IN_SECONDS = 0x100;
29    }
30}
31
32impl Default for QueryFlags {
33    #[inline]
34    fn default() -> Self {
35        QueryFlags::empty()
36    }
37}
38
39impl Serialize for QueryFlags {
40    fn serialize(&self, cursor: &mut Cursor<&mut Vec<u8>>, version: Version) {
41        if version >= Version::V5 {
42            self.bits().serialize(cursor, version);
43        } else {
44            (self.bits() as u8).serialize(cursor, version);
45        }
46    }
47}
48
49impl FromCursor for QueryFlags {
50    fn from_cursor(cursor: &mut Cursor<&[u8]>, version: Version) -> error::Result<Self> {
51        if version >= Version::V5 {
52            let mut buff = [0; INT_LEN];
53            cursor
54                .read_exact(&mut buff)
55                .map(|()| QueryFlags::from_bits_truncate(u32::from_be_bytes(buff)))
56                .map_err(|error| error.into())
57        } else {
58            let mut buff = [0];
59            cursor.read_exact(&mut buff)?;
60            Ok(QueryFlags::from_bits_truncate(buff[0] as u32))
61        }
62    }
63}