aro-macros 1.0.0

Procedural macros for the Aro web framework
Documentation
//! Crate path resolution for macro-generated code.
//!
//! Uses `proc-macro-crate` to detect whether the user depends on `aro`
//! (the facade crate) or individual sub-crates directly. Macro-generated
//! code uses paths through whichever crate is available, so users only
//! need `aro` in their Cargo.toml.

use proc_macro_crate::{FoundCrate, crate_name};
use proc_macro2::TokenStream;
use quote::quote;
use syn::Ident;

/// Returns the path to `aro_core` types in the user's crate.
///
/// - If user depends on `aro`: returns `::aro::core`
/// - If user depends on `aro-core`: returns `::aro_core`
pub fn aro_core_path() -> TokenStream {
    if let Ok(found) = crate_name("aro") {
        let ident = found_crate_ident(found);
        quote!(#ident::core)
    } else if let Ok(found) = crate_name("aro-core") {
        let ident = found_crate_ident(found);
        quote!(#ident)
    } else {
        // Fallback — will produce a clear compile error if neither is available
        quote!(::aro_core)
    }
}

/// Returns the path to `axum` types in the user's crate.
///
/// - If user depends on `aro`: returns `::aro::web::axum`
/// - If user depends on `aro-web`: returns `::aro_web::axum`
/// - If user depends on `axum` directly: returns `::axum`
pub fn axum_path() -> TokenStream {
    if let Ok(found) = crate_name("aro") {
        let ident = found_crate_ident(found);
        quote!(#ident::web::axum)
    } else if let Ok(found) = crate_name("aro-web") {
        let ident = found_crate_ident(found);
        quote!(#ident::axum)
    } else if let Ok(found) = crate_name("axum") {
        let ident = found_crate_ident(found);
        quote!(#ident)
    } else {
        quote!(::axum)
    }
}

/// Returns the path to `fletch-orm` types in the user's crate.
///
/// - If user depends on `fletch-orm` directly: returns `::fletch_orm`
/// - If user depends on `aro-fletch`: returns `::aro_fletch::fletch_orm`
/// - If user depends on `aro`: returns `::aro::fletch::fletch_orm`
pub fn fletch_path() -> TokenStream {
    if let Ok(found) = crate_name("fletch-orm") {
        let ident = found_crate_ident(found);
        quote!(#ident)
    } else if let Ok(found) = crate_name("aro-fletch") {
        let ident = found_crate_ident(found);
        quote!(#ident::fletch_orm)
    } else if let Ok(found) = crate_name("aro") {
        let ident = found_crate_ident(found);
        quote!(#ident::fletch::fletch_orm)
    } else {
        quote!(::fletch_orm)
    }
}

fn found_crate_ident(found: FoundCrate) -> TokenStream {
    match found {
        FoundCrate::Itself => quote!(crate),
        FoundCrate::Name(name) => {
            let ident = Ident::new(&name, proc_macro2::Span::call_site());
            quote!(::#ident)
        }
    }
}