use crate::model::ValueKind;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum FieldProtection {
None,
Sha256,
Hmac,
Rsa,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum FieldIndex {
#[default]
None,
Exact,
Prefix(usize),
Ngram(usize),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FieldDef {
pub name: String,
pub kind: ValueKind,
pub protection: FieldProtection,
pub indexed: bool,
pub required: bool,
pub search: FieldIndex,
}
impl FieldDef {
pub fn text(name: &str) -> Self {
Self {
name: name.to_string(),
kind: ValueKind::Text,
protection: FieldProtection::None,
indexed: false,
required: false,
search: FieldIndex::None,
}
}
pub fn indexed(mut self) -> Self {
self.indexed = true;
self
}
pub fn required(mut self) -> Self {
self.required = true;
self
}
pub fn kind(mut self, kind: ValueKind) -> Self {
self.kind = kind;
self
}
pub fn protection(mut self, p: FieldProtection) -> Self {
self.protection = p;
self
}
pub fn search(mut self, idx: FieldIndex) -> Self {
self.search = idx;
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TypeSchema {
pub type_name: String,
pub fields: Vec<FieldDef>,
}
impl TypeSchema {
pub fn new(type_name: &str, fields: Vec<FieldDef>) -> Self {
Self {
type_name: type_name.to_string(),
fields,
}
}
pub fn field(&self, name: &str) -> Option<&FieldDef> {
self.fields.iter().find(|f| f.name == name)
}
}
pub fn default_target_type() -> TypeSchema {
TypeSchema::new(
"default_target",
vec![
FieldDef::text("name").indexed().required(),
FieldDef::text("description"),
],
)
}
pub fn default_actor_type() -> TypeSchema {
TypeSchema::new(
"default_actor",
vec![
FieldDef::text("name").indexed().required(),
FieldDef::text("role").indexed(),
],
)
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomColumnDef {
pub name: String,
pub kind: ValueKind,
pub required: bool,
pub required_since: Option<u64>,
}
impl CustomColumnDef {
pub fn new(name: &str, kind: ValueKind) -> Self {
Self {
name: name.to_string(),
kind,
required: false,
required_since: None,
}
}
pub fn required(mut self) -> Self {
self.required = true;
self
}
}