aboutsummaryrefslogtreecommitdiff
path: root/mingling_macros/src/func/gen_program.rs
blob: 6b6a643b09089bb9014c6cd422930be1a9eedcea (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
57
use proc_macro::TokenStream;
use quote::quote;

/// Entry point for `gen_program!()`.
///
/// Generates the `Next` type alias, `Routable` impl for `ChainProcess`,
/// and delegates to `program_comp_gen!()`, `program_fallback_gen!()`,
/// and `program_final_gen!()`.
pub(crate) fn gen_program_impl(_input: TokenStream) -> TokenStream {
    #[cfg(feature = "comp")]
    let comp_gen = quote! {
        ::mingling::macros::program_comp_gen!();
    };

    #[cfg(not(feature = "comp"))]
    let comp_gen = quote! {};

    // When pathf is enabled, include the type_using.rs generated by the build
    // script so that types from submodules are in scope for gen_program!().
    #[cfg(feature = "pathf")]
    let pathf_include = quote! {
        include!(concat!(env!("OUT_DIR"), "/", env!("CARGO_PKG_NAME"), "/type_using.rs"));
    };
    #[cfg(not(feature = "pathf"))]
    let pathf_include = quote! {};

    TokenStream::from(quote! {
        /// Alias for the current program type `crate::ThisProgram`
        pub type Next = ::mingling::ChainProcess<crate::ThisProgram>;

        impl ::mingling::Routable<crate::ThisProgram> for ::mingling::ChainProcess<crate::ThisProgram>
        {
            fn to_chain(self) -> ::mingling::ChainProcess<crate::ThisProgram> {
                match self {
                    ::mingling::ChainProcess::Ok((any, _)) => {
                        ::mingling::ChainProcess::Ok((any, mingling::NextProcess::Chain))
                    }
                    other => other,
                }
            }

            fn to_render(self) -> ::mingling::ChainProcess<crate::ThisProgram> {
                match self {
                    ::mingling::ChainProcess::Ok((any, _)) => {
                        ::mingling::ChainProcess::Ok((any, mingling::NextProcess::Renderer))
                    }
                    other => other,
                }
            }
        }

        #pathf_include
        #comp_gen
        ::mingling::macros::program_fallback_gen!();
        ::mingling::macros::program_final_gen!();
    })
}