Enums with Fields in Diesel


#1

I can create an Enum usable in queries by implementing FromSql and ToSql. (And AsExpression.) Is it possible to use an Enum that has fields? Maybe by implementing AsSqlRow? Here’s my use case:

I have an application that tracks payments. These payments can come from a number of sources including PayPal, Cash, and Check. If it is a check, I need a check number. If it is PayPal, I need a transaction id. So my enum looks like this:

enum PaymentType {
  Check(i32),
  PayPal(String),
  Cash,
}

I’d like to store this enum in my payments table with string discriminator column having “check”, “cash”, or “paypal”. Also a column for check_number, which is null if not necessary, and a column for paypal_transaction, which is null if not necessary. Is this possible with Diesel?


#2

https://gitter.im/diesel-rs/diesel might be a better place to ask this question.