rcfe_core/
lib.rs

1pub mod client;
2pub mod error;
3pub mod factory;
4pub mod kv;
5pub mod options;
6pub mod txn;
7
8pub mod etcdserverpb {
9    tonic::include_proto!("etcdserverpb");
10}
11
12pub mod mvccpb {
13    tonic::include_proto!("mvccpb");
14}
15
16pub mod authpb {
17    tonic::include_proto!("authpb");
18}
19
20pub mod v3electionpb {
21    tonic::include_proto!("v3electionpb");
22}
23
24pub mod v3lockpb {
25    tonic::include_proto!("v3lockpb");
26}
27
28/// A struct representing a sequence of bytes.
29const EMPTY_BYTE_SEQUENCE_VALUE: Vec<u8> = Vec::new();
30const MAX_BYTE: u8 = 0xFF; // Maximum byte value
31const INCREMENT_BYTE: u8 = 0x01; // Increment by 1
32
33/// A struct representing a sequence of bytes.
34/// It provides methods to create an empty sequence and compute the next lexicographical sequence.
35/// # Examples
36/// ```rust
37/// let seq = ByteSequence::from("abc");
38/// let next_seq = seq.next();
39/// assert_eq!(next_seq.as_bytes(), b"abd");
40/// ```
41#[derive(Eq, PartialEq, Clone, Debug)]
42pub struct ByteSequence {
43    bytes: Vec<u8>, // A vector to hold the byte sequence
44}
45
46impl ByteSequence {
47    pub fn as_bytes(&self) -> &[u8] {
48        &self.bytes
49    }
50
51    /// Creates an empty ByteSequence.
52    /// # Examples
53    /// ```rust
54    /// let empty_seq = ByteSequence::empty();
55    /// assert_eq!(empty_seq.as_bytes(), b"\0");
56    /// ```
57    pub fn empty() -> Self {
58        ByteSequence {
59            bytes: EMPTY_BYTE_SEQUENCE_VALUE,
60        }
61    }
62
63    /// Computes the next lexicographical byte sequence.
64    /// If the current sequence is empty, it returns an empty sequence.
65    /// If all bytes are at their maximum value, it returns an empty sequence.
66    /// If the last byte can be incremented, it increments it and truncates the sequence.
67    /// # Examples
68    ///
69    /// - Basic test case
70    ///
71    /// ```rust
72    /// let from = ByteSequence::from("abc");
73    /// let next_seq = from.next();
74    /// assert_eq!(next_seq, ByteSequence::from("abd"));
75    /// ```
76    ///
77    /// - Test case with `0xff` bytes
78    ///
79    /// ```rust
80    /// let from = ByteSequence::from(b"ab\xff\xff" as &[u8]); // [0x61,0x62,0xff,0xff]
81    /// let next_seq = from.next();
82    /// assert_eq!(next_seq.as_bytes(), b"ac");
83    /// ```
84    ///
85    /// - Test case where all bytes are `0xff`
86    ///
87    /// ```rust
88    /// let from = ByteSequence::from(b"\xff\xff" as &[u8]); // [0xff,0xff]
89    /// let next_seq = from.next();
90    /// assert_eq!(next_seq.as_bytes(), ByteSequence::empty().as_bytes());
91    /// ```
92    pub fn next(&self) -> Self {
93        if self.bytes.is_empty() {
94            return ByteSequence::empty();
95        }
96
97        let mut end = self.bytes.clone();
98        // Increment the last byte
99        for i in (0..end.len()).rev() {
100            if end[i] != MAX_BYTE {
101                end[i] = end[i].wrapping_add(INCREMENT_BYTE); // +1
102                end.truncate(i + 1);
103                return ByteSequence { bytes: end };
104            }
105            // If the byte is MAX_BYTE continue to the previous byte
106        }
107        // If all bytes are MAX_BYTE, return the escape byte
108        ByteSequence::empty()
109    }
110
111    pub fn to_vec(&self) -> Vec<u8> {
112        self.bytes.clone()
113    }
114}
115
116impl From<&str> for ByteSequence {
117    fn from(value: &str) -> Self {
118        ByteSequence {
119            bytes: value.as_bytes().to_vec(),
120        }
121    }
122}
123
124impl From<String> for ByteSequence {
125    fn from(value: String) -> Self {
126        ByteSequence {
127            bytes: value.as_bytes().to_vec(),
128        }
129    }
130}
131
132impl From<Vec<u8>> for ByteSequence {
133    fn from(value: Vec<u8>) -> Self {
134        ByteSequence { bytes: value }
135    }
136}
137
138impl From<&[u8]> for ByteSequence {
139    fn from(value: &[u8]) -> Self {
140        ByteSequence {
141            bytes: value.to_vec(),
142        }
143    }
144}