cassandra_proto/query/
query_flags.rs

1use crate::frame::AsByte;
2
3const FLAGS_VALUE: u8 = 0x01;
4const FLAGS_SKIP_METADATA: u8 = 0x02;
5const WITH_PAGE_SIZE: u8 = 0x04;
6const WITH_PAGING_STATE: u8 = 0x08;
7const WITH_SERIAL_CONSISTENCY: u8 = 0x10;
8const WITH_DEFAULT_TIMESTAMP: u8 = 0x20;
9const WITH_NAME_FOR_VALUES: u8 = 0x40;
10
11/// Cassandra Query Flags.
12#[derive(Clone, Debug)]
13pub enum QueryFlags {
14  /// If set indicates that Query Params contains value.
15  Value,
16  /// If set indicates that Query Params does not contain metadata.
17  SkipMetadata,
18  /// If set indicates that Query Params contains page size.
19  PageSize,
20  /// If set indicates that Query Params contains paging state.
21  WithPagingState,
22  /// If set indicates that Query Params contains serial consistency.
23  WithSerialConsistency,
24  /// If set indicates that Query Params contains default timestamp.
25  WithDefaultTimestamp,
26  /// If set indicates that Query Params values are named ones.
27  WithNamesForValues,
28}
29
30impl QueryFlags {
31  #[doc(hidden)]
32  pub fn has_value(byte: u8) -> bool {
33    (byte & FLAGS_VALUE) != 0
34  }
35
36  #[doc(hidden)]
37  pub fn set_value(byte: u8) -> u8 {
38    byte | FLAGS_VALUE
39  }
40
41  #[doc(hidden)]
42  pub fn has_skip_metadata(byte: u8) -> bool {
43    (byte & FLAGS_SKIP_METADATA) != 0
44  }
45
46  #[doc(hidden)]
47  pub fn set_skip_metadata(byte: u8) -> u8 {
48    byte | FLAGS_SKIP_METADATA
49  }
50
51  #[doc(hidden)]
52  pub fn has_page_size(byte: u8) -> bool {
53    (byte & WITH_PAGE_SIZE) != 0
54  }
55
56  #[doc(hidden)]
57  pub fn set_page_size(byte: u8) -> u8 {
58    byte | WITH_PAGE_SIZE
59  }
60
61  #[doc(hidden)]
62  pub fn has_with_paging_state(byte: u8) -> bool {
63    (byte & WITH_PAGING_STATE) != 0
64  }
65
66  #[doc(hidden)]
67  pub fn set_with_paging_state(byte: u8) -> u8 {
68    byte | WITH_PAGING_STATE
69  }
70
71  #[doc(hidden)]
72  pub fn has_with_serial_consistency(byte: u8) -> bool {
73    (byte & WITH_SERIAL_CONSISTENCY) != 0
74  }
75
76  #[doc(hidden)]
77  pub fn set_with_serial_consistency(byte: u8) -> u8 {
78    byte | WITH_SERIAL_CONSISTENCY
79  }
80
81  #[doc(hidden)]
82  pub fn has_with_default_timestamp(byte: u8) -> bool {
83    (byte & WITH_DEFAULT_TIMESTAMP) != 0
84  }
85
86  #[doc(hidden)]
87  pub fn set_with_default_timestamp(byte: u8) -> u8 {
88    byte | WITH_DEFAULT_TIMESTAMP
89  }
90
91  #[doc(hidden)]
92  pub fn has_with_names_for_values(byte: u8) -> bool {
93    (byte & WITH_NAME_FOR_VALUES) != 0
94  }
95
96  #[doc(hidden)]
97  pub fn set_with_names_for_values(byte: u8) -> u8 {
98    byte | WITH_NAME_FOR_VALUES
99  }
100}
101
102impl AsByte for QueryFlags {
103  fn as_byte(&self) -> u8 {
104    match *self {
105      QueryFlags::Value => FLAGS_VALUE,
106      QueryFlags::SkipMetadata => FLAGS_SKIP_METADATA,
107      QueryFlags::PageSize => WITH_PAGE_SIZE,
108      QueryFlags::WithPagingState => WITH_PAGING_STATE,
109      QueryFlags::WithSerialConsistency => WITH_SERIAL_CONSISTENCY,
110      QueryFlags::WithDefaultTimestamp => WITH_DEFAULT_TIMESTAMP,
111      QueryFlags::WithNamesForValues => WITH_NAME_FOR_VALUES,
112    }
113  }
114}
115
116#[cfg(test)]
117mod tests {
118  use super::*;
119
120  #[test]
121  fn has_value_test() {
122    assert!(QueryFlags::has_value(FLAGS_VALUE | 0x10),
123            "should show that the flag has value");
124    assert!(!QueryFlags::has_value(FLAGS_SKIP_METADATA),
125            "should show that the flag does NOT have value");
126  }
127
128  #[test]
129  fn set_value_test() {
130    assert_eq!(QueryFlags::set_value(0),
131               FLAGS_VALUE,
132               "should set has value flag");
133  }
134
135  #[test]
136  fn has_skip_metadata_test() {
137    assert!(QueryFlags::has_skip_metadata(FLAGS_SKIP_METADATA | 0x10),
138            "should show that the flag has skip metadata");
139    assert!(!QueryFlags::has_skip_metadata(FLAGS_VALUE),
140            "should show that the flag does NOT have skip metadata");
141  }
142
143  #[test]
144  fn set_skip_metadata_test() {
145    assert_eq!(QueryFlags::set_skip_metadata(0),
146               FLAGS_SKIP_METADATA,
147               "should set has skip metadata flag");
148  }
149
150  #[test]
151  fn has_page_size_test() {
152    assert!(QueryFlags::has_page_size(WITH_PAGE_SIZE | 0x10),
153            "should show that the flag has with page size");
154    assert!(!QueryFlags::has_page_size(FLAGS_VALUE),
155            "should show that the flag does NOT have with page size");
156  }
157
158  #[test]
159  fn set_page_size_test() {
160    assert_eq!(QueryFlags::set_page_size(0),
161               WITH_PAGE_SIZE,
162               "should set has page size flag");
163  }
164
165  #[test]
166  fn has_with_paging_state_test() {
167    assert!(QueryFlags::has_with_paging_state(WITH_PAGING_STATE | 0x10),
168            "should show that the flag has with paging state");
169    assert!(!QueryFlags::has_with_paging_state(FLAGS_VALUE),
170            "should show that the flag does NOT have with paging state");
171  }
172
173  #[test]
174  fn set_with_paging_state_test() {
175    assert_eq!(QueryFlags::set_with_paging_state(0),
176               WITH_PAGING_STATE,
177               "should set has with paging state flag");
178  }
179
180  #[test]
181  fn has_with_serial_consistency_test() {
182    assert!(QueryFlags::has_with_serial_consistency(WITH_SERIAL_CONSISTENCY | 0x11),
183            "should show that the flag has with serial consistency");
184    assert!(!QueryFlags::has_with_serial_consistency(FLAGS_VALUE),
185            "should show that the flag does NOT have with serial consistency");
186  }
187
188  #[test]
189  fn set_with_serial_consistency_test() {
190    assert_eq!(QueryFlags::set_with_serial_consistency(0),
191               WITH_SERIAL_CONSISTENCY,
192               "should set has with serial consistency flag");
193  }
194
195  #[test]
196  fn has_with_default_timestamp_test() {
197    assert!(QueryFlags::has_with_default_timestamp(WITH_DEFAULT_TIMESTAMP | 0x10),
198            "should show that the flag has with default timestamp");
199    assert!(!QueryFlags::has_with_default_timestamp(FLAGS_VALUE),
200            "should show that the flag does NOT have with default timestamp");
201  }
202
203  #[test]
204  fn set_with_default_timestamp_test() {
205    assert_eq!(QueryFlags::set_with_default_timestamp(0),
206               WITH_DEFAULT_TIMESTAMP,
207               "should set has with serial consistency flag");
208  }
209
210  #[test]
211  fn has_with_names_for_values_test() {
212    assert!(QueryFlags::has_with_names_for_values(WITH_NAME_FOR_VALUES | 0x10),
213            "should show that the flag has with name for values");
214    assert!(!QueryFlags::has_with_names_for_values(FLAGS_VALUE),
215            "should show that the flag does NOT have with name for values");
216  }
217
218  #[test]
219  fn set_with_names_for_values_test() {
220    assert_eq!(QueryFlags::set_with_names_for_values(0),
221               WITH_NAME_FOR_VALUES,
222               "should set has with name for values flag");
223  }
224
225  #[test]
226  fn as_byte_test() {
227    assert_eq!(QueryFlags::Value.as_byte(),
228               FLAGS_VALUE,
229               "should propery convert values flag");
230
231    assert_eq!(QueryFlags::SkipMetadata.as_byte(),
232               FLAGS_SKIP_METADATA,
233               "should propery convert skip metadata flag");
234
235    assert_eq!(QueryFlags::PageSize.as_byte(),
236               WITH_PAGE_SIZE,
237               "should propery convert with page size flag");
238
239    assert_eq!(QueryFlags::WithPagingState.as_byte(),
240               WITH_PAGING_STATE,
241               "should propery convert with paging state flag");
242
243    assert_eq!(QueryFlags::WithSerialConsistency.as_byte(),
244               WITH_SERIAL_CONSISTENCY,
245               "should propery convert with serial consistency flag");
246
247    assert_eq!(QueryFlags::WithDefaultTimestamp.as_byte(),
248               WITH_DEFAULT_TIMESTAMP,
249               "should propery convert with default timestamp flag");
250
251    assert_eq!(QueryFlags::WithNamesForValues.as_byte(),
252               WITH_NAME_FOR_VALUES,
253               "should propery convert with name for values flag");
254  }
255}