rcfe_core/
lib.rs

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