Error handling in library: Enum or Trait type

Thanks for your reply !

I'm not sure exactly what your constraints

I think i need to give you more details about this init function.

The desired function signature is the following:

pub fn init(domain_name: &str, driver_type: Option<DriverType>, init_option: Option<DriverInitParam>) -> Result<Box<dyn Introspectable>, MicrovmiError>

As you can see, the DriverType enum is optional, in order to enable 2 behavior:

  • Some(): where we return a Box<dyn Introspectable> of the requested driver
  • None: in which case we will iterate over all the fields in the enums, and try to instantiate the driver, to return the first successful one. (not that this is not implemented yet)

So your proposed solution would be interesting for the first case, but for the second case, i'm not sure how to handle it with generic types here.

Now I'm wondering if it makes sense to have a unique init function, handling both cases, or you think it will be easier (in terms of implementation) to offer 2 distinct function to probe for available drivers, and another one to initialize a specific driver.

However, I like the idea of having this unique entrypoint into the library, as it's simplifying the API for the end user.

Thank you very much.