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
//! # Derive macros for jacquard lexicon types
//!
//! This crate provides attribute and derive macros for working with Jacquard types.
//! The code generator uses `#[lexicon]` and `#[open_union]` to add lexicon-specific behavior.
//! You'll use `#[derive(IntoStatic)]` frequently, `#[derive(XrpcRequest)]` when defining
//! custom XRPC endpoints, and `#[derive(LexiconSchema)]` for reverse codegen (Rust → lexicon).
//!
//! ## Macros
//!
//! ### `#[lexicon]`
//!
//! Adds an `extra_data` field to structs to capture unknown fields during deserialization.
//! This makes objects "open" - they'll accept and preserve fields not defined in the schema.
//!
//! ```ignore
//! #[lexicon]
//! struct Post<'s> {
//! text: &'s str,
//! }
//! // Expands to add:
//! // #[serde(flatten)]
//! // pub extra_data: BTreeMap<SmolStr, Data<'s>>
//! ```
//!
//! ### `#[open_union]`
//!
//! Adds an `Unknown(Data)` variant to enums to make them extensible unions. This lets
//! enums accept variants not defined in your code, storing them as loosely typed atproto `Data`.
//!
//! ```ignore
//! #[open_union]
//! enum RecordEmbed<'s> {
//! #[serde(rename = "app.bsky.embed.images")]
//! Images(Images),
//! }
//! // Expands to add:
//! // #[serde(untagged)]
//! // Unknown(Data<'s>)
//! ```
//!
//! ### `#[derive(IntoStatic)]`
//!
//! Derives conversion from borrowed (`'a`) to owned (`'static`) types by recursively calling
//! `.into_static()` on all fields. Works with structs and enums.
//!
//! ```ignore
//! #[derive(IntoStatic)]
//! struct Post<'a> {
//! text: CowStr<'a>,
//! }
//! // Generates:
//! // impl IntoStatic for Post<'_> {
//! // type Output = Post<'static>;
//! // fn into_static(self) -> Self::Output { ... }
//! // }
//! ```
//!
//! ### `#[derive(XrpcRequest)]`
//!
//! Derives XRPC request traits for custom endpoints. Generates the response marker struct
//! and implements `XrpcRequest` (and optionally `XrpcEndpoint` for server-side).
//!
//! ```ignore
//! #[derive(Serialize, Deserialize, XrpcRequest)]
//! #[xrpc(
//! nsid = "com.example.getThing",
//! method = Query,
//! output = GetThingOutput,
//! )]
//! struct GetThing<'a> {
//! #[serde(borrow)]
//! pub id: CowStr<'a>,
//! }
//! // Generates:
//! // - GetThingResponse struct
//! // - impl XrpcResp for GetThingResponse
//! // - impl XrpcRequest for GetThing
//! ```
//!
//! ### `#[derive(LexiconSchema)]`
//!
//! Derives `LexiconSchema` trait for reverse codegen (Rust → lexicon JSON). Generate
//! lexicon schemas from your Rust types for rapid prototyping and custom lexicons.
//!
//! **Type-level attributes** (`#[lexicon(...)]`):
//! - `nsid = "..."`: The lexicon NSID (required)
//! - `record`: Mark as a record type
//! - `key = "..."`: Record key type (`"tid"`, `"literal:self"`, or custom) - optional
//! - `object`: Mark as an object type (default if neither record/procedure/query)
//! - `fragment = "..."`: Fragment name for non-main defs (e.g., `fragment = "textSlice"`)
//!
//! **Field-level attributes** (`#[lexicon(...)]`):
//! - `max_length = N`: Max byte length for strings
//! - `max_graphemes = N`: Max grapheme count for strings
//! - `min_length = N`, `min_graphemes = N`: Minimum constraints
//! - `minimum = N`, `maximum = N`: Integer range constraints
//! - `max_items = N`: Max array length
//! - `item_max_length = N`, `item_max_graphemes = N`: Constraints on array items
//! - `ref = "..."`: Explicit type ref (e.g., `ref = "com.atproto.repo.strongRef"` or `ref = "#textSlice"`)
//! - `union`: Mark field as union type (use with `#[lexicon_union]` enum)
//!
//! **Serde integration**: Respects `#[serde(rename)]`, `#[serde(rename_all)]`, and
//! `#[serde(skip)]`. Defaults to camelCase for field names (lexicon standard).
//!
//! **Unions**: Use `#[lexicon_union]` attribute macro, not `#[derive(LexiconSchema)]`.
//! Mark union fields with `#[lexicon(union)]`.
//!
//! ```ignore
//! // Record with constraints and fragments
//! #[derive(LexiconSchema)]
//! #[lexicon(nsid = "app.bsky.feed.post", record, key = "tid")]
//! #[serde(rename_all = "camelCase")]
//! struct Post<'a> {
//! #[lexicon(max_graphemes = 300, max_length = 3000)]
//! pub text: CowStr<'a>,
//!
//! pub created_at: Datetime, // -> "createdAt" (camelCase)
//!
//! #[lexicon(union)]
//! pub embed: Option<PostEmbed<'a>>,
//!
//! #[lexicon(max_items = 8, item_max_length = 640, item_max_graphemes = 64)]
//! pub tags: Option<Vec<CowStr<'a>>>,
//!
//! #[lexicon(ref = "app.bsky.richtext.facet")]
//! pub facets: Option<Vec<CowStr<'a>>>,
//! }
//!
//! // Fragment (non-main def)
//! #[derive(LexiconSchema)]
//! #[lexicon(nsid = "app.bsky.feed.post", fragment = "textSlice")]
//! #[serde(rename_all = "camelCase")]
//! struct TextSlice {
//! #[lexicon(minimum = 0)]
//! pub start: i64,
//! #[lexicon(minimum = 0)]
//! pub end: i64,
//! }
//!
//! // Union (uses lexicon_union, not LexiconSchema)
//! #[lexicon_union]
//! #[serde(tag = "$type")]
//! enum PostEmbed<'a> {
//! #[serde(rename = "app.bsky.embed.images")]
//! Images(CowStr<'a>),
//! #[serde(rename = "app.bsky.embed.video")]
//! Video(CowStr<'a>),
//! }
//! ```
use TokenStream;
/// Attribute macro that adds an `extra_data` field to structs to capture unknown fields
/// during deserialization.
///
/// See crate documentation for examples.
/// Attribute macro that adds an `Unknown(Data)` variant to enums to make them open unions.
///
/// See crate documentation for examples.
/// Derive macro for `IntoStatic` trait.
///
/// Automatically implements conversion from borrowed to owned ('static) types.
/// See crate documentation for examples.
/// Derive macro for `XrpcRequest` trait.
///
/// Automatically generates the response marker struct, `XrpcResp` impl, and `XrpcRequest` impl
/// for an XRPC endpoint. See crate documentation for examples.
/// Derive macro for `LexiconSchema` trait.
///
/// Generates `LexiconSchema` trait impl from Rust types for reverse codegen (Rust → lexicon JSON).
/// Produces lexicon schema definitions and runtime validation code from your type definitions.
///
/// **What it generates:**
/// - `impl LexiconSchema` with `nsid()`, `schema_id()`, and `lexicon_doc()` methods
/// - `validate()` method that checks constraints at runtime
/// - `inventory::submit!` registration for schema discovery
///
/// **Attributes:** `#[lexicon(...)]` and `#[nsid = "..."]` on types and fields.
/// See crate docs for full attribute reference and examples.
/// Attribute macro for union enums.
///
/// Marks an enum as a lexicon union type and generates a const containing the union refs
/// extracted from `#[nsid = "..."]` or `#[serde(rename = "...")]` attributes on variants.
///
/// ```ignore
/// #[lexicon_union]
/// #[serde(tag = "$type")]
/// pub enum PostEmbed<'a> {
/// #[serde(rename = "app.bsky.embed.images")]
/// Images(Images<'a>),
/// #[nsid = "app.bsky.embed.video"]
/// Video(Video<'a>),
/// }
/// // Generates: PostEmbed::LEXICON_UNION_REFS const
/// ```