Skip to main content

noxu_db/
put.rs

1//! Put operation types.
2//!
3//! Put operation mode enum.
4
5/// Type of put operation for cursors and databases.
6///
7/// Specifies how to insert or update records.
8///
9/// Put operation mode enum.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub enum Put {
12    /// Insert if the key doesn't exist, else return error.
13    ///
14    /// For non-duplicate databases, returns an error if the key exists.
15    /// For duplicate databases, returns an error if the key/data pair exists.
16    NoOverwrite,
17
18    /// Insert or update (default behavior).
19    ///
20    /// For non-duplicate databases, inserts if the key doesn't exist, or
21    /// replaces the data if the key exists. For duplicate databases, inserts
22    /// a new duplicate.
23    Overwrite,
24
25    /// Insert if key doesn't exist, else do nothing.
26    ///
27    /// Similar to NoOverwrite, but returns success (with no update) if the
28    /// key already exists, rather than an error. For duplicate databases,
29    /// returns success if the key/data pair exists.
30    NoDupData,
31
32    /// Update the record at the current cursor position.
33    ///
34    /// Replaces the data at the current cursor position. The key cannot be
35    /// changed. Returns an error if the cursor is not positioned.
36    Current,
37}
38
39impl Put {
40    /// Returns whether this operation allows overwriting existing records.
41    pub fn allows_overwrite(&self) -> bool {
42        matches!(self, Put::Overwrite | Put::Current)
43    }
44
45    /// Returns whether this operation returns an error if the record exists.
46    pub fn errors_if_exists(&self) -> bool {
47        matches!(self, Put::NoOverwrite)
48    }
49
50    /// Returns whether this operation requires the cursor to be positioned.
51    pub fn requires_positioned_cursor(&self) -> bool {
52        matches!(self, Put::Current)
53    }
54
55    /// Returns whether this operation prevents duplicate data.
56    pub fn prevents_duplicates(&self) -> bool {
57        matches!(self, Put::NoDupData)
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn test_allows_overwrite() {
67        assert!(Put::Overwrite.allows_overwrite());
68        assert!(Put::Current.allows_overwrite());
69        assert!(!Put::NoOverwrite.allows_overwrite());
70        assert!(!Put::NoDupData.allows_overwrite());
71    }
72
73    #[test]
74    fn test_errors_if_exists() {
75        assert!(Put::NoOverwrite.errors_if_exists());
76        assert!(!Put::Overwrite.errors_if_exists());
77        assert!(!Put::NoDupData.errors_if_exists());
78        assert!(!Put::Current.errors_if_exists());
79    }
80
81    #[test]
82    fn test_requires_positioned_cursor() {
83        assert!(Put::Current.requires_positioned_cursor());
84        assert!(!Put::Overwrite.requires_positioned_cursor());
85        assert!(!Put::NoOverwrite.requires_positioned_cursor());
86        assert!(!Put::NoDupData.requires_positioned_cursor());
87    }
88
89    #[test]
90    fn test_prevents_duplicates() {
91        assert!(Put::NoDupData.prevents_duplicates());
92        assert!(!Put::Overwrite.prevents_duplicates());
93        assert!(!Put::NoOverwrite.prevents_duplicates());
94        assert!(!Put::Current.prevents_duplicates());
95    }
96
97    #[test]
98    fn test_equality() {
99        assert_eq!(Put::NoOverwrite, Put::NoOverwrite);
100        assert_ne!(Put::NoOverwrite, Put::Overwrite);
101    }
102
103    #[test]
104    fn test_clone() {
105        let put1 = Put::Current;
106        let put2 = put1;
107        assert_eq!(put1, put2);
108    }
109
110    #[test]
111    fn test_copy() {
112        let put1 = Put::Overwrite;
113        let put2 = put1;
114        assert_eq!(put1, put2);
115    }
116
117    #[test]
118    fn test_debug() {
119        let put = Put::NoDupData;
120        let debug = format!("{:?}", put);
121        assert_eq!(debug, "NoDupData");
122    }
123
124    #[test]
125    fn test_all_variants() {
126        let variants =
127            [Put::NoOverwrite, Put::Overwrite, Put::NoDupData, Put::Current];
128        // Just ensure all variants are created successfully
129        assert_eq!(variants.len(), 4);
130    }
131}