1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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 {
/// #[cf_gears_toolkit_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 {
/// #[cf_gears_toolkit_macros::api_dto(request, response)]
/// pub struct UserDto { pub id: String }
/// }
/// ```
#[doc = include_str!("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, cf_gears_toolkit_macros::api_dto, ::cf_gears_toolkit_macros::api_dto
let is_api_dto = matches!(
segments.as_slice(),
["api_dto"] | [.., "cf_gears_toolkit_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/");
});
}
}
}
}
}