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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright (c) 2025-2026 Kirky.X
// SPDX-License-Identifier: MIT
//! 该模块定义了oxcache的宏实现,提供缓存注解功能。
use proc_macro::TokenStream;
use quote::quote;
use syn::{Expr, ItemFn, Lit, Meta, Token, parse::Parser, parse_macro_input, punctuated::Punctuated, spanned::Spanned};
#[proc_macro_attribute]
pub fn cached(args: TokenStream, item: TokenStream) -> TokenStream {
let parser = Punctuated::<Meta, Token![,]>::parse_terminated;
// Rule 12: surface parse failures as `compile_error!` with a span
// pointing at the offending argument, instead of panicking.
let args = match parser.parse(args) {
Ok(args) => args,
Err(e) => return e.to_compile_error().into(),
};
let input = parse_macro_input!(item as ItemFn);
let mut service_name = "default".to_string();
let mut ttl = quote! { None };
let mut key_pattern = None;
let mut key_prefix = None;
let mut sync_mode = false;
// T003: when true, the macro skips the cache-write side effect for the
// `Ok` path (i.e. even successful results are NOT written to the cache).
// Default `false` preserves the existing behavior (Ok results are cached).
// Renamed from `skip_errors` (misleading) to `skip_cache_write` (accurate).
let mut skip_cache_write = false;
for arg in args {
let arg_span = arg.span();
match arg {
// `sync` flag — boolean path-style argument (no value).
Meta::Path(path) if path.is_ident("sync") => {
sync_mode = true;
}
// `skip_cache_write` flag — boolean path-style argument (no value).
Meta::Path(path) if path.is_ident("skip_cache_write") => {
skip_cache_write = true;
}
Meta::NameValue(nv) => {
let nv_span = nv.path.span();
if nv.path.is_ident("service") {
match nv.value {
Expr::Lit(expr_lit) => match expr_lit.lit {
Lit::Str(lit) => service_name = lit.value(),
other => {
return syn::Error::new(
other.span(),
"`service` argument expects a string literal, e.g. `service = \"my_svc\"`",
)
.to_compile_error()
.into();
}
},
other => {
return syn::Error::new(
other.span(),
"`service` argument expects a string literal, e.g. `service = \"my_svc\"`",
)
.to_compile_error()
.into();
}
}
} else if nv.path.is_ident("ttl") {
match nv.value {
Expr::Lit(expr_lit) => match expr_lit.lit {
Lit::Int(lit) => {
// Rule 12: surface parse failures (e.g. u64
// overflow) as `compile_error!` instead of
// panicking inside the proc-macro.
let val = match lit.base10_parse::<u64>() {
Ok(v) => v,
Err(e) => {
return syn::Error::new(lit.span(), format!("invalid ttl value: {}", e))
.to_compile_error()
.into();
}
};
ttl = quote! { Some(#val) };
}
other => {
return syn::Error::new(
other.span(),
"`ttl` argument expects an integer literal, e.g. `ttl = 60`",
)
.to_compile_error()
.into();
}
},
other => {
return syn::Error::new(
other.span(),
"`ttl` argument expects an integer literal, e.g. `ttl = 60`",
)
.to_compile_error()
.into();
}
}
} else if nv.path.is_ident("key") {
match nv.value {
Expr::Lit(expr_lit) => match expr_lit.lit {
Lit::Str(lit) => key_pattern = Some(lit.value()),
other => {
return syn::Error::new(
other.span(),
"`key` argument expects a string literal, e.g. `key = \"user_{id}\"`",
)
.to_compile_error()
.into();
}
},
other => {
return syn::Error::new(
other.span(),
"`key` argument expects a string literal, e.g. `key = \"user_{id}\"`",
)
.to_compile_error()
.into();
}
}
} else if nv.path.is_ident("key_prefix") {
match nv.value {
Expr::Lit(expr_lit) => match expr_lit.lit {
Lit::Str(lit) => key_prefix = Some(lit.value()),
other => {
return syn::Error::new(
other.span(),
"`key_prefix` argument expects a string literal, e.g. `key_prefix = \"ns\"`",
)
.to_compile_error()
.into();
}
},
other => {
return syn::Error::new(
other.span(),
"`key_prefix` argument expects a string literal, e.g. `key_prefix = \"ns\"`",
)
.to_compile_error()
.into();
}
}
} else {
// Rule 12: unknown NameValue argument — surface as
// compile_error instead of silent ignore.
let unknown = nv
.path
.get_ident()
.map(|i| i.to_string())
.unwrap_or_else(|| "unknown".to_string());
return syn::Error::new(
nv_span,
format!(
"unknown `#[cached]` argument `{}`; supported: service, ttl, key, key_prefix",
unknown
),
)
.to_compile_error()
.into();
}
}
// Rule 12: unsupported argument shape (e.g. Meta::List or unknown
// path) — surface as compile_error instead of silent ignore.
_ => {
return syn::Error::new(
arg_span,
"unsupported `#[cached]` argument; supported: sync, skip_cache_write, service = \"...\", ttl = N, key = \"...\", key_prefix = \"...\"",
)
.to_compile_error()
.into();
}
}
}
// Validate: `sync` cannot be combined with `async fn`. The sync branch
// generates a plain `fn`, which is incompatible with `async` in the
// user's signature. Rule 12: emit a `compile_error!` with a span
// pointing at the attribute, instead of panicking.
if sync_mode && input.sig.asyncness.is_some() {
return syn::Error::new(
proc_macro2::Span::call_site(),
"`#[cached(sync)]` cannot be used with `async fn`; either remove `async` from the function signature or remove `sync` from the `#[cached]` arguments",
)
.to_compile_error()
.into();
}
let fn_name = &input.sig.ident;
let fn_args = &input.sig.inputs;
let fn_output = &input.sig.output;
let fn_block = &input.block;
let vis = &input.vis;
// Extract return type from fn_output for type annotations
// For Result<T, E>, we need to extract T
let return_type = match fn_output {
syn::ReturnType::Default => quote! { () },
syn::ReturnType::Type(_, ty) => {
// Try to extract T from Result<T, E>
if let syn::Type::Path(path) = &**ty {
if let Some(seg) = path.path.segments.last() {
if seg.ident == "Result" {
if let syn::PathArguments::AngleBracketed(args) = &seg.arguments {
if let Some(first_arg) = args.args.first() {
quote! { #first_arg }
} else {
quote! { #ty }
}
} else {
quote! { #ty }
}
} else {
quote! { #ty }
}
} else {
quote! { #ty }
}
} else {
quote! { #ty }
}
}
};
// Generate argument names for key generation.
// Rule 12: surface unsupported parameter shapes (destructuring) as
// compile_error! instead of silently skipping — a skipped parameter
// would silently weaken cache key uniqueness.
let mut arg_names: Vec<&syn::Ident> = Vec::new();
for arg in fn_args.iter() {
if let syn::FnArg::Typed(pat_type) = arg {
match &*pat_type.pat {
syn::Pat::Ident(pat_ident) => arg_names.push(&pat_ident.ident),
other => {
return syn::Error::new(
other.span(),
"#[cached] does not support destructured parameters; use named parameters",
)
.to_compile_error()
.into();
}
}
}
// FnArg::Receiver (self) is silently skipped — not part of cache key.
}
// Generate cloned argument names for key generation to avoid ownership issues
let arg_names_cloned: Vec<_> = arg_names
.iter()
.map(|name| {
quote! { (#name).clone() }
})
.collect();
// Generate key logic with cloned args
let key_gen_with_cloned_args = if let Some(pattern) = key_pattern {
// Custom format string pattern: "user_{id}"
quote! {
format!(#pattern)
}
} else if let Some(prefix) = key_prefix {
// Use key_prefix with default generation
if arg_names.is_empty() {
quote! { format!("{}:{}:{}", #service_name, #prefix, stringify!(#fn_name)) }
} else {
quote! {
format!("{}:{}:{}:{:?}", #service_name, #prefix, stringify!(#fn_name), (#(#arg_names_cloned),*))
}
}
} else {
// Default key generation: service:fn_name:arg1:arg2...
if arg_names.is_empty() {
quote! { format!("{}:{}", #service_name, stringify!(#fn_name)) }
} else {
quote! {
format!("{}:{}:{:?}", #service_name, stringify!(#fn_name), (#(#arg_names_cloned),*))
}
}
};
let output = if sync_mode {
// Sync branch: generate a plain `fn` (no `async`). Uses
// `get_bytes_sync` / `set_bytes_sync` which require the registered
// cache to have been built with `sync_mode(true)`.
quote! {
#vis fn #fn_name(#fn_args) #fn_output {
let cache_key = #key_gen_with_cloned_args;
// Try to get cache instance, if fails, run original function
let cache = match ::oxcache::__internal_get_cache(#service_name) {
Some(c) => c,
None => return { #fn_block },
};
// Try get from cache using sync byte-level operations.
// Deserialize failure is treated as cache miss (silent fallback).
if let Ok(Some(bytes)) = cache.get_bytes_sync(&cache_key) {
if let Ok(val) = cache.unified_serializer().deserialize::<#return_type>(&bytes) {
return ::std::result::Result::Ok(val);
}
}
// Run original function
let result = { #fn_block };
// Cache result if Ok — skipped when `skip_cache_write` is set.
// Serialize/write failures are silently ignored (result still returned).
if !#skip_cache_write {
if let Ok(ref val) = result {
if let Ok(bytes) = cache.unified_serializer().serialize(val) {
let _ = cache.set_bytes_sync(&cache_key, bytes, #ttl);
}
}
}
result
}
}
} else {
// Async branch (default, original behavior)
quote! {
#vis async fn #fn_name(#fn_args) #fn_output {
let cache_key = #key_gen_with_cloned_args;
// Try to get cache instance, if fails, run original function
let cache = match ::oxcache::__internal_get_cache(#service_name) {
Some(c) => c,
None => return async { #fn_block }.await,
};
// Try get from cache using byte-level operations.
// Deserialize failure is treated as cache miss (silent fallback).
if let Ok(Some(bytes)) = cache.get_bytes(&cache_key).await {
if let Ok(val) = cache.unified_serializer().deserialize::<#return_type>(&bytes) {
return ::std::result::Result::Ok(val);
}
}
// Run original function
let result = async { #fn_block }.await;
// Cache result if Ok — skipped when `skip_cache_write` is set.
// Serialize/write failures are silently ignored (result still returned).
if !#skip_cache_write {
if let Ok(ref val) = result {
if let Ok(bytes) = cache.unified_serializer().serialize(val) {
let _ = cache.set_bytes(&cache_key, bytes, #ttl).await;
}
}
}
result
}
}
};
output.into()
}