Is it possible to define a trait like the following one:
pub trait DataValidator {
fn validate_data(&self, data: TBD) -> bool;
}
where the TBD type can be different data types like u32, Vec<u8>, Vec<Vec<u8>>, ... and have a different implementation for different enums that implement that trait?
The idea is to create a validator for different data types. I have the following enum:
pub enum DataType {
Text,
Number,
Array,
}
and I have an struct that uses that enum
pub struct Validator {
pub data_type: DataType
}
I want to implement the trait for Validator, but the input that the validate_data function is not the same all the time, or at least that's what I want to cover.
So for example, let's say I have a Validator name validator with data_type set as Text. I want to then call the validate_data function like validator.validate_data(data) where data is a Vec<u8>. It only has to validate that the length of the Vec is within a certain limits.
But let's say I have another Validator named validator2 with data_type set as Array. I want to pass to the validate_data function an array of different DataTypes and validate each one of them using the same logic, but for that I need the trait to be flexible with the input type. Is it possible?
datainput to any data typeimpl DataValidator<u32> for Validatormeans that it can validateu32s and do that for all the types you need.