dbc_codegen/
feature_config.rs1use std::fmt::Display;
2use std::io::Write;
3
4use anyhow::{Error, Result};
5
6#[derive(Default)]
10pub enum FeatureConfig<'a> {
11 Always,
13
14 Gated(&'a str),
16
17 #[default]
19 Never,
20}
21
22impl FeatureConfig<'_> {
23 pub(crate) fn fmt_attr(&self, w: &mut impl Write, attr: impl Display) -> Result<()> {
24 match self {
25 FeatureConfig::Always => writeln!(w, "#[{attr}]")?,
26 FeatureConfig::Gated(gate) => writeln!(w, "#[cfg_attr(feature = {gate:?}, {attr})]")?,
27 FeatureConfig::Never => {}
28 }
29 Ok(())
30 }
31
32 pub(crate) fn fmt_cfg<W: Write, E: Into<Error>>(
33 &self,
34 mut w: W,
35 f: impl FnOnce(W) -> Result<(), E>,
36 ) -> Result<()> {
37 match self {
38 FeatureConfig::Never => return Ok(()),
40 FeatureConfig::Gated(gate) => {
42 writeln!(w, "#[cfg(feature = {gate:?})]")?;
43 }
44 FeatureConfig::Always => {}
46 }
47
48 f(w).map_err(Into::into)
49 }
50}