use serde::{Deserialize, Serialize};
use crate::IdType;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DbConfig {
pub id_type: IdType,
pub id_key: String,
}
impl Default for DbConfig {
fn default() -> Self {
Self {
id_type: Default::default(),
id_key: "id".to_string(),
}
}
}
impl DbConfig {
pub fn new() -> Self {
Self::default()
}
pub fn from(id_type: IdType, id_key: &str) -> Self {
Self {
id_type,
id_key: id_key.to_string(),
}
}
pub fn int(id_key: &str) -> Self {
Self {
id_type: IdType::Int,
id_key: id_key.to_string(),
}
}
pub fn uuid(id_key: &str) -> Self {
Self {
id_type: IdType::Uuid,
id_key: id_key.to_string(),
}
}
pub fn none(id_key: &str) -> Self {
Self {
id_type: IdType::None,
id_key: id_key.to_string(),
}
}
}
#[cfg(test)]
mod tests {
use super::DbConfig;
use crate::IdType;
#[test]
fn constructors_set_id_type_and_key() {
assert_eq!(
DbConfig::new(),
DbConfig {
id_type: IdType::Uuid,
id_key: "id".to_string(),
}
);
assert_eq!(
DbConfig::from(IdType::Int, "row_id"),
DbConfig {
id_type: IdType::Int,
id_key: "row_id".to_string(),
}
);
assert_eq!(
DbConfig::int("number"),
DbConfig {
id_type: IdType::Int,
id_key: "number".to_string(),
}
);
assert_eq!(
DbConfig::uuid("uuid"),
DbConfig {
id_type: IdType::Uuid,
id_key: "uuid".to_string(),
}
);
assert_eq!(
DbConfig::none("external_id"),
DbConfig {
id_type: IdType::None,
id_key: "external_id".to_string(),
}
);
}
}