1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use std::collections::HashSet;
use std::path::{Path, PathBuf};

use derive_builder::Builder;

use super::dependency::Dependency;

#[derive(Debug, Default, Builder, Clone)]
#[must_use]
pub struct Config {
  #[builder(setter(into))]
  #[builder(default = "std::env::var(\"OUT_DIR\").map(Into::into).unwrap_or_default()")]
  pub(crate) out_dir: PathBuf,
  #[builder(default)]
  pub(crate) raw: bool,
  #[builder(setter(into))]
  pub(crate) spec: PathBuf,
  #[builder(default = "true")]
  pub(crate) op_traits: bool,
  #[builder(default = "true")]
  pub(crate) components: bool,
  #[builder(default = "true")]
  pub(crate) output_structs: bool,
  #[builder(setter(skip))]
  pub(crate) deps: HashSet<Dependency>,
}

impl Config {
  pub fn new() -> Self {
    Self::default()
  }

  pub(crate) fn add_dep(&mut self, dep: Dependency) {
    self.deps.insert(dep);
  }

  pub fn exec(self) -> anyhow::Result<()> {
    super::build(self)?;
    Ok(())
  }
}

impl ConfigBuilder {
  pub fn new() -> Self {
    Self::default()
  }
  pub fn generate(&mut self, spec: impl AsRef<Path>) -> anyhow::Result<()> {
    let config = self.spec(spec.as_ref().to_path_buf()).build()?;
    super::build(config)?;

    Ok(())
  }
}

#[must_use]
pub fn configure() -> ConfigBuilder {
  ConfigBuilder::default()
}