#[non_exhaustive]pub enum FieldType {
}Expand description
字段类型
定义数据表字段支持的所有类型,包括:
- 基本类型:String, Integer, BigInt, Float, Double, Boolean
- 时间类型:Date, DateTime, Timestamp
- 复杂类型:Json, Text
- 枚举类型:Enum
§示例
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()],
};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;Decimal
定点小数类型。
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(),
],
};Uuid
UUID 类型
运行期以连字符小写字符串表示。PostgreSQL 映射为原生 uuid;MySQL 没有
原生 UUID,统一映射为定长 CHAR(36)(保留连字符文本形式,跨后端读写
表示一致)。
§示例
use yang_base::table::FieldType;
let field_type = FieldType::Uuid;Bytes
二进制类型
运行期以 Base64 字符串表示。PostgreSQL 映射为 BYTEA,MySQL 映射为
BLOB。
§示例
use yang_base::table::FieldType;
let field_type = FieldType::Bytes;Array(Box<FieldType>)
数组类型(仅支持标量元素,如 INTEGER[] / TEXT[])
运行期以 JSON 数组表示,元素按标量规则转换,数组内 NULL 保持 JSON
null。禁止嵌套数组与 Json 元素(无法无损映射到数据库数组字面量),
构造/构建期会明确报错。MySQL 没有数组类型,渲染 DDL 时明确拒绝。
Implementations§
Source§impl FieldType
impl FieldType
Sourcepub fn display_name(&self) -> &str
pub fn display_name(&self) -> &str
Sourcepub fn array(element: FieldType) -> Self
pub fn array(element: FieldType) -> Self
构造数组类型(标量元素)。
只做构造,不校验元素;构建期 Table::build 会拒绝嵌套数组与 Json
元素等无法映射的声明。
§示例
use yang_base::table::FieldType;
let field_type = FieldType::array(FieldType::Integer);
assert!(field_type.is_array());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 Forget for FieldType
impl Freeze for FieldType
impl Move 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