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
//! Emits the `<Model>Count` synthetic struct used by the
//! `select: { _count: { rel: true } }` ad-hoc accessor and by
//! schema-level relation aggregates.
//!
//! # Design
//!
//! For every model that has at least one outgoing relation, the emitter
//! produces a public struct named `<Model>Count` with one `pub <rel>:
//! Option<i64>` field per outgoing relation. The struct derives
//! `Debug`, `Clone`, and `Default` so callers can zero-initialize it
//! and fill in only the relations they selected.
//!
//! Models with **zero** outgoing relations: no struct is emitted.
//! Attempting to use `_count` on such a model becomes a compile-time
//! error (enforced in Task 14 macro lowering / Task 15 trybuild tests).
//!
//! # Deferred work
//!
//! Adding a `pub _count: Option<<Model>Count>` field to the user's own
//! model struct is intentionally deferred to Task 14. The `#[derive(Model)]`
//! macro has no way to inject new fields into the struct it is applied
//! to — the struct definition is already fixed by the time proc-macro
//! expansion runs. Task 14 will return `<Model>Count` values through a
//! richer query result type (e.g., `FindManyResult<T>` carrying a
//! parallel `Vec<Option<<Model>Count>>`).
use TokenStream;
use ;
/// Information about an outgoing relation needed to emit a single
/// `Option<i64>` field on `<Model>Count`.
/// Emit the `<Model>Count` synthetic struct, or `None` if the model
/// has no outgoing relations.
///
/// The returned `TokenStream` (when `Some`) is a top-level item — the
/// caller is responsible for placing it in the correct scope. For the
/// `#[derive(Model)]` path it should be emitted at the same level as
/// the derive output (crate-root scope, outside the `pub mod <model>`
/// module). For the schema path it should be emitted at the same
/// level as the generated model struct.