Skip to main content

Crate laravel_casts

Crate laravel_casts 

Source
Expand description

Eloquent-style attribute casts for laravel-rust models.

laravel-rust models have typed fields and no cast hook, so a cast is the field’s type, the way Encrypted<T> is. get is Decode + Serialize, set is From<&T> for Value + Deserialize.

ⓘ
#[model]
struct Invoice {
    id: i64,
    issued_at: DateCast,                  // DateCast::class
    paid_at: Option<DateTimeCast<Parsed>>, // DateTimeCast::SETPARSED
    period: Option<DateCast<MonthYear>>,  // DateCast::SETONLYMONTHYEAR
}

A cast of your own implements ColumnCast. Casts chosen at run time, by name, are in laravel_casts::dynamic, behind the dynamic feature (off by default).

Modules§

canonical
A date cast stored as data, not shown to people: in a JSON column, a cache or a queued job.
dynamicdynamic
Casts chosen at run time, by name, over JSON values, as a cast_array() helper over Eloquent casts does in PHP.

Macros§

castsdynamic
A list of (field, cast) where each cast is a text or an object, without wrapping each one in a Spec.
date_format
Declare a Format with its own pattern, then use it as the cast’s type parameter.
impl_column_cast
Gives a type that implements ColumnCast the traits a laravel-rust model field asks for: Decode, Nullable, and From<T>, From<&T> and From<&&T> for Value.

Structs§

Cast
A date column that reads with its storage format, writes the same, and serializes with the configured display pattern. Use DateCast and DateTimeCast as field types.
Configured
DateCast::class: serialize with the configured pattern, accept the storage format.
DateAndTime
DATETIME column.
DateConfig
Where the date patterns of an application are configured: one per kind, one per purpose, and two legacy settings used when nothing else is set.
DateOnly
DATE column.
DatePatterns
One pattern per kind.
MonthYear
DateCast::SETONLYMONTHYEAR: read and write m/Y.
Parsed
DateCast::SETPARSED: also accept the configured pattern.
PhpDatePattern
A compiled PHP date pattern.

Enums§

CastError
A value that cannot be cast, or a cast that cannot be understood.
Error
A date pattern that cannot be used.
Kind
Which column type a cast stores.

Traits§

ColumnCast
A model field stored as another type: the column holds ColumnCast::Repr, the program works with Self.
Format
Marker for the cast’s constructor arguments. Equivalent to the PHP Cast::class . ':true,m/Y,m/Y' string, as a type.
Storage
Marker for the stored column type. Equivalent to the PHP subclass (DateCast, DateTimeCast).

Functions§

configure
Replace the settings. Patterns are validated first, so a bad one fails at boot, not on a row.

Type Aliases§

DateCast
Casts\DateCast: a DATE column. DateCast<Parsed> is DateCast::SETPARSED, DateCast<MonthYear> is DateCast::SETONLYMONTHYEAR.
DateTimeCast
Casts\DateTimeCast: a DATETIME column.
Result
A date pattern that failed validation.