I have a library with an enum that looks somewhat like this:
enum Style {
Base,
LineNumber,
Severity(Level),
#[cfg(feature = "code-styling")]
Code(CodeStyle),
#[doc(hidden)]
NonExhaustive,
}
And a trait somewhat like this:
trait Stylesheet {
fn apply(style: Style, to: &mut dyn WriteColor) -> io::Result<()>;
}
I'd like to have downstream users provide library implementations of Stylesheet
to be used by other binaries, with the end binary selecting whether they want to include the CodeStyle
variant, without the middle library having to also expose a code-styling
feature.
What's the best way to let these middle libraries conditionally include code if a feature is enabled in my library? Note that the way that this would likely be used is something like
impl Stylesheet for MyStyle {
fn apply(style: Style, to: &mut dyn WriteColor) -> io::Result<()> {
match style {
#[cfg(has_code_style!())]
Code(style) => match style {
_ => unimplemented!(),
}
_ => unimplemented!(),
}
}
}