// Copyright (c) 2026, Salesforce, Inc.,
// All rights reserved.
// For full license text, see the LICENSE.txt file

//! PDK Macros
//!
//! This module provides the macros to declare the entry points of your code as a custom policy.

mod entrypoint;

use crate::entrypoint::{generate_entrypoint, generate_entrypoint_flex};
use proc_macro2::TokenStream;

#[proc_macro_attribute]
/// Use this macro to annotate the configuration function for you custom policy.
/// ```rust
/// #[entrypoint]
/// async fn configure(
///     launcher: Launcher,
///     Configuration(configuration): Configuration,
/// ) -> anyhow::Result<()> {
///     ...
/// }
/// ```
pub fn entrypoint(
    metadata: proc_macro::TokenStream,
    input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let input = TokenStream::from(input);
    let metadata = TokenStream::from(metadata);
    let output = match generate_entrypoint(metadata, input) {
        Ok(stream) => stream,
        Err(error) => error.to_compile_error(),
    };
    proc_macro::TokenStream::from(output)
}

#[proc_macro_attribute]
/// Macro to annotate the setup function for the flex abi entrypoint. Manual usage of this macro is
/// discouraged. It should is automatically handled with the autogenerated code from the policy definition.
pub fn entrypoint_flex(
    metadata: proc_macro::TokenStream,
    input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let input = TokenStream::from(input);
    let metadata = TokenStream::from(metadata);
    let output = match generate_entrypoint_flex(metadata, input) {
        Ok(stream) => stream,
        Err(error) => error.to_compile_error(),
    };
    proc_macro::TokenStream::from(output)
}
