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! {
#[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) {
if !matches!(item.kind, ItemKind::Struct(..) | ItemKind::Enum(..)) {
return;
}
if !is_in_domain_module_ast(cx, item) {
return;
}
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();
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/");
});
}
}
}
}
}