#[non_exhaustive]pub struct FieldConfig {
pub name: String,
pub display_name: String,
pub field_type: FieldType,
pub required: bool,
pub default_value: Option<Value>,
pub validators: Vec<Validator>,
pub permissions: FieldPermissions,
pub filterable: bool,
pub sortable: bool,
pub relation: Option<RelationConfig>,
}Expand description
字段配置
定义单个字段的所有属性,包括:
- 字段名称和显示名称
- 字段类型
- 必填标记和默认值
- 验证器列表
- 字段级权限配置
- 关联表信息
§示例
use yang_base::table::{FieldConfig, FieldType, Validator, FieldPermissions};
use serde_json::json;
use std::collections::HashSet;
// 创建一个必填的用户名字段
let username_field = FieldConfig::new("username", FieldType::String { max_length: 50 })
.display_name("用户名")
.required(true)
.validator(Validator::MinLength(3))
.validator(Validator::MaxLength(50));
// 创建一个带默认值的状态字段
let status_field = FieldConfig::new("status", FieldType::Enum {
values: vec!["active".to_string(), "inactive".to_string()],
})
.display_name("状态")
.default_value(json!("active"));
// 创建一个带权限控制的邮箱字段
let email_field = FieldConfig::new("email", FieldType::String { max_length: 100 })
.display_name("邮箱")
.required(true)
.validator(Validator::Email)
.permissions(FieldPermissions {
readable_roles: HashSet::from(["admin".to_string(), "user".to_string()]),
writable_roles: HashSet::from(["admin".to_string()]),
filterable_roles: HashSet::new(),
sortable_roles: HashSet::new(),
});Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.name: String字段名
数据库中的字段名称,通常使用蛇形命名法(snake_case)
display_name: String显示名称
用于前端展示的字段名称,通常使用中文
field_type: FieldType字段类型
定义字段的数据类型,如字符串、整数、枚举等
required: bool是否必填
如果为 true,则字段值不能为 null
default_value: Option<Value>默认值
当插入数据时未提供该字段值时使用的默认值
validators: Vec<Validator>验证器列表
用于验证字段值的验证器,按顺序执行
permissions: FieldPermissions字段权限
定义不同角色对该字段的访问权限
filterable: bool是否可筛选
如果为 true,则该字段可以用于 WHERE 条件筛选
sortable: bool是否可排序
如果为 true,则该字段可以用于 ORDER BY 排序
relation: Option<RelationConfig>关联表配置
用于外键字段,定义与其他表的关联关系
Implementations§
Source§impl FieldConfig
impl FieldConfig
Sourcepub fn display_name(self, name: impl Into<String>) -> Self
pub fn display_name(self, name: impl Into<String>) -> Self
Sourcepub fn default_value(self, value: Value) -> Self
pub fn default_value(self, value: Value) -> Self
Sourcepub fn permissions(self, permissions: FieldPermissions) -> Self
pub fn permissions(self, permissions: FieldPermissions) -> Self
设置字段权限
§参数
permissions: 字段权限配置
§返回值
返回 self,支持链式调用
§示例
use yang_base::table::{FieldConfig, FieldType, FieldPermissions};
use std::collections::HashSet;
let permissions = FieldPermissions {
readable_roles: HashSet::from(["admin".to_string()]),
writable_roles: HashSet::from(["admin".to_string()]),
filterable_roles: HashSet::new(),
sortable_roles: HashSet::new(),
};
let field = FieldConfig::new("salary", FieldType::Double)
.permissions(permissions);
assert_eq!(field.permissions.readable_roles.len(), 1);Sourcepub fn relation(self, relation: RelationConfig) -> Self
pub fn relation(self, relation: RelationConfig) -> Self
设置关联表配置
§参数
relation: 关联表配置
§返回值
返回 self,支持链式调用
§示例
use yang_base::table::{FieldConfig, FieldType, RelationConfig, RelationType};
let relation = RelationConfig {
table: "users".to_string(),
field: "id".to_string(),
display_fields: vec!["username".to_string(), "email".to_string()],
relation_type: RelationType::OneToOne,
};
let field = FieldConfig::new("user_id", FieldType::BigInt)
.relation(relation);
assert!(field.relation.is_some());Sourcepub fn filterable(self, filterable: bool) -> Self
pub fn filterable(self, filterable: bool) -> Self
Sourcepub fn validate(&self, value: &Value) -> Result<(), BaseError>
pub fn validate(&self, value: &Value) -> Result<(), BaseError>
验证字段值
执行完整的字段验证流程:
- 检查必填约束
- 执行字段类型验证
- 依次执行所有配置的验证器
§参数
value: 要验证的值
§返回值
如果验证通过返回 Ok(()),否则返回相应的错误
§错误
BaseError::FieldRequired: 字段为必填但值为 nullBaseError::InvalidFieldType: 字段类型验证失败BaseError::ValidationFailed: 验证器验证失败
§示例
use yang_base::table::{FieldConfig, FieldType, Validator};
use serde_json::json;
let field = FieldConfig::new("username", FieldType::String { max_length: 50 })
.required(true)
.validator(Validator::MinLength(3));
// 验证通过
assert!(field.validate(&json!("alice")).is_ok());
// 必填字段为 null
assert!(field.validate(&json!(null)).is_err());
// 长度不足
assert!(field.validate(&json!("ab")).is_err());Trait Implementations§
Source§impl Clone for FieldConfig
impl Clone for FieldConfig
Source§fn clone(&self) -> FieldConfig
fn clone(&self) -> FieldConfig
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for FieldConfig
impl !UnwindSafe for FieldConfig
impl Freeze for FieldConfig
impl Send for FieldConfig
impl Sync for FieldConfig
impl Unpin for FieldConfig
impl UnsafeUnpin for FieldConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more