wasm_dbms_api/dbms/autoincrement.rs
1//! Autoincrement type and related functions.
2
3/// Represents an autoincrement value, which can either be automatically generated or explicitly provided.
4///
5/// This is used when providing values in [`crate::prelude::InsertRecord`] if a column is defined as autoincrement.
6/// If the value is set to [`Autoincrement::Auto`], the DBMS will automatically generate the next value for that column.
7/// If it is set to [`Autoincrement::Value`], the provided value will be used instead.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum Autoincrement<T>
10where
11 T: std::fmt::Debug + Clone + PartialEq + Eq,
12{
13 /// Indicates that the value should be automatically generated by the DBMS.
14 Auto,
15 /// Indicates that the provided value should be used for the autoincrement column.
16 ///
17 /// Once inserted, the next value for the autoincrement column will be `provided value + 1`.
18 Value(T),
19}
20
21impl<T> Autoincrement<T>
22where
23 T: std::fmt::Debug + Clone + PartialEq + Eq,
24{
25 /// Converts the `Autoincrement` value into an [`Option<T>`], where [`Autoincrement::Auto`] is represented as [`Option::None`],
26 /// and [`Autoincrement::Value`] is represented as [`Option::Some`].
27 pub fn into_option(self) -> Option<T> {
28 match self {
29 Autoincrement::Auto => None,
30 Autoincrement::Value(v) => Some(v),
31 }
32 }
33}
34
35#[cfg(test)]
36mod tests {
37
38 use super::*;
39
40 #[test]
41 fn test_autoincrement_into_option() {
42 let auto = Autoincrement::<i32>::Auto;
43 let value = Autoincrement::Value(42);
44
45 assert_eq!(auto.into_option(), None);
46 assert_eq!(value.into_option(), Some(42));
47 }
48}