pub mod options;
use crate::bson::Document;
use self::options::*;
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;
#[derive(Clone, Debug, Default, Deserialize, TypedBuilder, Serialize)]
#[builder(field_defaults(default, setter(into)))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct IndexModel {
#[serde(rename = "key")]
pub keys: Document,
#[serde(flatten)]
pub options: Option<IndexOptions>,
}
impl IndexModel {
pub(crate) fn update_name(&mut self) {
if self
.options
.as_ref()
.and_then(|o| o.name.as_ref())
.is_none()
{
let key_names: Vec<String> = self
.keys
.iter()
.map(|(k, v)| format!("{}_{}", k, v))
.collect();
self.options.get_or_insert(IndexOptions::default()).name = Some(key_names.join("_"));
}
}
pub(crate) fn get_name(&self) -> Option<String> {
self.options.as_ref().and_then(|o| o.name.as_ref()).cloned()
}
#[cfg(test)]
pub(crate) fn is_unique(&self) -> bool {
self.options
.as_ref()
.and_then(|o| o.unique)
.unwrap_or(false)
}
}