waddling-errors-macros 0.7.0

Procedural macros for structured error codes with compile-time validation and taxonomy enforcement
Documentation
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
# waddling-errors-macros


**Procedural macros for ergonomic error code definitions**

[![Crates.io](https://img.shields.io/crates/v/waddling-errors-macros.svg)](https://crates.io/crates/waddling-errors-macros)
[![Documentation](https://docs.rs/waddling-errors-macros/badge.svg)](https://docs.rs/waddling-errors-macros)
[![License](https://img.shields.io/crates/l/waddling-errors-macros.svg)](../LICENSE)

---

## Overview


This crate provides procedural macros for [waddling-errors](../waddling-errors), reducing boilerplate by ~70% compared to the manual trait-based approach.

**Manual approach:**
```rust
// Define enum
#[derive(Debug, Copy, Clone)]

enum Component { Auth }

// Implement trait
impl ComponentId for Component {
    fn as_str(&self) -> &'static str {
        match self { Component::Auth => "AUTH" }
    }
}

// Create constant
const ERR: Code<Component, Primary> = Code::error(Component::Auth, Primary::Token, 1);
```

**Macro approach:**
```rust
component! { Auth { value: "AUTH", docs: "Authentication" } }
diag! { E.AUTH.TOKEN.MISSING: { message: "Token missing" } }
```

---

## Installation


```toml
[dependencies]
waddling-errors = "0.7"
waddling-errors-macros = "0.7"

# For documentation generation

[features]
doc-gen = ["waddling-errors-macros/doc-gen", "waddling-errors-macros/metadata"]
```

---

## Quick Start


> ⚠️ **Critical:** You **must** call `setup!` at your crate root before `diag!` will work. This is easy to miss!

### Step 1: Setup at Crate Root (Required)


```rust
// src/lib.rs or src/main.rs - MUST be at crate root level
use waddling_errors_macros::setup;

// Configure where your definitions live
setup! {
    components = crate::components,
    primaries = crate::primaries,
    sequences = crate::sequences,
}

// If using default paths, you can use empty setup:
// setup! {}
```

### Step 2: Define Building Blocks


```rust
// src/components.rs
use waddling_errors_macros::component;

component! {
    Auth {
        value: "AUTH",
        docs: "Authentication and authorization",
        tags: ["security", "user-management"],
    },
}
```

```rust
// src/primaries.rs
use waddling_errors_macros::primary;

primary! {
    Token {
        value: "TOKEN",
        docs: "Token-related errors",
        related: ["Session"],
    },
}
```

### Step 3: Define Diagnostics (Anywhere)


```rust
// src/errors.rs (or any module - setup! makes this work)
use waddling_errors_macros::diag;

diag! {
    E.AUTH.TOKEN.EXPIRED: {
        message: "JWT token expired at {{timestamp}}",
        fields: [timestamp],
        
        'CR 'Pub description: "Your session has expired. Please log in again.",
        'CR 'Dev description: "Token TTL exceeded. Check refresh logic.",
        'CR 'Int description: "Query auth_tokens table for debug info.",
        
        'R role: "Public",
        'R tags: ["auth", "session"],
    },
}

// Use the generated constant
let error = E_AUTH_TOKEN_EXPIRED;
println!("{}", error.runtime.full_code());  // E.AUTH.TOKEN.EXPIRED
```

### Common Mistake


```rust
// ❌ WRONG: Missing setup! - diag! won't find your definitions
use waddling_errors_macros::diag;
diag! { E.AUTH.TOKEN.001: { message: "Error" } }  // Compile error!

// ✅ CORRECT: Add setup! at crate root first
// In lib.rs: setup! { components = crate::components, ... }
```

---

## Macros


### `component!` - Define Components


```rust
component! {
    ComponentName {
        value: "COMPONENT_VALUE",              // Required: UPPER_SNAKE_CASE
        docs: "Component description",         // Optional
        tags: ["tag1", "tag2"],               // Optional
        related: ["OtherComponent"],          // Optional
    },
}
```

### `primary!` - Define Primary Categories


```rust
primary! {
    CategoryName {
        value: "CATEGORY_VALUE",              // Required: UPPER_SNAKE_CASE
        docs: "Category description",         // Optional
        tags: ["tag1", "tag2"],              // Optional
        related: ["OtherCategory"],          // Optional
    },
}
```

### `sequence!` - Define Sequences


```rust
pub mod sequences {
    use waddling_errors_macros::sequence;
    
    sequence! {
        MISSING(1) {
            description: "Required item not provided",
            typical_severity: "Error",
            hints: ["Check required parameters"],
        },
    }
}
```

### `diag!` - Define Diagnostics


```rust
diag! {
    // Optional: Auto-register for doc generation
    <json, html, catalog>,
    
    SEVERITY.COMPONENT.PRIMARY.SEQUENCE: {
        message: "Error for {{pii/email}} at {{timestamp}}",
        fields: [timestamp],       // Non-PII fields → {{field}} in message
        pii: [email],              // PII fields → {{pii/field}} in message
        
        // Role-based descriptions
        'CR 'Pub description: "User-facing description",
        'CR 'Dev description: "Developer description",
        'CR 'Int description: "Internal team description",
        
        // Metadata
        'R role: "Public",
        'R tags: ["tag1", "tag2"],
        'C docs_url: "https://docs.example.com/errors",
    },
}
```

**Field Placeholders:**
- `{{field}}` - Non-PII field, sent in `f` object in wire protocol
- `{{pii/field}}` - PII field, sent in `pii.data` object for access-controlled handling

### `component_location!` - Mark Component Locations


Mark files as containing code for a component (for documentation):

```rust
use waddling_errors_macros::component_location;

// Simple: mark file as Auth component (default: internal role)
component_location!(Auth);

// With explicit role
component_location!(Auth, role = public);
component_location!(Database, role = developer);

// Multiple components per file
component_location!(Auth, role = public);
component_location!(Api, role = developer);
```

Auto-registers with `ctor` - no manual registration needed!

### `#[in_component]` - Attribute for Modules


Mark a module as belonging to a component:

```rust
use waddling_errors_macros::in_component;

#[in_component(Auth)]

mod auth_impl {
    // Implementation code
}

#[in_component(Auth, role = public)]

mod auth_example {
    // Example code for public documentation
}
```

---

## Visibility Markers


Control where metadata appears using visibility markers:

| Marker | Context | Use Case |
|--------|---------|----------|
| `'C` | Compile-time only | Documentation, code snippets, URLs |
| `'R` | Runtime only | Role, tags, runtime categorization |
| `'CR` or `'RC` | Both contexts | Descriptions, hints, messages |

**Why this matters:**
- **Smaller binaries** - Documentation doesn't bloat production code
- **Flexible content** - Different hints for docs vs runtime
- **Security** - Keep sensitive info out of binaries

### Example


```rust
diag! {
    E.AUTH.SECRET.ROTATION_FAILED: {
        message: "Secret rotation failed",
        
        // Documentation: verbose explanation
        'C description: "Key rotation failed during HSM communication. \
                        Check network connectivity to key management service...",
        
        // Runtime: concise message
        'R description: "Key rotation failed",
        
        // Both: end-user hint
        'CR hints: ["Contact security team if issue persists"],
        
        // Documentation only
        'C code_snippet: {
            wrong: "rotate_key()",
            correct: "rotate_key().with_retry(3)",
        },
        
        // Runtime only
        'R role: "Internal",
    },
}
```

### Field Defaults


When no marker is specified:

| Field | Default | Rationale |
|-------|---------|-----------|
| `description`, `hints` | `'CR` | Useful in both contexts |
| `role`, `tags`, `related_codes` | `'R` | Runtime behavior |
| `code_snippet`, `docs_url`, `introduced`, `deprecated` | `'C` | Documentation only |

---

## Role-Based Documentation


Support three documentation roles with gated fields:

```rust
diag! {
    E.AUTH.TOKEN.EXPIRED: {
        message: "Token expired",
        
        // Public: sanitized, safe for end users
        'CR 'Pub description: "Your session has expired.",
        'CR 'Pub hints: ["Click login button"],
        
        // Developer: debugging context
        'CR 'Dev description: "JWT token TTL exceeded (3600s default).",
        'CR 'Dev hints: ["Check token refresh logic", "Verify server time sync"],
        
        // Internal: full transparency
        'CR 'Int description: "Token expired. Redis key: auth:token:{user_id}",
        'CR 'Int hints: ["Query auth_tokens table", "Check token_refresh_log"],
        
        'R role: "Public",  // Minimum role to see this error
    },
}
```

**Generated constants access role-specific fields:**

```rust
// Runtime access
let hints_pub = error.hints_for_role(Role::Public);
let hints_dev = error.hints_for_role(Role::Developer);
let hints_int = error.hints_for_role(Role::Internal);
```

---

## Component Location Tracking


Mark where components are implemented with role-based filtering:

```rust
use waddling_errors_macros::in_component;

// Public documentation example (visible to all)
#[in_component(Auth, role = public)]

mod auth_example {
    // Example code
}

// Internal implementation (default: internal role)
#[in_component(Auth)]

mod auth_internals {
    // Internal implementation
}

// Developer debugging utilities
#[in_component(Auth, role = developer)]

mod auth_debug {
    // Debug utilities
}
```

**Security**: File paths are filtered by role in generated documentation!

---

## Compile-Time Validation


Enable strict validation to catch errors at compile time:

```rust
#[validate_strict]

component! {
    Auth {
        value: "AUTH",  // Must be UPPER_SNAKE_CASE
        docs: "Authentication",
    }
}

#[validate_strict]

diag! {
    E.AUTH.TOKEN.INVALID: {
        message: "Invalid token",
        // Compiler checks:
        // - Component "AUTH" exists
        // - Primary "TOKEN" exists  
        // - Sequence value is valid
        // - Severity is valid
    }
}
```

Use `#[validate_relaxed]` for prototyping.

See [VALIDATION.md](VALIDATION.md) for complete guide.

---

## Auto-Registration


Automatically register diagnostics for documentation generation:

```rust
diag! {
    <json, html, catalog>,  // Auto-register with these renderers
    
    E.AUTH.TOKEN.EXPIRED: {
        message: "Token expired",
        'CR 'Pub description: "Session expired",
    },
}
```

Enable with `auto-register` feature:

```toml
[dependencies]
waddling-errors-macros = { version = "0.7", features = ["auto-register"] }
```

## Documentation Generation Workflow


Complete end-to-end workflow for generating documentation:

### Step 1: Define Errors with Auto-Registration


```rust
// src/errors/mod.rs
use waddling_errors_macros::{component, primary, diag, setup};

// Configure paths (in lib.rs or main.rs)
setup! {
    components = crate::errors,
    primaries = crate::errors,
}

component! { Auth { value: "AUTH" } }
primary! { Token { value: "TOKEN" } }

diag! {
    <json, html>,  // Auto-register for these formats
    
    E.AUTH.TOKEN.EXPIRED: {
        message: "JWT token expired at {{timestamp}}",
        fields: [timestamp],
        
        'CR 'Pub description: "Your session has expired. Please log in again.",
        'CR 'Dev hints: ["Check token refresh logic", "Verify server time sync"],
        'CR 'Int hints: ["Query auth_tokens table for debug info"],
        
        'R role: "Public",
        'R tags: ["auth", "security"],
    },
}
```

### Step 2: Create Separate Doc Generation Binary


```rust
// src/bin/doc_gen.rs
use my_app::errors::*;  // Imports all errors → triggers auto-registration
use waddling_errors::doc_generator::{DocRegistry, HtmlRenderer, JsonRenderer};
use waddling_errors::registry;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut doc_registry = DocRegistry::new(
        env!("CARGO_PKG_NAME"),
        env!("CARGO_PKG_VERSION")
    );
    
    // Transfer auto-registered diagnostics from global registry
    registry::register_all_with_doc_gen(&mut doc_registry);
    
    let (diag_count, comp_count, prim_count, seq_count, _) = registry::statistics();
    println!("📊 Registered: {} diagnostics, {} components", diag_count, comp_count);
    
    // Generate documentation for all roles
    doc_registry.render_all_roles(
        vec![Box::new(HtmlRenderer::new()), Box::new(JsonRenderer)],
        "target/docs"
    )?;
    
    println!("✅ Documentation generated:");
    println!("   📘 target/docs/{}-pub.html", env!("CARGO_PKG_NAME"));
    println!("   👨‍💻 target/docs/{}-dev.html", env!("CARGO_PKG_NAME"));
    println!("   🔒 target/docs/{}-int.html", env!("CARGO_PKG_NAME"));
    
    Ok(())
}
```

### Step 3: Configure Cargo.toml


```toml
# In your Cargo.toml


[dependencies]
waddling-errors = { version = "0.7", features = ["metadata"] }
waddling-errors-macros = { version = "0.7", features = ["metadata", "auto-register"] }

[dev-dependencies]
waddling-errors = { version = "0.7", features = ["doc-gen", "auto-register"] }

[[bin]]
name = "doc-gen"
path = "src/bin/doc_gen.rs"
required-features = ["waddling-errors/doc-gen", "waddling-errors-macros/auto-register"]
```

### Step 4: Generate Documentation


```bash
# Generate docs

cargo run --bin doc-gen --features waddling-errors/doc-gen,waddling-errors-macros/auto-register

# Or create a cargo alias in .cargo/config.toml:

# [alias]

# docs = "run --bin doc-gen --features waddling-errors/doc-gen,waddling-errors-macros/auto-register"

cargo docs
```

### Why This Pattern?


**✅ Advantages:**
- **Clean main binary**: No doc-gen dependencies, no error imports
- **Zero boilerplate**: `<json, html>` in `diag!` handles registration
- **Separate concerns**: Doc generation isolated from app logic
- **Production-ready**: Main binary has zero doc-gen overhead
- **Idiomatic Rust**: Same pattern as criterion benchmarks, cargo-bench

**Production binary** (`cargo build --release`):
- No serde, serde_json
- No HTML/JSON renderers
- No doc generation code
- Only lightweight error structs with WDP hashes

**Doc-gen binary** (`cargo run --bin doc-gen`):
- Only runs during development/CI
- Never ships to production
- Loads errors → triggers auto-registration → generates docs

---

## Generated Constants


The `diag!` macro generates up to three constants:

```rust
// ✅ Always generated (no feature flags needed)
pub const E_AUTH_TOKEN_EXPIRED: DiagnosticRuntime = /* ... */;

// 📚 Only with 'metadata' feature
#[cfg(feature = "metadata")]

pub const E_AUTH_TOKEN_EXPIRED_DOCS: DiagnosticDocs = /* ... */;

#[cfg(feature = "metadata")]

pub const E_AUTH_TOKEN_EXPIRED_COMPLETE: DiagnosticComplete = /* ... */;
```

**Usage:**

```rust
// Production: lightweight runtime
let error = E_AUTH_TOKEN_EXPIRED;
println!("Code: {}", error.runtime.full_code());
println!("Message: {}", error.runtime.message);

// Documentation generation: full metadata
#[cfg(feature = "metadata")]

fn generate_docs(registry: &mut DocRegistry) {
    let complete = E_AUTH_TOKEN_EXPIRED_COMPLETE;
    registry.register_diagnostic(&complete)?;
}
```

---

## Examples


Run examples to see macros in action:

```bash
# Complete system with macros (~70% less code than manual)
cargo run --example complete_system --features "metadata,doc-gen,auto-register"

# Browser-server catalog for IoT/mobile

cargo run --example browser_server_catalog --features "metadata,hash"

# Component location security

cargo run --example component_location_security --features "metadata,doc-gen"

# Compile-time validation

cargo run --example strict_validation_demo --features "metadata"

# Custom XML renderer

cargo run --example custom_xml_renderer --features "metadata,doc-gen,auto-register"

# WASM/no_std

cargo run --example no_std_wasm --features "metadata"
```

See [examples/](examples/) directory for all examples.

---

## Manual vs Macro Approach


| Aspect | Manual | Macros |
|--------|--------|--------|
| **Boilerplate** | High (~100 lines per component) | Low (~10 lines) |
| **Type safety** | Manual trait impl | Automatic |
| **Validation** | Runtime only | Compile-time + runtime |
| **Role gating** | Manual filtering | Automatic |
| **Auto-registration** | Manual calls | Automatic (with feature) |
| **Learning curve** | Steep (traits + generics) | Gentle (declarative) |
| **Flexibility** | Full control | Sufficient for most use cases |

**Use manual approach when:**
- You need maximum flexibility
- You're integrating with existing trait-based code
- You want to avoid proc macros

**Use macro approach when:**
- You want less boilerplate (~70% reduction)
- You want compile-time validation
- You're starting a new project
- You want automatic doc generation

See [waddling-errors/examples/complete_system](../waddling-errors/examples/complete_system) for manual approach comparison.

---

## Features


| Feature | Description | Default |
|---------|-------------|---------|
| `metadata` | Enable compile-time documentation metadata ||
| `doc-gen` | Enable documentation generation (includes metadata) ||
| `auto-register` | Automatic diagnostic registration ||
| `hash` | Base62 hash code generation ||

---

## Documentation


- **[Examples]examples/README.md** - Comprehensive examples with explanations
- **[Validation Guide]VALIDATION.md** - Compile-time validation modes
- **[Component Location Roles]../docs/COMPONENT_LOCATION_ROLES.md** - File path security
- **[In-Component Attribute]../docs/IN_COMPONENT_ROLE_SUPPORT.md** - Component tracking
- **[API Documentation]https://docs.rs/waddling-errors-macros** - Full API reference

---

## When to Use Macros


**✅ Perfect For:**
- New projects starting fresh
- Teams wanting less boilerplate
- Projects needing compile-time validation
- Documentation-heavy error systems
- Multi-role security requirements

**❌ Consider Manual Approach:**
- Maximum flexibility needed
- Existing trait-based codebase
- Proc macro avoidance required
- Learning trait system is goal

---

## License


Dual-licensed under MIT or Apache-2.0. See [LICENSE-MIT](LICENSE-MIT) and [LICENSE-APACHE](LICENSE-APACHE).