wasm-dbms-api 0.8.1

Runtime-agnostic API types and traits for the wasm-dbms DBMS engine.
Documentation
//! Autoincrement type and related functions.

/// Represents an autoincrement value, which can either be automatically generated or explicitly provided.
///
/// This is used when providing values in [`crate::prelude::InsertRecord`] if a column is defined as autoincrement.
/// If the value is set to [`Autoincrement::Auto`], the DBMS will automatically generate the next value for that column.
/// If it is set to [`Autoincrement::Value`], the provided value will be used instead.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Autoincrement<T>
where
    T: std::fmt::Debug + Clone + PartialEq + Eq,
{
    /// Indicates that the value should be automatically generated by the DBMS.
    Auto,
    /// Indicates that the provided value should be used for the autoincrement column.
    ///
    /// Once inserted, the next value for the autoincrement column will be `provided value + 1`.
    Value(T),
}

impl<T> Autoincrement<T>
where
    T: std::fmt::Debug + Clone + PartialEq + Eq,
{
    /// Converts the `Autoincrement` value into an [`Option<T>`], where [`Autoincrement::Auto`] is represented as [`Option::None`],
    /// and [`Autoincrement::Value`] is represented as [`Option::Some`].
    pub fn into_option(self) -> Option<T> {
        match self {
            Autoincrement::Auto => None,
            Autoincrement::Value(v) => Some(v),
        }
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn test_autoincrement_into_option() {
        let auto = Autoincrement::<i32>::Auto;
        let value = Autoincrement::Value(42);

        assert_eq!(auto.into_option(), None);
        assert_eq!(value.into_option(), Some(42));
    }
}