use std::fmt::Display;
use std::io::Write;
use anyhow::{Error, Result};
#[derive(Default)]
pub enum FeatureConfig<'a> {
Always,
Gated(&'a str),
#[default]
Never,
}
impl FeatureConfig<'_> {
pub(crate) fn fmt_attr(&self, w: &mut impl Write, attr: impl Display) -> Result<()> {
match self {
FeatureConfig::Always => writeln!(w, "#[{attr}]")?,
FeatureConfig::Gated(gate) => writeln!(w, "#[cfg_attr(feature = {gate:?}, {attr})]")?,
FeatureConfig::Never => {}
}
Ok(())
}
pub(crate) fn fmt_cfg<W: Write, E: Into<Error>>(
&self,
mut w: W,
f: impl FnOnce(W) -> Result<(), E>,
) -> Result<()> {
match self {
FeatureConfig::Never => return Ok(()),
FeatureConfig::Gated(gate) => {
writeln!(w, "#[cfg(feature = {gate:?})]")?;
}
FeatureConfig::Always => {}
}
f(w).map_err(Into::into)
}
}