Expand description
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
#[macro_use] extern crate diesel;
use diesel_enum_case::DbEnum;
use diesel::{deserialize::FromSqlRow, sql_types::SmallInt};
#[derive(Debug, thiserror::Error)]
#[error("CustomError: {msg}, {status}")]
pub struct CustomError {
msg: String,
status: u16,
}
impl CustomError {
fn not_found(msg: String) -> Self {
Self {
msg,
status: 404,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, FromSqlRow, DbEnum)]
#[diesel(sql_type = SmallInt)]
#[diesel_enum(error_fn = CustomError::not_found)]
#[diesel_enum(error_type = CustomError)]
pub enum Status {
/// Will be represented as 0.
Ready,
/// Will be represented as 1.
Pending,
}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):
#[derive(Debug, Clone, Copy, PartialEq, Eq, FromSqlRow, DbEnum)]
#[diesel(sql_type = VarChar)]
#[diesel_enum(error_fn = CustomError::not_found)]
#[diesel_enum(error_type = CustomError)]
pub enum Status {
/// Will be represented as `"ready"`.
Ready,
/// Will be represented as `"pending"`.
Pending,
}You can also specify a case convention using the case attribute. Supported options are:
lowercase, UPPERCASE, camelCase, PascalCase, snake_case, SCREAMING_SNAKE_CASE, kebab-case.
For example, using snake_case:
#[derive(Debug, Clone, Copy, PartialEq, Eq, FromSqlRow, DbEnum)]
#[diesel(sql_type = VarChar)]
#[diesel_enum(error_fn = CustomError::not_found)]
#[diesel_enum(error_type = CustomError)]
#[diesel_enum(case = "snake_case")]
pub enum Status {
/// Will be represented as `"ready_to_go"`.
ReadyToGo,
/// Will be represented as `"pending_review"`.
PendingReview,
}