I'm messing around with a practice project and I'm wondering if there's a way to succinctly express this code... I've got an enum that represents all of the datatypes in SQL Server:
#[derive(PartialEq)]
pub enum SqlType{
Numeric { scale: u16, precision: u16 },
Bit,
TinyInt,
SmallInt,
Int,
BigInt,
SmallMoney,
Money,
Float,
//a whole bunch of other stuff
}
Now, I'd like to be able to write a function on SqlType
that is called like so:
enum TypeConversionDetail{
Implicit,
Explicit,
ImplicitWithLoss,
ExplicitWithLoss,
NotAllowed
}
impl SqlType{
fn get_conversion_details_for(&self, other: SqlType) -> TypeConversionDetail{
//VERY large match statement, n*n in size (roughly) where n is # of types
}
}
How can I encapsulate this? I'd like to be able to write this a chunk at a time (e.g., write a get_conversion_details_for
for each type like so:
impl SqlType::Numeric {
fn get_conversion_details_for(&self, other: SqlType) -> TypeConversionDetail{
//smaller match statement!
}
}
However, this isn't possible. In addition, manually changing the enum to contain real types still forces you to write the original match statement that delegates the conversion logic to each type.
Is there a better way to do this?