// Copyright Two Neutron Stars Incorporated and contributors
// SPDX-License-Identifier: BlueOak-1.0.0

use {
  self::parse::parse,
  proc_macro2::TokenStream,
};

mod codegen;
mod parse;

pub(crate) fn process(
  item: TokenStream,
  flags: crate::Args,
) -> Result<TokenStream, syn::Error> {
  let ast = parse(item)?;

  // Convert our Error type to syn::Error for the codegen step
  match codegen::codegen(flags, ast) {
    | Ok(tokens) => Ok(tokens),
    | Err(error) => {
      let details = error.get();
      let mut syn_error = syn::Error::new(details.span, &details.message);

      // Add help message if available
      if let Some(help) = &details.help {
        let help_error = syn::Error::new(details.span, format!("help: {help}"));
        syn_error.combine(help_error);
      }

      // Add hints if available
      if let Some(hints) = &details.hints {
        let hints_error =
          syn::Error::new(details.span, format!("hint: {hints}"));
        syn_error.combine(hints_error);
      }

      Err(syn_error)
    },
  }
}

#[cfg(test)]
mod tests;
