use crate::error::BaseError;
use crate::table::{FieldType, RegexCache, Validator};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub(crate) struct FieldConfig {
pub name: String,
pub display_name: String,
pub field_type: FieldType,
pub required: bool,
pub default_value: Option<serde_json::Value>,
pub auto_increment: bool,
pub renamed_from: Option<String>,
pub generated: Option<String>,
pub comment: Option<String>,
pub collation: Option<String>,
pub native_enum: Option<String>,
pub validators: Vec<Validator>,
pub permissions: FieldPermissions,
pub(crate) hidden: bool,
pub searchable: bool,
pub filterable: bool,
pub sortable: bool,
pub tenant_key: bool,
pub relation: Option<RelationConfig>,
}
impl FieldConfig {
pub(crate) fn new(name: impl Into<String>, field_type: FieldType) -> Self {
Self {
name: name.into(),
display_name: String::new(),
field_type,
required: false,
default_value: None,
auto_increment: false,
renamed_from: None,
generated: None,
comment: None,
collation: None,
native_enum: None,
validators: Vec::new(),
permissions: FieldPermissions::default(),
hidden: false,
searchable: false,
filterable: true,
sortable: true,
tenant_key: false,
relation: None,
}
}
pub(crate) fn validate(
&self,
value: &serde_json::Value,
cache: &RegexCache,
) -> Result<(), BaseError> {
if self.required && value.is_null() {
return Err(BaseError::FieldRequired(self.name.clone()));
}
if value.is_null() {
return Ok(());
}
self.field_type.validate(&self.name, value)?;
for validator in &self.validators {
validator.validate_with(&self.name, value, cache)?;
}
Ok(())
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", content = "roles", rename_all = "snake_case")]
pub(crate) enum Audience {
#[default]
Everyone,
Nobody,
Roles(HashSet<String>),
}
impl Audience {
pub(crate) fn roles<I, S>(roles: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
Self::Roles(roles.into_iter().map(Into::into).collect())
}
pub(crate) fn allows(&self, user_roles: &HashSet<String>) -> bool {
match self {
Self::Everyone => true,
Self::Nobody => false,
Self::Roles(roles) => user_roles.iter().any(|role| roles.contains(role)),
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub(crate) struct FieldPermissions {
pub readable: Audience,
pub writable: Audience,
pub filterable: Audience,
pub sortable: Audience,
}
impl FieldPermissions {
pub(crate) fn can_read(&self, user_roles: &HashSet<String>) -> bool {
self.readable.allows(user_roles)
}
#[cfg(any(feature = "mysql", feature = "postgres", test))]
pub(crate) fn can_write(&self, user_roles: &HashSet<String>) -> bool {
self.writable.allows(user_roles)
}
pub(crate) fn can_filter(&self, user_roles: &HashSet<String>) -> bool {
self.filterable.allows(user_roles)
}
pub(crate) fn can_sort(&self, user_roles: &HashSet<String>) -> bool {
self.sortable.allows(user_roles)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct RelationConfig {
pub table: String,
pub field: String,
pub display_fields: Vec<String>,
pub relation_type: RelationType,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RelationType {
OneToOne,
OneToMany,
ManyToOne,
ManyToMany,
}