cargo-gears-lints 0.0.1

Dylint lint collection for cargo-gears architectural rules
extern crate rustc_ast;

use rustc_ast::{Item, ItemKind};
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};

use crate::lint_utils::is_in_domain_module_ast;

dylint_linting::declare_pre_expansion_lint! {
    /// ### What it does
    ///
    /// Checks that structs and enums in domain modules do not use the `api_dto` attribute macro.
    ///
    /// ### Why is this bad?
    ///
    /// Domain models should remain independent of API serialization concerns.
    /// The `api_dto` macro is specifically designed for API DTOs (Data Transfer Objects)
    /// and should only be used in the API layer, not in domain models.
    ///
    /// ### Example
    ///
    /// ```rust
    /// // Bad - domain model uses api_dto
    /// mod domain {
    ///     #[modkit_macros::api_dto(request, response)]
    ///     pub struct User { pub id: String }
    /// }
    /// ```
    ///
    /// Use instead:
    ///
    /// ```rust
    /// // Good - domain model without api_dto
    /// mod domain {
    ///     pub struct User { pub id: String }
    /// }
    ///
    /// // Separate DTO in API layer
    /// mod api {
    ///     #[modkit_macros::api_dto(request, response)]
    ///     pub struct UserDto { pub id: String }
    /// }
    /// ```
    #[doc = include_str!("../../docs/de01_domain_layer/de0104_no_api_dto_in_domain/README.md")]
    pub DE0104_NO_API_DTO_IN_CONTRACT,
    Deny,
    "domain models should not use api_dto macro (DE0104)"
}

impl EarlyLintPass for De0104NoApiDtoInContract {
    fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
        // Only check structs and enums
        if !matches!(item.kind, ItemKind::Struct(..) | ItemKind::Enum(..)) {
            return;
        }

        if !is_in_domain_module_ast(cx, item) {
            return;
        }

        // Check for api_dto attribute macro
        for attr in &item.attrs {
            if let rustc_ast::AttrKind::Normal(attr_item) = &attr.kind {
                let path = &attr_item.item.path;
                let segments: Vec<&str> = path
                    .segments
                    .iter()
                    .map(|s| s.ident.name.as_str())
                    .collect();

                // Check if this is an api_dto attribute
                // Handles: api_dto, modkit_macros::api_dto, ::modkit_macros::api_dto
                let is_api_dto = matches!(
                    segments.as_slice(),
                    ["api_dto"] | [.., "modkit_macros", "api_dto"]
                );

                if is_api_dto {
                    cx.span_lint(DE0104_NO_API_DTO_IN_CONTRACT, attr.span, |diag| {
                        diag.primary_message("domain type should not use `api_dto` macro (DE0104)");
                        diag.help("api_dto is for API DTOs; use plain structs in domain/ and create DTOs in api/rest/");
                    });
                }
            }
        }
    }
}