native_db_32bit/
model.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use crate::db_type::{DatabaseKeyDefinition, DatabaseSecondaryKeyOptions, Error, Result};
use std::collections::HashSet;

#[derive(Clone, Debug)]
pub struct DatabaseModel {
    pub primary_key: DatabaseKeyDefinition<()>,
    pub secondary_keys: HashSet<DatabaseKeyDefinition<DatabaseSecondaryKeyOptions>>,
}

impl DatabaseModel {
    pub fn check_secondary_options<F>(
        &self,
        secondary_key: &DatabaseKeyDefinition<DatabaseSecondaryKeyOptions>,
        check: F,
    ) -> Result<()>
    where
        F: Fn(DatabaseSecondaryKeyOptions) -> bool,
    {
        let key = self
            .secondary_keys
            .get(secondary_key.into())
            .ok_or_else(|| Error::SecondaryKeyDefinitionNotFound {
                table: self.primary_key.unique_table_name.to_string(),
                key: secondary_key.unique_table_name.clone(),
            })?;

        if check(key.options.clone()) {
            Ok(())
        } else {
            Err(Error::SecondaryKeyConstraintMismatch {
                table: self.primary_key.unique_table_name.to_string(),
                key: secondary_key.unique_table_name.clone(),
                got: key.options.clone(),
            })
        }
    }
}