This crate allows the user to represent state in the database using Rust enums. This is achieved
through a proc macro. First the macro looks at your chosen sql_type, and then it devises a
corresponding Rust type. The mapping is as follows:
| SQL | Rust |
|---|---|
SmallInt |
i16 |
Integer |
i32 |
Int |
i32 |
BigInt |
i64 |
VarChar |
String |
Text |
String |
The macro then generates three impls: a FromSql impl, an ToSql impl and a
TryFrom impl, which allow conversion between the Sql type an the enum (FromSql and ToSql),
and from the Rust type into the enum (TryInto).
Usage
use DbEnum;
use ;
Alternatively you can use strings, which will be cast to lowercase by default. (e.g. Status::Ready will be
stored as "ready" in the database):
Case Conversion
For string types, you can specify a case convention using the case attribute. Supported options are:
lowercase(default) - e.g.,ReadyToGo→"readytogo"UPPERCASE- e.g.,ReadyToGo→"READYTOGO"camelCase- e.g.,ReadyToGo→"readyToGo"PascalCase- e.g.,ReadyToGo→"ReadyToGo"snake_case- e.g.,ReadyToGo→"ready_to_go"SCREAMING_SNAKE_CASE- e.g.,ReadyToGo→"READY_TO_GO"kebab-case- e.g.,ReadyToGo→"ready-to-go"
Example using snake_case:
The case attribute works together with the val attribute - if a variant has an explicit val attribute, it will override the case conversion for that specific variant.
Another option is to manually override the values set for each or some of the variants. This is done
using the val attribute: