toasty_core/schema/app/
auto.rs

1/// Strategy for automatically populating a field's value on insert.
2///
3/// When a field has an `AutoStrategy`, Toasty generates the value
4/// automatically when a new record is created, rather than requiring the
5/// caller to supply it.
6///
7/// # Examples
8///
9/// ```
10/// use toasty_core::schema::app::{AutoStrategy, UuidVersion};
11///
12/// let strategy = AutoStrategy::Uuid(UuidVersion::V4);
13/// assert!(!strategy.is_increment());
14///
15/// let inc = AutoStrategy::Increment;
16/// assert!(inc.is_increment());
17/// ```
18#[derive(Debug, Clone)]
19pub enum AutoStrategy {
20    /// Generate a UUID of the specified version.
21    Uuid(UuidVersion),
22    /// Use an auto-incrementing integer sequence (database-assigned).
23    Increment,
24}
25
26/// UUID version to use for auto-generated UUID fields.
27///
28/// # Examples
29///
30/// ```
31/// use toasty_core::schema::app::UuidVersion;
32///
33/// let v4 = UuidVersion::V4;
34/// let v7 = UuidVersion::V7;
35/// ```
36#[derive(Debug, Clone)]
37pub enum UuidVersion {
38    /// Random UUID (version 4).
39    V4,
40    /// Time-ordered UUID (version 7).
41    V7,
42}
43
44impl AutoStrategy {
45    /// Returns `true` if this strategy is [`AutoStrategy::Increment`].
46    ///
47    /// # Examples
48    ///
49    /// ```
50    /// use toasty_core::schema::app::AutoStrategy;
51    ///
52    /// let inc = AutoStrategy::Increment;
53    /// assert!(inc.is_increment());
54    /// ```
55    #[must_use]
56    pub fn is_increment(&self) -> bool {
57        matches!(self, Self::Increment)
58    }
59}