TryInto, Self and Error

I have a Config struct which can be a little bit daunting to use at first. In most cases, users (in particular new users) will only use a tiny subset of Config, and different users will use different small subsets of it.

I wanted to experiment with a few simpler, special-purpose, structs that can be converted into Config.

I figured I'd try using TryInto<Config> for this. All configurations end up as a Config in the end, but the factory function can take in other types, such as PlainTextServer. This works great, apart from trying to actually use the Config as an input. If I understand it correctly: When Config is passed in to a TryInto<Config> it ends up using the Infallible as the Error type, which is a mismatch with the type I explicitly specify as crate::err::Error.

I know I can work around this by introducing a custom IntoConfig trait, but is there a way to express the Error bound such that I can keep using TryInfo<Config> (for Config as input)?

the standard library has several impl From<Infallible> for XYZError {}, you can do similar thing:

impl From<Infallible> for crate::err::Error { ... }

fn foo<T>(config: T) -> Result<..., crate::err::Error>
where
    T: TryInto<Config>,
    T::Error: Into<crate::err::Error>
{
    let config = config.try_into()?;
    todo!()
}

personally i would also suggest taking advantage of rust syntax for struct initialization. you can have something like this

impl Config{
      pub fn preset_1()->Self{.....}

      pub fn preset_2()->Self{.....}

      pub fn preset_3()->Self{.....}
}


and do
let my_config=Config{
      something_to_change:"some new value",
      ..Config::preset_2()
};

this way users can easily see all options available and use all that they need but the can also focus on what's of interest and leave the rest to a reasonable default