aboutsummaryrefslogtreecommitdiff
path: root/mingling_macros/src/build.rs
blob: 8f2949df2bdc94e420b3f4a17f33b18a017eb7ba (plain) (blame)
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
//! Compile-time build logic for `build_comp!()` and `build_pathf!()`.
//!
//! The build steps run as a side effect of macro expansion (during `gen_program!`),
//! writing artifacts under `{target_directory}/mingling/`.

#[doc(hidden)]
#[cfg(feature = "comp")]
pub(crate) mod comp;

#[doc(hidden)]
#[cfg(feature = "pathf")]
pub(crate) mod pathf;

/// Shared implementation behind `build_comp!()`.
///
/// Accepts an optional string literal (the binary name); defaults to
/// `CARGO_PKG_NAME`. Returns an empty token stream on success, or a
/// `compile_error!` token stream on failure.
#[cfg(feature = "comp")]
pub(crate) fn comp_build_impl(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let bin_name: String = if input.is_empty() {
        std::env::var("CARGO_PKG_NAME").unwrap_or_default()
    } else {
        match syn::parse::<syn::LitStr>(input) {
            Ok(lit) => lit.value(),
            Err(e) => return e.to_compile_error().into(),
        }
    };

    match comp::build_comp_scripts(&bin_name) {
        Ok(()) => proc_macro::TokenStream::new(),
        Err(e) => {
            let msg = format!("build_comp: failed to generate completion scripts: {e}");
            syn::Error::new(proc_macro2::Span::call_site(), msg)
                .to_compile_error()
                .into()
        }
    }
}

/// Shared implementation behind `build_pathf!()`.
///
/// Runs the pathf type-mapping analysis. Returns an empty token stream on
/// success, or a `compile_error!` token stream on failure.
#[cfg(feature = "pathf")]
pub(crate) fn pathf_build_impl(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    match pathf::analyze_and_build_type_mapping() {
        Ok(()) => proc_macro::TokenStream::new(),
        Err(e) => {
            let msg = format!("build_pathf: type mapping analysis failed: {e}");
            syn::Error::new(proc_macro2::Span::call_site(), msg)
                .to_compile_error()
                .into()
        }
    }
}