aerospike-core 2.1.0

Aerospike Client for Rust
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
// Copyright 2015-2020 Aerospike, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Bit operations. Create bit operations used by client operate command.
//!
//! Offset orientation is left-to-right. Negative offsets are supported.
//! If the offset is negative, the offset starts backwards from end of the bitmap.
//! If an offset is out of bounds, a parameter error will be returned.
//!
//! Nested CDT operations are supported by optional CTX context arguments. Example:
//!
//! ```
//! use aerospike::operations::bitwise::{resize, BitwiseResizeFlags, BitPolicy};
//! // bin = [[0b00000001, 0b01000010], [0b01011010]]
//! // Resize first bitmap (in a list of bitmaps) to 3 bytes.
//! resize("bin", 3, Some(BitwiseResizeFlags::Default), &BitPolicy::default());
//! // bin result = [[0b00000001, 0b01000010, 0b00000000], [0b01011010]]
//! ```

use std::sync::Arc;

use crate::msgpack::encoder::pack_cdt_bit_op;
use crate::operations::cdt::{CdtArgument, CdtOperation};
use crate::operations::cdt_context::DEFAULT_CTX;
use crate::operations::{Operation, OperationBin, OperationData, OperationType};
use crate::Value;

#[derive(Debug, Clone, Copy)]
pub(crate) enum CdtBitwiseOpType {
    Resize = 0,
    Insert = 1,
    Remove = 2,
    Set = 3,
    Or = 4,
    Xor = 5,
    And = 6,
    Not = 7,
    LShift = 8,
    RShift = 9,
    Add = 10,
    Subtract = 11,
    SetInt = 12,
    Get = 50,
    Count = 51,
    LScan = 52,
    RScan = 53,
    GetInt = 54,
}

/// [`BitwiseResizeFlags`] specifies the bitwise operation flags for resize.
#[derive(Debug, Clone)]
pub enum BitwiseResizeFlags {
    /// Default specifies the defalt flag.
    Default = 0,
    /// `FromFront` Adds/removes bytes from the beginning instead of the end.
    FromFront = 1,
    /// `GrowOnly` will only allow the byte[] size to increase.
    GrowOnly = 2,
    /// `ShrinkOnly` will only allow the byte[] size to decrease.
    ShrinkOnly = 4,
}

/// [`BitwiseWriteFlags`] specify bitwise operation policy write flags.
#[derive(Debug, Clone)]
pub enum BitwiseWriteFlags {
    /// Default allows create or update.
    Default = 0,
    /// `CreateOnly` specifies that:
    /// If the bin already exists, the operation will be denied.
    /// If the bin does not exist, a new bin will be created.
    CreateOnly = 1,
    /// `UpdateOnly` specifies that:
    /// If the bin already exists, the bin will be overwritten.
    /// If the bin does not exist, the operation will be denied.
    UpdateOnly = 2,
    /// `NoFail` specifies not to raise error if operation is denied.
    NoFail = 4,
    /// Partial allows other valid operations to be committed if this operations is
    /// denied due to flag constraints.
    Partial = 8,
}

/// [`BitwiseOverflowActions`] specifies the action to take when bitwise add/subtract results in overflow/underflow.
#[derive(Debug, Clone)]
pub enum BitwiseOverflowActions {
    /// Fail specifies to fail operation with error.
    Fail = 0,
    /// Saturate specifies that in add/subtract overflows/underflows, set to max/min value.
    /// Example: MAXINT + 1 = MAXINT
    Saturate = 2,
    /// Wrap specifies that in add/subtract overflows/underflows, wrap the value.
    /// Example: MAXINT + 1 = -1
    Wrap = 4,
}
/// `BitPolicy` determines the Bit operation policy.
#[derive(Debug, Clone, Copy)]
pub struct BitPolicy {
    /// The flags determined by `CdtBitwiseWriteFlags`
    pub flags: u8,
}

impl BitPolicy {
    /// Creates a new `BitPolicy` with defined `CdtBitwiseWriteFlags`
    pub const fn new(flags: u8) -> Self {
        BitPolicy { flags }
    }
}

impl Default for BitPolicy {
    /// Returns the default `BitPolicy`
    fn default() -> Self {
        BitPolicy::new(BitwiseWriteFlags::Default as u8)
    }
}

/// Creates byte "resize" operation.
/// Server resizes byte[] to byteSize according to resizeFlags.
/// Server does not return a value.
///
/// Example:
/// ```text
/// bin = [0b00000001, 0b01000010]
/// byteSize = 4
/// resizeFlags = 0
/// bin result = [0b00000001, 0b01000010, 0b00000000, 0b00000000]
/// ```
pub fn resize(
    bin: &str,
    byte_size: i64,
    resize_flags: Option<BitwiseResizeFlags>,
    policy: &BitPolicy,
) -> Operation {
    let mut args = vec![CdtArgument::Int(byte_size), CdtArgument::Byte(policy.flags)];
    if let Some(resize_flags) = resize_flags {
        args.push(CdtArgument::Byte(resize_flags as u8));
    }
    let cdt_op = CdtOperation {
        op: CdtBitwiseOpType::Resize as u8,
        encoder: Arc::new(pack_cdt_bit_op),
        args,
    };
    Operation {
        op: OperationType::BitWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin.into()),
        data: OperationData::CdtBitOp(cdt_op),
    }
}

/// Creates byte "insert" operation.
/// Server inserts value bytes into byte[] bin at byteOffset.
/// Server does not return a value.
///
/// Example:
/// ```text
/// bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101]
/// byteOffset = 1
/// value = [0b11111111, 0b11000111]
/// bin result = [0b00000001, 0b11111111, 0b11000111, 0b01000010, 0b00000011, 0b00000100, 0b00000101]
/// ```
pub fn insert(bin: &str, byte_offset: i64, value: Value, policy: &BitPolicy) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtBitwiseOpType::Insert as u8,
        encoder: Arc::new(pack_cdt_bit_op),
        args: vec![
            CdtArgument::Int(byte_offset),
            CdtArgument::Value(value),
            CdtArgument::Byte(policy.flags),
        ],
    };

    Operation {
        op: OperationType::BitWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin.into()),
        data: OperationData::CdtBitOp(cdt_op),
    }
}

/// Creates byte "remove" operation.
/// Server removes bytes from byte[] bin at byteOffset for byteSize.
/// Server does not return a value.
///
/// Example:
/// ```text
/// bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101]
/// byteOffset = 2
/// byteSize = 3
/// bin result = [0b00000001, 0b01000010]
/// ```
pub fn remove(bin: &str, byte_offset: i64, byte_size: i64, policy: &BitPolicy) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtBitwiseOpType::Remove as u8,
        encoder: Arc::new(pack_cdt_bit_op),
        args: vec![
            CdtArgument::Int(byte_offset),
            CdtArgument::Int(byte_size),
            CdtArgument::Byte(policy.flags),
        ],
    };

    Operation {
        op: OperationType::BitWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin.into()),
        data: OperationData::CdtBitOp(cdt_op),
    }
}

/// Creates bit "set" operation.
/// Server sets value on byte[] bin at bitOffset for bitSize.
/// Server does not return a value.
///
/// Example:
/// ```text
/// bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101]
/// bitOffset = 13
/// bitSize = 3
/// value = [0b11100000]
/// bin result = [0b00000001, 0b01000111, 0b00000011, 0b00000100, 0b00000101]
/// ```
pub fn set(
    bin: &str,
    bit_offset: i64,
    bit_size: i64,
    value: Value,
    policy: &BitPolicy,
) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtBitwiseOpType::Set as u8,
        encoder: Arc::new(pack_cdt_bit_op),
        args: vec![
            CdtArgument::Int(bit_offset),
            CdtArgument::Int(bit_size),
            CdtArgument::Value(value),
            CdtArgument::Byte(policy.flags),
        ],
    };

    Operation {
        op: OperationType::BitWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin.into()),
        data: OperationData::CdtBitOp(cdt_op),
    }
}

/// Creates bit "or" operation.
/// Server performs bitwise "or" on value and byte[] bin at bitOffset for bitSize.
/// Server does not return a value.
///
/// Example:
/// ```text
/// bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101]
/// bitOffset = 17
/// bitSize = 6
/// value = [0b10101000]
/// bin result = [0b00000001, 0b01000010, 0b01010111, 0b00000100, 0b00000101]
/// ```
pub fn or(
    bin: &str,
    bit_offset: i64,
    bit_size: i64,
    value: Value,
    policy: &BitPolicy,
) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtBitwiseOpType::Or as u8,
        encoder: Arc::new(pack_cdt_bit_op),
        args: vec![
            CdtArgument::Int(bit_offset),
            CdtArgument::Int(bit_size),
            CdtArgument::Value(value),
            CdtArgument::Byte(policy.flags),
        ],
    };

    Operation {
        op: OperationType::BitWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin.into()),
        data: OperationData::CdtBitOp(cdt_op),
    }
}

/// Creates bit "exclusive or" operation.
/// Server performs bitwise "xor" on value and byte[] bin at bitOffset for bitSize.
/// Server does not return a value.
///
/// Example:
/// ```text
/// bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101]
/// bitOffset = 17
/// bitSize = 6
/// value = [0b10101100]
/// bin result = [0b00000001, 0b01000010, 0b01010101, 0b00000100, 0b00000101]
/// ```
pub fn xor(
    bin: &str,
    bit_offset: i64,
    bit_size: i64,
    value: Value,
    policy: &BitPolicy,
) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtBitwiseOpType::Xor as u8,
        encoder: Arc::new(pack_cdt_bit_op),
        args: vec![
            CdtArgument::Int(bit_offset),
            CdtArgument::Int(bit_size),
            CdtArgument::Value(value),
            CdtArgument::Byte(policy.flags),
        ],
    };

    Operation {
        op: OperationType::BitWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin.into()),
        data: OperationData::CdtBitOp(cdt_op),
    }
}

/// Creates bit "and" operation.
/// Server performs bitwise "and" on value and byte[] bin at bitOffset for bitSize.
/// Server does not return a value.
///
/// Example:
/// ```text
/// bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101]
/// bitOffset = 23
/// bitSize = 9
/// value = [0b00111100, 0b10000000]
/// bin result = [0b00000001, 0b01000010, 0b00000010, 0b00000000, 0b00000101]
/// ```
pub fn and(
    bin: &str,
    bit_offset: i64,
    bit_size: i64,
    value: Value,
    policy: &BitPolicy,
) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtBitwiseOpType::And as u8,
        encoder: Arc::new(pack_cdt_bit_op),
        args: vec![
            CdtArgument::Int(bit_offset),
            CdtArgument::Int(bit_size),
            CdtArgument::Value(value),
            CdtArgument::Byte(policy.flags),
        ],
    };

    Operation {
        op: OperationType::BitWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin.into()),
        data: OperationData::CdtBitOp(cdt_op),
    }
}

/// Creates bit "not" operation.
/// Server negates byte[] bin starting at bitOffset for bitSize.
/// Server does not return a value.
///
/// Example:
/// ```text
/// bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101]
/// bitOffset = 25
/// bitSize = 6
/// bin result = [0b00000001, 0b01000010, 0b00000011, 0b01111010, 0b00000101]
/// ```
pub fn not(bin: &str, bit_offset: i64, bit_size: i64, policy: &BitPolicy) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtBitwiseOpType::Not as u8,
        encoder: Arc::new(pack_cdt_bit_op),
        args: vec![
            CdtArgument::Int(bit_offset),
            CdtArgument::Int(bit_size),
            CdtArgument::Byte(policy.flags),
        ],
    };

    Operation {
        op: OperationType::BitWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin.into()),
        data: OperationData::CdtBitOp(cdt_op),
    }
}

/// Creates bit "left shift" operation.
/// Server shifts left byte[] bin starting at bitOffset for bitSize.
/// Server does not return a value.
///
/// Example:
/// ```text
/// bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101]
/// bitOffset = 32
/// bitSize = 8
/// shift = 3
/// bin result = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00101000]
/// ```
pub fn lshift(
    bin: &str,
    bit_offset: i64,
    bit_size: i64,
    shift: i64,
    policy: &BitPolicy,
) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtBitwiseOpType::LShift as u8,
        encoder: Arc::new(pack_cdt_bit_op),
        args: vec![
            CdtArgument::Int(bit_offset),
            CdtArgument::Int(bit_size),
            CdtArgument::Int(shift),
            CdtArgument::Byte(policy.flags),
        ],
    };

    Operation {
        op: OperationType::BitWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin.into()),
        data: OperationData::CdtBitOp(cdt_op),
    }
}

/// Creates bit "right shift" operation.
/// Server shifts right byte[] bin starting at bitOffset for bitSize.
/// Server does not return a value.
///
/// Example:
/// ```text
/// bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101]
/// bitOffset = 0
/// bitSize = 9
/// shift = 1
/// bin result = [0b00000000, 0b11000010, 0b00000011, 0b00000100, 0b00000101]
/// ```
pub fn rshift(
    bin: &str,
    bit_offset: i64,
    bit_size: i64,
    shift: i64,
    policy: &BitPolicy,
) -> Operation {
    let cdt_op = CdtOperation {
        encoder: Arc::new(pack_cdt_bit_op),
        op: CdtBitwiseOpType::RShift as u8,
        args: vec![
            CdtArgument::Int(bit_offset),
            CdtArgument::Int(bit_size),
            CdtArgument::Int(shift),
            CdtArgument::Byte(policy.flags),
        ],
    };

    Operation {
        op: OperationType::BitWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin.into()),
        data: OperationData::CdtBitOp(cdt_op),
    }
}

/// Creates bit "add" operation.
///
/// Server adds value to byte[] bin starting at bitOffset for bitSize. `BitSize` must be <= 64.
/// Signed indicates if bits should be treated as a signed number.
/// If add overflows/underflows, `CdtBitwiseOverflowAction` is used.
/// Server does not return a value.
///
/// Example:
/// ```text
/// bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101]
/// bitOffset = 24
/// bitSize = 16
/// value = 128
/// signed = false
/// bin result = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b10000101]
/// ```
pub fn add(
    bin: &str,
    bit_offset: i64,
    bit_size: i64,
    value: i64,
    signed: bool,
    action: BitwiseOverflowActions,
    policy: &BitPolicy,
) -> Operation {
    let mut action_flags = action as u8;
    if signed {
        action_flags |= 1;
    }

    let cdt_op = CdtOperation {
        op: CdtBitwiseOpType::Add as u8,
        encoder: Arc::new(pack_cdt_bit_op),
        args: vec![
            CdtArgument::Int(bit_offset),
            CdtArgument::Int(bit_size),
            CdtArgument::Int(value),
            CdtArgument::Byte(policy.flags),
            CdtArgument::Byte(action_flags),
        ],
    };

    Operation {
        op: OperationType::BitWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin.into()),
        data: OperationData::CdtBitOp(cdt_op),
    }
}

/// Creates bit "subtract" operation.
///
/// Server subtracts value from byte[] bin starting at bitOffset for bitSize. `bit_size` must be <= 64.
/// Signed indicates if bits should be treated as a signed number.
/// If add overflows/underflows, `CdtBitwiseOverflowAction` is used.
/// Server does not return a value.
///
/// Example:
/// ```text
/// bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101]
/// bitOffset = 24
/// bitSize = 16
/// value = 128
/// signed = false
/// bin result = [0b00000001, 0b01000010, 0b00000011, 0b0000011, 0b10000101]
/// ```
pub fn subtract(
    bin: &str,
    bit_offset: i64,
    bit_size: i64,
    value: i64,
    signed: bool,
    action: BitwiseOverflowActions,
    policy: &BitPolicy,
) -> Operation {
    let mut action_flags = action as u8;
    if signed {
        action_flags |= 1;
    }

    let cdt_op = CdtOperation {
        op: CdtBitwiseOpType::Subtract as u8,
        encoder: Arc::new(pack_cdt_bit_op),
        args: vec![
            CdtArgument::Int(bit_offset),
            CdtArgument::Int(bit_size),
            CdtArgument::Int(value),
            CdtArgument::Byte(policy.flags),
            CdtArgument::Byte(action_flags),
        ],
    };

    Operation {
        op: OperationType::BitWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin.into()),
        data: OperationData::CdtBitOp(cdt_op),
    }
}

/// Creates bit "setInt" operation.
/// Server sets value to byte[] bin starting at bitOffset for bitSize. Size must be <= 64.
/// Server does not return a value.
///
/// Example:
/// ```text
/// bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101]
/// bitOffset = 1
/// bitSize = 8
/// value = 127
/// bin result = [0b00111111, 0b11000010, 0b00000011, 0b0000100, 0b00000101]
/// ```
pub fn set_int(
    bin: &str,
    bit_offset: i64,
    bit_size: i64,
    value: i64,
    policy: &BitPolicy,
) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtBitwiseOpType::SetInt as u8,
        encoder: Arc::new(pack_cdt_bit_op),
        args: vec![
            CdtArgument::Int(bit_offset),
            CdtArgument::Int(bit_size),
            CdtArgument::Int(value),
            CdtArgument::Byte(policy.flags),
        ],
    };

    Operation {
        op: OperationType::BitWrite,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin.into()),
        data: OperationData::CdtBitOp(cdt_op),
    }
}

/// Creates bit "get" operation.
/// Server returns bits from byte[] bin starting at bitOffset for bitSize.
///
/// Example:
/// ```text
/// bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101]
/// bitOffset = 9
/// bitSize = 5
/// returns [0b1000000]
/// ```
pub fn get(bin: &str, bit_offset: i64, bit_size: i64) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtBitwiseOpType::Get as u8,
        encoder: Arc::new(pack_cdt_bit_op),
        args: vec![CdtArgument::Int(bit_offset), CdtArgument::Int(bit_size)],
    };

    Operation {
        op: OperationType::BitRead,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin.into()),
        data: OperationData::CdtBitOp(cdt_op),
    }
}

/// Creates bit "count" operation.
/// Server returns integer count of set bits from byte[] bin starting at bitOffset for bitSize.
///
/// Example:
/// ```text
/// bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101]
/// bitOffset = 20
/// bitSize = 4
/// returns 2
/// ```
pub fn count(bin: &str, bit_offset: i64, bit_size: i64) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtBitwiseOpType::Count as u8,
        encoder: Arc::new(pack_cdt_bit_op),
        args: vec![CdtArgument::Int(bit_offset), CdtArgument::Int(bit_size)],
    };

    Operation {
        op: OperationType::BitRead,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin.into()),
        data: OperationData::CdtBitOp(cdt_op),
    }
}

/// Creates bit "left scan" operation.
/// Server returns integer bit offset of the first specified value bit in byte[] bin
/// starting at bitOffset for bitSize.
///
/// Example:
/// ```text
/// bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101]
/// bitOffset = 24
/// bitSize = 8
/// value = true
/// returns 5
/// ```
pub fn lscan(bin: &str, bit_offset: i64, bit_size: i64, value: bool) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtBitwiseOpType::LScan as u8,
        encoder: Arc::new(pack_cdt_bit_op),
        args: vec![
            CdtArgument::Int(bit_offset),
            CdtArgument::Int(bit_size),
            CdtArgument::Bool(value),
        ],
    };

    Operation {
        op: OperationType::BitRead,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin.into()),
        data: OperationData::CdtBitOp(cdt_op),
    }
}

/// Creates bit "right scan" operation.
/// Server returns integer bit offset of the last specified value bit in byte[] bin
/// starting at bitOffset for bitSize.
///
/// Example:
/// ```text
/// bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101]
/// bitOffset = 32
/// bitSize = 8
/// value = true
/// returns 7
/// ```
pub fn rscan(bin: &str, bit_offset: i64, bit_size: i64, value: bool) -> Operation {
    let cdt_op = CdtOperation {
        op: CdtBitwiseOpType::RScan as u8,
        encoder: Arc::new(pack_cdt_bit_op),
        args: vec![
            CdtArgument::Int(bit_offset),
            CdtArgument::Int(bit_size),
            CdtArgument::Bool(value),
        ],
    };

    Operation {
        op: OperationType::BitRead,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin.into()),
        data: OperationData::CdtBitOp(cdt_op),
    }
}

/// Creates bit "get integer" operation.
/// Server returns integer from byte[] bin starting at bitOffset for bitSize.
/// Signed indicates if bits should be treated as a signed number.
///
/// Example:
/// ```text
/// bin = [0b00000001, 0b01000010, 0b00000011, 0b00000100, 0b00000101]
/// bitOffset = 8
/// bitSize = 16
/// signed = false
/// returns 16899
/// ```
pub fn get_int(bin: &str, bit_offset: i64, bit_size: i64, signed: bool) -> Operation {
    let mut args = vec![CdtArgument::Int(bit_offset), CdtArgument::Int(bit_size)];
    if signed {
        args.push(CdtArgument::Byte(1));
    }
    let cdt_op = CdtOperation {
        op: CdtBitwiseOpType::GetInt as u8,
        encoder: Arc::new(pack_cdt_bit_op),
        args,
    };

    Operation {
        op: OperationType::BitRead,
        ctx: DEFAULT_CTX,
        bin: OperationBin::Name(bin.into()),
        data: OperationData::CdtBitOp(cdt_op),
    }
}