cdrs_async/query/
query_flags.rs

1use cassandra_proto::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!(
123      QueryFlags::has_value(FLAGS_VALUE | 0x10),
124      "should show that the flag has value"
125    );
126    assert!(
127      !QueryFlags::has_value(FLAGS_SKIP_METADATA),
128      "should show that the flag does NOT have value"
129    );
130  }
131
132  #[test]
133  fn set_value_test() {
134    assert_eq!(
135      QueryFlags::set_value(0),
136      FLAGS_VALUE,
137      "should set has value flag"
138    );
139  }
140
141  #[test]
142  fn has_skip_metadata_test() {
143    assert!(
144      QueryFlags::has_skip_metadata(FLAGS_SKIP_METADATA | 0x10),
145      "should show that the flag has skip metadata"
146    );
147    assert!(
148      !QueryFlags::has_skip_metadata(FLAGS_VALUE),
149      "should show that the flag does NOT have skip metadata"
150    );
151  }
152
153  #[test]
154  fn set_skip_metadata_test() {
155    assert_eq!(
156      QueryFlags::set_skip_metadata(0),
157      FLAGS_SKIP_METADATA,
158      "should set has skip metadata flag"
159    );
160  }
161
162  #[test]
163  fn has_page_size_test() {
164    assert!(
165      QueryFlags::has_page_size(WITH_PAGE_SIZE | 0x10),
166      "should show that the flag has with page size"
167    );
168    assert!(
169      !QueryFlags::has_page_size(FLAGS_VALUE),
170      "should show that the flag does NOT have with page size"
171    );
172  }
173
174  #[test]
175  fn set_page_size_test() {
176    assert_eq!(
177      QueryFlags::set_page_size(0),
178      WITH_PAGE_SIZE,
179      "should set has page size flag"
180    );
181  }
182
183  #[test]
184  fn has_with_paging_state_test() {
185    assert!(
186      QueryFlags::has_with_paging_state(WITH_PAGING_STATE | 0x10),
187      "should show that the flag has with paging state"
188    );
189    assert!(
190      !QueryFlags::has_with_paging_state(FLAGS_VALUE),
191      "should show that the flag does NOT have with paging state"
192    );
193  }
194
195  #[test]
196  fn set_with_paging_state_test() {
197    assert_eq!(
198      QueryFlags::set_with_paging_state(0),
199      WITH_PAGING_STATE,
200      "should set has with paging state flag"
201    );
202  }
203
204  #[test]
205  fn has_with_serial_consistency_test() {
206    assert!(
207      QueryFlags::has_with_serial_consistency(WITH_SERIAL_CONSISTENCY | 0x11),
208      "should show that the flag has with serial consistency"
209    );
210    assert!(
211      !QueryFlags::has_with_serial_consistency(FLAGS_VALUE),
212      "should show that the flag does NOT have with serial consistency"
213    );
214  }
215
216  #[test]
217  fn set_with_serial_consistency_test() {
218    assert_eq!(
219      QueryFlags::set_with_serial_consistency(0),
220      WITH_SERIAL_CONSISTENCY,
221      "should set has with serial consistency flag"
222    );
223  }
224
225  #[test]
226  fn has_with_default_timestamp_test() {
227    assert!(
228      QueryFlags::has_with_default_timestamp(WITH_DEFAULT_TIMESTAMP | 0x10),
229      "should show that the flag has with default timestamp"
230    );
231    assert!(
232      !QueryFlags::has_with_default_timestamp(FLAGS_VALUE),
233      "should show that the flag does NOT have with default timestamp"
234    );
235  }
236
237  #[test]
238  fn set_with_default_timestamp_test() {
239    assert_eq!(
240      QueryFlags::set_with_default_timestamp(0),
241      WITH_DEFAULT_TIMESTAMP,
242      "should set has with serial consistency flag"
243    );
244  }
245
246  #[test]
247  fn has_with_names_for_values_test() {
248    assert!(
249      QueryFlags::has_with_names_for_values(WITH_NAME_FOR_VALUES | 0x10),
250      "should show that the flag has with name for values"
251    );
252    assert!(
253      !QueryFlags::has_with_names_for_values(FLAGS_VALUE),
254      "should show that the flag does NOT have with name for values"
255    );
256  }
257
258  #[test]
259  fn set_with_names_for_values_test() {
260    assert_eq!(
261      QueryFlags::set_with_names_for_values(0),
262      WITH_NAME_FOR_VALUES,
263      "should set has with name for values flag"
264    );
265  }
266
267  #[test]
268  fn as_byte_test() {
269    assert_eq!(
270      QueryFlags::Value.as_byte(),
271      FLAGS_VALUE,
272      "should propery convert values flag"
273    );
274
275    assert_eq!(
276      QueryFlags::SkipMetadata.as_byte(),
277      FLAGS_SKIP_METADATA,
278      "should propery convert skip metadata flag"
279    );
280
281    assert_eq!(
282      QueryFlags::PageSize.as_byte(),
283      WITH_PAGE_SIZE,
284      "should propery convert with page size flag"
285    );
286
287    assert_eq!(
288      QueryFlags::WithPagingState.as_byte(),
289      WITH_PAGING_STATE,
290      "should propery convert with paging state flag"
291    );
292
293    assert_eq!(
294      QueryFlags::WithSerialConsistency.as_byte(),
295      WITH_SERIAL_CONSISTENCY,
296      "should propery convert with serial consistency flag"
297    );
298
299    assert_eq!(
300      QueryFlags::WithDefaultTimestamp.as_byte(),
301      WITH_DEFAULT_TIMESTAMP,
302      "should propery convert with default timestamp flag"
303    );
304
305    assert_eq!(
306      QueryFlags::WithNamesForValues.as_byte(),
307      WITH_NAME_FOR_VALUES,
308      "should propery convert with name for values flag"
309    );
310  }
311}