Skip to main content

FieldType

Enum FieldType 

Source
#[non_exhaustive]
pub enum FieldType {
Show 13 variants String { max_length: usize, }, Integer, BigInt, Float, Double, Boolean, Date, DateTime, Timestamp, Json, Text, Enum { values: Vec<String>, }, ForeignKey { table: String, field: String, },
}
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 };

Fields

§max_length: usize

最大长度(字符数)

§

Integer

整数类型

用于存储 32 位有符号整数(i32)。

§示例

use yang_base::table::FieldType;

let field_type = FieldType::Integer;
§

BigInt

长整数类型

用于存储 64 位有符号整数(i64)。

§示例

use yang_base::table::FieldType;

let field_type = FieldType::BigInt;
§

Float

单精度浮点数类型

用于存储 32 位浮点数(f32)。

§示例

use yang_base::table::FieldType;

let field_type = FieldType::Float;
§

Double

双精度浮点数类型

用于存储 64 位浮点数(f64)。

§示例

use yang_base::table::FieldType;

let field_type = FieldType::Double;
§

Boolean

布尔类型

用于存储布尔值(true/false)。

§示例

use yang_base::table::FieldType;

let field_type = FieldType::Boolean;
§

Date

日期类型

用于存储日期(年-月-日),不包含时间信息。

§示例

use yang_base::table::FieldType;

let field_type = FieldType::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

JSON 类型

用于存储 JSON 格式的复杂数据结构。

§示例

use yang_base::table::FieldType;

let field_type = FieldType::Json;
§

Text

文本类型

用于存储大文本数据,没有长度限制。

§示例

use yang_base::table::FieldType;

let field_type = FieldType::Text;
§

Enum

枚举类型

用于存储预定义的可选值列表中的一个值。

§字段

  • values: 可选值列表

§示例

use yang_base::table::FieldType;

let field_type = FieldType::Enum {
    values: vec![
        "pending".to_string(),
        "approved".to_string(),
        "rejected".to_string(),
    ],
};

Fields

§values: Vec<String>

可选值列表

§

ForeignKey

外键类型

用于关联其他表的字段,建立表之间的关系。

§字段

  • table: 关联的目标表名
  • field: 关联的目标字段名

§示例

use yang_base::table::FieldType;

let field_type = FieldType::ForeignKey {
    table: "users".to_string(),
    field: "id".to_string(),
};

Fields

§table: String

关联的目标表名

§field: String

关联的目标字段名

Implementations§

Source§

impl FieldType

Source

pub fn display_name(&self) -> &str

获取字段类型的显示名称

§返回值

返回字段类型的中文显示名称

§示例
use yang_base::table::FieldType;

let field_type = FieldType::String { max_length: 50 };
assert_eq!(field_type.display_name(), "字符串");

let field_type = FieldType::Integer;
assert_eq!(field_type.display_name(), "整数");
Source

pub fn is_numeric(&self) -> bool

检查字段类型是否为数值类型

§返回值

如果是数值类型(Integer, BigInt, Float, Double)返回 true,否则返回 false

§示例
use yang_base::table::FieldType;

assert!(FieldType::Integer.is_numeric());
assert!(FieldType::Float.is_numeric());
assert!(!FieldType::String { max_length: 50 }.is_numeric());
Source

pub fn is_temporal(&self) -> bool

检查字段类型是否为时间类型

§返回值

如果是时间类型(Date, DateTime, Timestamp)返回 true,否则返回 false

§示例
use yang_base::table::FieldType;

assert!(FieldType::Date.is_temporal());
assert!(FieldType::DateTime.is_temporal());
assert!(!FieldType::String { max_length: 50 }.is_temporal());
Source

pub fn is_text(&self) -> bool

检查字段类型是否为文本类型

§返回值

如果是文本类型(String, Text)返回 true,否则返回 false

§示例
use yang_base::table::FieldType;

assert!(FieldType::String { max_length: 50 }.is_text());
assert!(FieldType::Text.is_text());
assert!(!FieldType::Integer.is_text());
Source

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 Clone for FieldType

Source§

fn clone(&self) -> FieldType

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FieldType

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for FieldType

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for FieldType

Source§

fn eq(&self, other: &FieldType) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for FieldType

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for FieldType

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more