Skip to main content

annotate_build/
lib.rs

1use std::path::PathBuf;
2
3pub(crate) use function::*;
4pub(crate) use module::*;
5pub(crate) use path::*;
6pub(crate) use render::*;
7
8mod builder;
9mod environment;
10mod function;
11mod module;
12mod parser;
13mod path;
14mod render;
15mod visitor;
16
17#[derive(Default)]
18pub struct BuildConfig {
19    pragmas: Vec<String>,
20    derives: Vec<visitor::CustomDerive>,
21}
22
23impl BuildConfig {
24    /// Register an additional attribute name that should be treated like `#[pragma(...)]`.
25    pub fn pragma(&mut self, pragma: impl Into<String>) -> &mut Self {
26        self.pragmas.push(pragma.into());
27        self
28    }
29
30    /// Register a custom derive name and its mustache template expansion.
31    pub fn derive(&mut self, name: impl Into<String>, template: impl AsRef<str>) -> &mut Self {
32        self.derives
33            .push(visitor::CustomDerive::new(name, template.as_ref()));
34        self
35    }
36}
37
38/// Scan the current crate for `#[pragma(...)]` annotations and export the generated environment.
39pub fn build() {
40    build_with(|_| {});
41}
42
43/// Scan the current crate and export the generated environment using a small configuration DSL.
44pub fn build_with(configure: impl FnOnce(&mut BuildConfig)) {
45    let mut config = BuildConfig::default();
46    configure(&mut config);
47
48    let manifest_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
49
50    builder::build_manifest(
51        config.pragmas,
52        &config.derives,
53        manifest_dir.join("Cargo.toml"),
54        manifest_dir.file_name().unwrap(),
55    )
56}