Let's say, I have a variable "p" of ParameterType - how can I set the generic based on that ParameterType, like:
let p = ParameterType::KILOMETER;
let schema = parameter::Schema<p> {
_id: ObjectId::new(),
user_id: authenticated_user._id,
parameter_type: p,
value: 7 // i64
}
is there any way to achieve this?
An alternative would be to have something like an "std::any::Any" type that can be inserted to mongodb regardless of it's type, like - it can be a String or an i64...
You are passing an instance of ParameterType, not the ParameterType type. Since you are new to Rust I would suggest that you formulate your questions based on what you want to achieve, and not how you want to achieve it, because you are using patterns from other languages that are not replicable in Rust or are not the best solution to achieve the results you are looking for.
what I want to achieve is to insert a document to mongodb based on a struct, that has a field with a dynamic type that can be for instance a String or an i64. Nevermind about the enum thing - it's just about having a field that can differ in type :>
In Rust, the algebraic sum type is the Enum, so you can represent a type that is either A or B. Rust enums are tagged, so you will always know what variant are you dealing with (i.e. KilometerSchema or ReviewSchema) because you will need to pattern match on it. When deserializing it from your MongoDB database, the deserializer will visit all of the enum variants until it finds one that matches. You could also use an internally tagged enum to further hint the deserializer:
it will not only validate that a certain type is only allowed for a certain enum key but also enable insertion to mongo with different types for the same value key. this is exactly what I thought of, thank you!
Just a word of caution: Think how you'll consume your types when defining the database schema. Depending on your business logic you might want to have a single struct as per ExpHP's example, or multiple structs (like in my example).