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
360
361
362
363
364
365
366
367
368
369
370
//! # protto
//!
//! Derive seamless conversions between `prost`-generated Protobuf types and custom Rust types.
//!
//! ## Overview
//!
//! `protto` is a procedural macro for automatically deriving
//! efficient, bidirectional conversions between Protobuf types generated by
//! [`prost`](https://github.com/tokio-rs/prost) and your native Rust structs.
//! This macro will significantly reduce boilerplate when you're working with
//! Protobufs.
//!
//! ## Features
//!
//! - **Automatic Bidirectional Conversion:** Derives `From<Proto>` and `Into<Proto>` implementations.
//! - **Primitive Type Support:** Direct mapping for Rust primitive types (`u32`, `i64`, `String`, etc.).
//! - **Option and Collections:** Supports optional fields (`Option<T>`) and collections (`Vec<T>`).
//! - **Newtype Wrappers:** Transparent conversions for single-field tuple structs.
//! - **Field Renaming:** Customize mapping between Rust and Protobuf field names using `#[protto(proto_name = "...")]`.
//! - **Custom Conversion Functions:** Handle complex scenarios with user-defined functions via `#[protto(from_proto_fn = "...")]` and `#[protto(to_proto_fn = "...")]`.
//! - **Ignored Fields:** Exclude fields from conversion using `#[protto(ignore)]`.
//! - **Advanced Error Handling:** Support for custom error types and functions.
//! - **Smart Optionality Detection:** Automatic inference with manual override capabilities.
//! - **Configurable Protobuf Module:** Defaults to searching for types in a `proto` module, customizable per struct or globally.
//!
//! ## Basic Usage
//!
//! Given Protobuf definitions compiled with `prost`:
//!
//! ```protobuf
//! syntax = "proto3";
//! package service;
//!
//! message Track {
//! uint64 track_id = 1;
//! }
//!
//! message State {
//! repeated Track tracks = 1;
//! }
//! ```
//!
//! Derive conversions in Rust:
//!
//! ```rust,ignore
//! use protto::Protto;
//!
//! mod proto {
//! tonic::include_proto!("service");
//! }
//!
//! #[derive(Protto)]
//! #[protto(module = "proto")]
//! pub struct Track {
//! #[protto(transparent, proto_name = "track_id")]
//! pub id: TrackId,
//! }
//!
//! #[derive(Protto)]
//! pub struct TrackId(u64);
//!
//! #[derive(Protto)]
//! pub struct State {
//! pub tracks: Vec<Track>,
//! }
//! ```
//!
//! ## Attribute Reference
//!
//! ### Struct-Level Attributes
//!
//! #### `#[protto(module = "path")]`
//! Specifies the module path where protobuf types are located.
//! ```rust,ignore
//! #[derive(Protto)]
//! #[protto(module = "my_proto")] // looks for types in my_proto::*
//! struct MyStruct { ... }
//! ```
//!
//! #### `#[protto(proto_name = "ProtoName")]`
//! Maps the struct to a different protobuf type name.
//! ```rust,ignore
//! #[derive(Protto)]
//! #[protto(proto_name = "StateMessage")] // maps to proto::StateMessage
//! struct State { ... }
//! ```
//!
//! #### `#[protto(error_type = ErrorType)]`
//! Sets the error type for conversions that can fail.
//! ```rust,ignore
//! #[derive(Protto)]
//! #[protto(error_type = MyError)]
//! struct MyStruct { ... }
//! ```
//!
//! #### `#[protto(error_fn = "function_name")]`
//! Specifies a function to handle conversion errors at the struct level.
//!
//! ### Field-Level Attributes
//!
//! #### `#[protto(transparent)]`
//! For newtype wrappers - directly converts the inner value.
//! ```rust,ignore
//! #[derive(Protto)]
//! struct UserId(#[protto(transparent)] u64);
//! ```
//!
//! #### `#[protto(proto_name = "proto_field_name")]`
//! Maps the field to a different name in the protobuf.
//! ```rust,ignore
//! #[protto(proto_name = "user_id")]
//! pub id: u64, // maps to proto.user_id
//! ```
//!
//! #### `#[protto(ignore)]`
//! Excludes the field from proto conversion (uses Default::default).
//! ```rust,ignore
//! #[protto(ignore)]
//! pub runtime_data: HashMap<String, String>,
//! ```
//!
//! #### Custom Conversion Functions
//!
//! ##### `#[protto(from_proto_fn = "function")]`
//! Uses a custom function for proto → rust conversion.
//! ```rust,ignore
//! #[protto(from_proto_fn = "parse_timestamp")]
//! pub created_at: DateTime<Utc>,
//! ```
//!
//! ##### `#[protto(to_proto_fn = "function")]`
//! Uses a custom function for rust → proto conversion.
//! ```rust,ignore
//! #[protto(to_proto_fn = "format_timestamp")]
//! pub created_at: DateTime<Utc>,
//! ```
//!
//! Both can be combined for bidirectional custom conversion:
//! ```rust,ignore
//! #[protto(from_proto_fn = "from_proto_map", to_proto_fn = "to_proto_map")]
//! pub metadata: HashMap<String, Value>,
//! ```
//!
//! #### Optionality Control
//!
//! ##### `#[protto(proto_optional)]`
//! Explicitly treats the proto field as optional.
//! ```rust,ignore
//! #[protto(proto_optional)]
//! pub field: String, // proto field is Option<String>, gets unwrapped
//! ```
//!
//! ##### `#[protto(proto_required)]`
//! Explicitly treats the proto field as required.
//! ```rust,ignore
//! #[protto(proto_required)]
//! pub field: Option<String>, // proto field is String, gets wrapped
//! ```
//!
//! #### Error Handling
//!
//! ##### `#[protto(expect)]`
//! Uses `.expect()` with panic on missing optional fields.
//! ```rust,ignore
//! #[protto(expect)]
//! pub required_field: String, // panics if proto field is None
//! ```
//!
//! ##### `#[protto(error_type = ErrorType)]`
//! Field-level error type override.
//!
//! ##### `#[protto(error_fn = "function")]`
//! Custom error handling function for this field.
//! ```rust,ignore
//! #[protto(expect, error_fn = "handle_missing_field")]
//! pub critical_field: String,
//! ```
//!
//! #### Default Values
//!
//! ##### `#[protto(default)]`
//! Uses `Default::default()` for missing/empty fields.
//! ```rust,ignore
//! #[protto(default)]
//! pub optional_field: String, // empty string if proto field is None
//! ```
//!
//! ##### `#[protto(default_fn = "function")]`
//! Uses a custom function for default values.
//! ```rust,ignore
//! #[protto(default_fn = "default_username")]
//! pub username: String,
//!
//! fn default_username() -> String {
//! "anonymous".to_string()
//! }
//! ```
//!
//! ## Advanced Examples
//!
//! ### Complex conversions with custom functions
//!
//! ```rust,ignore
//! use std::collections::HashMap;
//!
//! #[derive(Protto)]
//! #[protto(proto_name = "State")]
//! pub struct StateMap {
//! #[protto(from_proto_fn = "into_map", to_proto_fn = "from_map")]
//! pub tracks: HashMap<TrackId, Track>,
//! }
//!
//! pub fn into_map(tracks: Vec<proto::Track>) -> HashMap<TrackId, Track> {
//! tracks.into_iter().map(|t| (TrackId(t.track_id), t.into())).collect()
//! }
//!
//! pub fn from_map(tracks: HashMap<TrackId, Track>) -> Vec<proto::Track> {
//! tracks.into_values().map(Into::into).collect()
//! }
//! ```
//!
//! ### Ignoring runtime fields
//!
//! ```rust,ignore
//! use std::sync::atomic::AtomicU64;
//!
//! #[derive(Protto)]
//! #[protto(proto_name = "State")]
//! pub struct ComplexState {
//! pub tracks: Vec<Track>,
//! #[protto(ignore)]
//! pub counter: AtomicU64,
//! #[protto(ignore, default = "default_cache")]
//! pub cache: HashMap<String, String>,
//! }
//!
//! fn default_cache() -> HashMap<String, String> {
//! HashMap::with_capacity(100)
//! }
//! ```
//!
//! ### Handling enums
//!
//! ```protobuf
//! enum Status {
//! STATUS_OK = 0;
//! STATUS_MOVED_PERMANENTLY = 1;
//! STATUS_FOUND = 2;
//! STATUS_NOT_FOUND = 3;
//! }
//!
//! message StatusResponse {
//! Status status = 1;
//! string message = 2;
//! }
//! ```
//!
//! ```rust,ignore
//! // Enum variant names are automatically mapped (prefix removal supported)
//! #[derive(Protto)]
//! pub enum Status {
//! Ok, // maps to STATUS_OK
//! MovedPermanently, // maps to STATUS_MOVED_PERMANENTLY
//! Found, // maps to STATUS_FOUND
//! NotFound, // maps to STATUS_NOT_FOUND
//! }
//!
//! #[derive(Protto)]
//! pub struct StatusResponse {
//! pub status: Status,
//! pub message: String,
//! }
//! ```
//!
//! ### Error handling strategies
//!
//! ```rust,ignore
//! #[derive(Protto)]
//! #[protto(error_type = ConversionError)]
//! pub struct User {
//! // Panic on missing field
//! #[protto(expect)]
//! pub id: UserId,
//!
//! // Use default value
//! #[protto(default)]
//! pub name: String,
//!
//! // Custom error handling
//! #[protto(error_fn = handle_email_error)]
//! pub email: String,
//!
//! // Custom default function
//! #[protto(default_fn = default_role)]
//! pub role: UserRole,
//! }
//!
//! fn handle_email_error() -> ConversionError {
//! ConversionError::MissingEmail
//! }
//!
//! fn default_role() -> UserRole {
//! UserRole::Guest
//! }
//! ```
//!
//! ## Optionality Handling
//!
//! The macro automatically detects and handles optionality between proto and Rust types:
//!
//! | Proto Type | Rust Type | Behavior |
//! |------------|-----------|----------|
//! | `string field = 1;` | `String` | Direct assignment |
//! | `optional string field = 1;` | `Option<String>` | Direct assignment |
//! | `optional string field = 1;` | `String` | Use `#[protto(expect)]` or `#[protto(default)]` |
//! | `string field = 1;` | `Option<String>` | Wrapped in `Some()` |
//! | `repeated string items = 1;` | `Vec<String>` | Direct conversion |
//! | `repeated string items = 1;` | `Option<Vec<String>>` | `None` for empty, `Some(vec)` otherwise |
//!
//! ## Type Support
//!
//! ### Primitives
//! All Rust primitives are supported: `bool`, `u32`, `u64`, `i32`, `i64`, `f32`, `f64`, `String`
//!
//! ### Collections
//! - `Vec<T>` ↔ `repeated T`
//! - `Option<Vec<T>>` ↔ `repeated T` (with empty handling)
//! - Custom collections via `from_proto_fn`/`to_proto_fn`
//!
//! ### Optional Types
//! - `Option<T>` ↔ `optional T`
//! - Automatic unwrapping/wrapping with error handling
//!
//! ### Custom Types
//! - Any type implementing `From`/`Into` traits
//! - Newtype wrappers with `#[protto(transparent)]`
//! - Custom conversion functions
//!
//! ### Enums
//! - Rust enums ↔ proto enums (with automatic prefix handling)
//! - Custom discriminant handling
//!
//! ## Error Handling
//!
//! The macro supports multiple error handling strategies:
//!
//! 1. **Panic**: Use `#[protto(expect)]` - panics with descriptive messages
//! 2. **Default Values**: Use `#[protto(default)]` - provides sensible defaults
//! 3. **Custom Errors**: Use `#[protto(expect, error_type = T, error_fn = "f")]` - custom error handling
//! 4. **Result Types**: Generated `TryFrom` implementations for fallible conversions
//!
//! ## Limitations
//!
//! - Assumes Protobuf-generated types live in a single module (configurable).
//! - Optional Protobuf message fields use `.expect` and panic if missing (unless configured otherwise).
//! - Complex nested generics may require custom conversion functions.
//! - Recursive types require careful handling of conversion cycles.
//!
//! ## Best Practices
//!
//! 1. **Let the macro infer**: Start without attributes and add them only when needed.
//! 2. **Use transparent for newtypes**: `#[protto(transparent)]` for simple wrapper types.
//! 3. **Handle errors appropriately**: Choose between panic, default, or custom error strategies.
//! 4. **Test edge cases**: Verify behavior with None/empty values and boundary conditions.
//! 5. **Document custom functions**: Make conversion logic clear for maintenance.
// re-export the derive macro
pub use *;