#[non_exhaustive]pub enum FieldType {
}Expand description
字段类型
定义数据表字段支持的所有类型,包括:
- 基本类型:String, Integer, BigInt, Float, Double, Boolean
- 时间类型:Date, DateTime, Timestamp
- 复杂类型:Json, Text
- 枚举类型:Enum
- 外键类型:ForeignKey
§示例
use yang_base::table::FieldType;
// 创建字符串类型,最大长度 50
let name_type = FieldType::String { max_length: 50 };
// 创建整数类型
let age_type = FieldType::Integer;
// 创建枚举类型
let status_type = FieldType::Enum {
values: vec!["active".to_string(), "inactive".to_string()],
};
// 创建外键类型
let user_id_type = FieldType::ForeignKey {
table: "users".to_string(),
field: "id".to_string(),
};Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
String
字符串类型
用于存储文本数据,需要指定最大长度。
§字段
max_length: 字符串的最大长度(字符数)
§示例
use yang_base::table::FieldType;
let field_type = FieldType::String { max_length: 100 };Integer
BigInt
Float
Double
双精度浮点数类型
用于存储 64 位浮点数(f64)。
§示例
use yang_base::table::FieldType;
let field_type = FieldType::Double;Boolean
Date
DateTime
日期时间类型
用于存储日期和时间(年-月-日 时:分:秒)。
§示例
use yang_base::table::FieldType;
let field_type = FieldType::DateTime;Timestamp
时间戳类型
用于存储 Unix 时间戳(秒数)。
§示例
use yang_base::table::FieldType;
let field_type = FieldType::Timestamp;Json
Text
Enum
枚举类型
用于存储预定义的可选值列表中的一个值。
§字段
values: 可选值列表
§示例
use yang_base::table::FieldType;
let field_type = FieldType::Enum {
values: vec![
"pending".to_string(),
"approved".to_string(),
"rejected".to_string(),
],
};ForeignKey
Implementations§
Source§impl FieldType
impl FieldType
Sourcepub fn display_name(&self) -> &str
pub fn display_name(&self) -> &str
Sourcepub fn is_numeric(&self) -> bool
pub fn is_numeric(&self) -> bool
Sourcepub fn is_temporal(&self) -> bool
pub fn is_temporal(&self) -> bool
Sourcepub fn validate(&self, field_name: &str, value: &Value) -> Result<(), BaseError>
pub fn validate(&self, field_name: &str, value: &Value) -> Result<(), BaseError>
验证字段值是否符合字段类型要求
§参数
field_name: 字段名称(用于错误消息)value: 要验证的值
§返回值
如果验证通过返回 Ok(()),否则返回相应的错误
§示例
use yang_base::table::FieldType;
use serde_json::json;
let field_type = FieldType::String { max_length: 10 };
assert!(field_type.validate("name", &json!("hello")).is_ok());
assert!(field_type.validate("name", &json!("this is too long")).is_err());
let field_type = FieldType::Integer;
assert!(field_type.validate("age", &json!(25)).is_ok());
assert!(field_type.validate("age", &json!("not a number")).is_err());Trait Implementations§
Source§impl<'de> Deserialize<'de> for FieldType
impl<'de> Deserialize<'de> for FieldType
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl StructuralPartialEq for FieldType
Auto Trait Implementations§
impl Freeze for FieldType
impl RefUnwindSafe for FieldType
impl Send for FieldType
impl Sync for FieldType
impl Unpin for FieldType
impl UnsafeUnpin for FieldType
impl UnwindSafe for FieldType
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,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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