nestify 0.3.2

Nestify offers a macro to simplify and beautify nested struct definitions in Rust, enabling cleaner, more readable code structures with less verbosity. It's especially valuable for handling API responses.
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
685
686
687
688
689
690
691
# Nestify


Nestify is a Rust library offering a powerful macro to streamline the definition of nested structs and enums.
Designed to improve code readability and maintainability

[<img alt="crates.io" src="https://img.shields.io/crates/v/nestify.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/nestify)
[<img alt="github" src="https://img.shields.io/badge/snowfoxsh/nestify-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/snowfoxsh/nestify)
[<img alt="License" src="https://img.shields.io/crates/l/nestify?style=for-the-badge&labelColor=555555&logo" height="20">](LICENSE)


## Abstract


Nestify re-imagines Rust struct and enum definitions with its "Type is Definition" approach,
streamlining the way you handle nested structures.
Gone are the days of flipping back and forth between type definitions—Nestify
Unifies your codebase, making your code cleaner and far more readable.

Nestify is crafted for ease of learning, with its syntax tailored to be comfortable for Rust developers. The aim is for anyone, even those unfamiliar with the Nest macro, to quickly grasp its concept upon first glance.

## Features


- Simplify nested struct and enum definitions in Rust.
- Make your codebase more readable and less verbose.
- Ideal for modeling complex API responses.
- Advanced attribute modifiers.
- Works well with Serde.
- Intuitive syntax

## Installation


Add this to your `Cargo.toml`:

```toml
[dependencies]
nestify = "0.3.1"
```

Then use the macro:

```rust
use nestify::nest;
```

> [!NOTE]
> A nightly toolchain might provide better error diagnostics

## Quick Examples


### Simple Nested Structures


Here's a quick example to show how Nestify simplifies nested struct definitions:

```rust
// Define a user profile with nested address and preferences structures
nest! {
    struct UserProfile {
        name: String,
        address: struct Address {
            street: String,
            city: String,
        },
        preferences: struct Preferences {
            newsletter: bool,
        },
    }
}
```


<details class="expand">
    <summary>
    Expand
    </summary>
    <br>

```rust
struct UserProfile {
    name: String,
    address: Address,
    preferences: Preferences,
}

struct Address {
    street: String,
    city: String,
}

struct Preferences {
    newsletter: bool,
}
```

</details>

### Simple Nested Enums

```rust
// Define a task with a nested status enum
nest! {
    struct Task {
        id: i32,
        description: String,
        status: enum Status {
            Pending,
            InProgress,
            Completed,
        },
    }
}
```

<details class="expand">
    <summary>
    Expand
    </summary>
    <br>

```rust
struct Task {
    id: i32,
    description: String,
    status: Status,
}

enum Status {
    Pending,
    InProgress,
    Completed,
}
```

</details>


## Supported definitions


Nestify supports both [structs](https://doc.rust-lang.org/reference/expressions/struct-expr.html) and [enums](https://doc.rust-lang.org/reference/items/enumerations.html).

```rust
// field structs (named)
nest! {
    struct Named {
        f: struct Nested {}
    }
}

// tuple structs (unnamed)
nest! {
    struct Unnamed(struct Nested())
}

// unit structs
nest! {
    struct Unit {
        unit: struct UnitStruct
    }
}


// enums
nest! {
    enum EnumVariants {
        Unit,
        Tuple(i32, struct TupleNested),
        Struct {
            f1: i32,

        }
        DiscriminantVariant = 1,
    }
}
// note: any variant can have a discriminant
// just as in normal rust
```

<details class="expand">
    <summary>
    Expand
    </summary>
    <br>


```rust
// field structs (named)
struct Named {
    f: Nested,
}
struct Nested {}

// tuple structs (unnamed)
struct Unnamed(Nested,);
struct Nested();

// unit structs
struct Unit {
    unit: UnitStruct,
}
struct UnitStruct;


// enums
enum EnumVariants {
    Unit,
    Tuple(i32, TupleNested),
    Struct { 
        f1: i32 
    },
    DiscriminantVariant = 1,
}
struct TupleNested;
```

</details>

## Generics

Nestify fully supports Rust's generic parameters. This compatibility ensures that you can incorporate both lifetime and type parameters within your nested struct definitions, just as you would in standard Rust code.

```rust
nest! {
    struct Example<'a, T> {
        s: &'a str,
        t: T
    }
}
```
<details class="expand">
    <summary>
    Expand
    </summary>
    <br>


```rust
struct Example<'a, T> { 
    s: &'a str, 
    t: T, 
}
```
</details>

### Nested Generics


When defining nested generics, you need to add generics to types. Enter "FishHook" syntax.
To define generics on the field use `||<...>`. This will let you specify the nested generic types.
It also works with lifetimes if needed. 

```rust
nest! {
    struct Parent<'a> {
        child : struct Child<'c, C> {
            s: &'c str,
            f: C
        } ||<'a, i32>
    }
}
```

<details class="expand">
    <summary>
    Expand
    </summary>
    <br>

```rust
struct Parent<'a> {
    child: Child<'a, i32>,
    //           ^^^^^^^^ FishHook expands to this part
}

struct Child<'c, C> {
    s: &'c str,
    f: C,
}
```

</details>

## Attributes


You can apply attributes just like you would with a normal struct.

```rust
nest! {
    #[derive(Clone)]
    struct CloneMe {}
}

let x = CloneMe {};
let cl = x.clone();
```


### Recursive Attributes **`#[meta]*`**


Using `*` syntax you can inherit attributes to child structures easily. The attribute
will propagate to each nested structure or enum. 

```rust
nest! {
    #[apply_all]*
    struct One {
        two: struct Two {
            three: struct Three {
                payload: ()
            }
        }
    }
}
```

<details class="expand">
    <summary>
    Expand
    </summary>
    <br>


```rust
#[apply_all]

struct One {
    two: Tow,
}

#[apply_all]

struct Two {
    three: Three,
}

#[apply_all]

struct Three {
    payload: (),
}
```

</details>

### Removal Syntax


#### Disable Propagation **`#[meta]/`**


You can end the recursion of an attribute with a `/` attribute modifier.
It will remove a recursive attribute from the current structure and all nested structures

```rust
nest! {
    #[nest]*
    struct One {
        two: struct Two {
            three: #[nest]/ 
            struct Three {
                four: struct Four { }
            }
        }
    }
}
```

<details class="expand">
    <summary>
    Expand
    </summary>
    <br>


```rust
#[nest]

struct One {
    two: Two,
}

#[nest]

struct Two {
    three: Three,
}

struct Three {
    four: Four,
}

struct Four {}
```

</details>

#### Disable Single **`#[meta]-`**


Using the `-` modifier will remove a recursive attribute from a single structure
To use the previous example using `-` instead of `/`:

```rust
nest! {
    #[nest]*
    struct One {
        two: struct Two {
            three: #[nest]- 
            struct Three {
                four: struct Four { }
            }
        }
    }
}
```

<details class="expand">
    <summary>
    Expand
    </summary>
    <br>


```rust
#[nest]

struct One {
    two: Two,
}

#[nest]

struct Two {
    three: Three,
}

struct Three {
    four: Four,
}

#[nest]

struct Four {}
```

</details>

### Field Attributes **`#>[meta]`**


If you structure has many defined attributes, it can become awkward to define attributes before the nested structure. To combat this, you can define attributes that apply to nested objects before fields and enum variants. This can be accomplished by using `#>[meta]` syntax. `#>` will apply the attribute to the next struct.

```rust
nest! {
    struct MyStruct {
        #>[derive(Debug)]
        f: struct DebugableStruct { } 
        // equivlent to: 
        // f: #[derive(Debug)]
        // struct DebugableStruct { }
    }
}
```


<details class="expand">
    <summary>
    Expand
    </summary>
    <br>

```rust
struct MyStruct {
    f: DebugableStruct,
}

#[derive(Debug)]

//       ^^^^^ applied to structure and not field `f`
struct DebugableStruct {}
```

</details>

#### Enum Variant Attributes

Field attributes can also be applied to an enum variant. If there are multiple items defined in a single variant then the attribute will be applied to each.

```rust
nest! {
    enum MyEnum {
        #>[derive(Debug)]
        Variant {
            // #[derive(Debug)
            one: struct One,
            // #[derive(Debug)
            two: struct Two
        }
    }
}
```

<details class="expand">
    <summary>
    Expand
    </summary>
    <br>

```rust
enum MyEnum {
    Variant {
        one: One,
        two: Two,
    }
}

#[derive(Debug)]

struct One;

#[derive(Debug)]

struct Two;
```

</details>

## Semicolons


Rust mandates semicolons to mark the end of tuple struct and unit struct declarations. Nestify, however, introduces flexibility by making this semicolon optional.

### Rust Standard


- Tuple struct: `struct MyTuple(i32, String);`
- Unit struct: `struct MyUnit;`

### Nestify Flexibility


With Nestify, you can omit the semicolon without any impact:

```rust
// Unit struct without a semicolon
nest! {
    struct MyUnit
}

// Tuple struct without a semicolon
nest! {
    struct MyTuple(i32, String)
}
```
<details class="expand">
    <summary>
    Expand
    </summary>
    <br>

```rust
struct MyUnit;
//           ^ automaticly added

struct MyTuple(i32, String);
//                         ^ automaticly added
```

</details>
<br>

This adjustment simplifies syntax, particularly in the context of defining nested structures, aligning with Nestify's goal of enhancing code readability and maintenance. Whether you include the semicolon or not, Nestify processes the definitions correctly, thanks to its domain-specific optimizations.

## Visibility 


Visibility can be altered in both parent and nested structures. It exhibits the following behavior

### Named Field Visibility

When using named fields, you must specify the desired visibility before *both* the field and the definition.

```rust
nest! {
    pub struct One {
        pub two: pub struct Two
        //|      ^^^ visibility applied to definition (b)
        //|> visibility applied to field (a)
    }
}
```

<details class="expand">
    <summary>
    Expand
    </summary>
    <br>

```rust
pub struct One { 
    pub two: Two,
    //^ (a)
}

pub struct Two;
//^ (b)
```

</details>

### Unnamed Field Visibility

Unnamed fields apply visibility to both the field and the item.

```rust
nest! {
    pub struct One(pub struct Two)
    //             ^^^ visibility applied to both field and struct
}
```


<details class="expand">
    <summary>
    Expand
    </summary>
    <br>

```rust
pub struct One(pub Two);
//             ^^^ applied here

pub struct Two;
//^ and here
```

</details>

### Enum Variants

Enum variants apply visibility just to the structure.
This is because variants inherit the base visibility of the enum.
See [E0449](https://doc.rust-lang.org/error_codes/E0449.html) for more details.

```rust
nest! {
    pub enum One {
        Two(pub struct Two)
        //  ^^^ will apply to the structure
    }
}
```

<details class="expand">
    <summary>
    Expand
    </summary>
    <br>

```rust
pub enum One { 
    Two(Two) 
}

pub struct Two;
//^ applied to structure
```

</details>

--- 

## Limitations


In Nestify, while you can work with a wide range of complex types to structure your data effectively, there's a specific limitation regarding the definition of new types directly within the generics of other types. This limitation affects scenarios where you might want to dynamically define a struct or enum inside a generic container like `Vec<T>`, `Option<T>`, or `Result<T, E>` as part of the type declaration.

The limitation is specifically around embedding a new type definition within the generic parameters of another type. For instance:

```rust
// This pattern is not supported:
struct One(Vec<struct Two { field: i32 }>);
```

Here, `struct Two` is being defined directly within the generic parameter of `Vec<T>`, which is not currently possible with Nestify.

### Another Example


To further illustrate, consider a scenario where you want to include an optional configuration struct within another struct:

```rust
// This pattern is also not supported:
struct AppConfig(Option<struct DatabaseConfig { url: String }>);
```

In this example, `struct DatabaseConfig` is defined directly within the `Option<T>` generic type in the declaration of `AppConfig`. This specific way of defining `DatabaseConfig` inline as part of the `AppConfig` declaration is not supported by Nestify at the moment.

---

## Contributing


I love contributors. I'm a bad writer, so I would love community support to improve this guide!

To make code changes:

1. Fork the repository.
2. Create a new branch for your features or bug fixes.
3. Write tests for your changes.
4. Make sure all tests pass.
5. Submit a pull request.

Standard stuff!

## License


This project is licensed under the MIT License. If you need it under a different license *Contact Me*.
MIT license support will always be maintained. Don't fear!

## Contact me


Check github for information @snowfoxsh