view-types 0.2.1

A macro to create a view type data structure
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
# View-types: A Concise Way To Model Data With View Projections

[<img alt="crates.io" src="https://img.shields.io/crates/v/view-types.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/view-types)

The `views` macro provides a declarative way to define type-safe projections from a single source-of-truth data structure declaration. These projections provide different ways of representing data with overlapping fields or needing runtime validation, and minimizes the necessary boilerplate. This can even be made more powerful when combined with the builder pattern ([example](https://github.com/mcmah309/view-types/blob/1137d4bb7a20d405d01a5a6c79ddb19c158a5c89/tests/mod.rs#L182) with [bon](https://crates.io/crates/bon)).

Jump straight to [examples](#examples) to see it in action.

Article: [Solving Rust Data Modeling with View-Types: A Macro-Driven Approach](https://mcmah309.github.io/posts/solving-data-modeling-in-rust-with-view-types/)

## Syntax

### Syntax Example

```rust
use view_types::views;

fn validate_ratio(ratio: &f32) -> bool {
    *ratio >= 0.0 && *ratio <= 1.0
}

enum EnumVariant {
    Branch1(String),
    Branch2(usize),
}

#[views(
    // A fragment is a set of fields to be included in view(s)
    frag all {
        // Declaring a field to be included
        offset,
        limit,
        // Enum pattern matching extraction with explicit type declaration
        EnumVariant::Branch1(cannot_infer_type: String),
        // Result pattern matching extraction
        Ok(result1),
    }
    
    frag keyword {
        // Option pattern matching extraction
        Some(query),
        // Explicit type declaration
        words_limit: Option<usize>
    }
    
    frag semantic {
        // Option pattern matching extraction with validation
        Some(vector) if vector.len() == 768,
        mut_number
    }
    
    // A view is a projection/subset of fields
    #[derive(Debug, Clone)]
    pub view KeywordSearch {
        // Expanding a fragment to include all fields in this view
        ..all,
        ..keyword,
    }
    
    #[derive(Debug)]
    pub view SemanticSearch<'a> where 'a: 'a {
        ..all,
        ..semantic,
        // Directly declaring a field (same as in a fragment)
        semantic_only_ref
    }
    
    #[derive(Debug)]
    pub view HybridSearch<'a> {
        ..all,
        ..keyword,
        ..semantic,
        Some(ratio) if validate_ratio(ratio)
    }
)]
pub struct Search<'a> {
    query: Option<String>,
    offset: usize,
    limit: usize,
    words_limit: Option<usize>,
    vector: Option<&'a Vec<u8>>,
    ratio: Option<f32>,
    mut_number: &'a mut usize,
    field_never_used: bool,
    semantic_only_ref: &'a usize,
    cannot_infer_type: EnumVariant,
    result1: Result<usize, String>,
}
```

See the macro expansion below to understand the generated code.
<details>

<summary>Expansion</summary>

```rust,ignore
// Recursive expansion of views macro
// ===================================

pub struct Search<'a> {
    query: Option<String>,
    offset: usize,
    limit: usize,
    words_limit: Option<usize>,
    vector: Option<&'a Vec<u8>>,
    ratio: Option<f32>,
    mut_number: &'a mut usize,
    field_never_used: bool,
    semantic_only_ref: &'a usize,
    cannot_infer_type: EnumVariant,
    result1: Result<usize, String>,
}
#[derive(Debug, Clone)]
pub struct KeywordSearch {
    offset: usize,
    limit: usize,
    cannot_infer_type: String,
    result1: usize,
    query: String,
    words_limit: Option<usize>,
}
pub struct KeywordSearchRef<'original> {
    offset: &'original usize,
    limit: &'original usize,
    cannot_infer_type: &'original String,
    result1: &'original usize,
    query: &'original String,
    words_limit: &'original Option<usize>,
}
pub struct KeywordSearchMut<'original> {
    offset: &'original mut usize,
    limit: &'original mut usize,
    cannot_infer_type: &'original mut String,
    result1: &'original mut usize,
    query: &'original mut String,
    words_limit: &'original mut Option<usize>,
}
impl<'original> KeywordSearch {
    pub fn as_ref(&'original self) -> KeywordSearchRef<'original> {
        KeywordSearchRef {
            offset: &self.offset,
            limit: &self.limit,
            cannot_infer_type: &self.cannot_infer_type,
            result1: &self.result1,
            query: &self.query,
            words_limit: &self.words_limit,
        }
    }
    pub fn as_mut(&'original mut self) -> KeywordSearchMut<'original> {
        KeywordSearchMut {
            offset: &mut self.offset,
            limit: &mut self.limit,
            cannot_infer_type: &mut self.cannot_infer_type,
            result1: &mut self.result1,
            query: &mut self.query,
            words_limit: &mut self.words_limit,
        }
    }
}
#[derive(Debug)]
pub struct SemanticSearch<'a>
where
    'a: 'a,
{
    offset: usize,
    limit: usize,
    cannot_infer_type: String,
    result1: usize,
    vector: &'a Vec<u8>,
    mut_number: &'a mut usize,
    semantic_only_ref: &'a usize,
}
pub struct SemanticSearchRef<'original, 'a>
where
    'a: 'a,
{
    offset: &'original usize,
    limit: &'original usize,
    cannot_infer_type: &'original String,
    result1: &'original usize,
    vector: &'a Vec<u8>,
    mut_number: &'original usize,
    semantic_only_ref: &'a usize,
}
pub struct SemanticSearchMut<'original, 'a>
where
    'a: 'a,
{
    offset: &'original mut usize,
    limit: &'original mut usize,
    cannot_infer_type: &'original mut String,
    result1: &'original mut usize,
    vector: &'a Vec<u8>,
    mut_number: &'original mut usize,
    semantic_only_ref: &'a usize,
}
impl<'original, 'a> SemanticSearch<'a>
where
    'a: 'a,
{
    pub fn as_ref(&'original self) -> SemanticSearchRef<'original, 'a> {
        SemanticSearchRef {
            offset: &self.offset,
            limit: &self.limit,
            cannot_infer_type: &self.cannot_infer_type,
            result1: &self.result1,
            vector: &self.vector,
            mut_number: &self.mut_number,
            semantic_only_ref: &self.semantic_only_ref,
        }
    }
    pub fn as_mut(&'original mut self) -> SemanticSearchMut<'original, 'a> {
        SemanticSearchMut {
            offset: &mut self.offset,
            limit: &mut self.limit,
            cannot_infer_type: &mut self.cannot_infer_type,
            result1: &mut self.result1,
            vector: &mut self.vector,
            mut_number: &mut self.mut_number,
            semantic_only_ref: &mut self.semantic_only_ref,
        }
    }
}
#[derive(Debug)]
pub struct HybridSearch<'a> {
    offset: usize,
    limit: usize,
    cannot_infer_type: String,
    result1: usize,
    query: String,
    words_limit: Option<usize>,
    vector: &'a Vec<u8>,
    mut_number: &'a mut usize,
    ratio: f32,
}
pub struct HybridSearchRef<'original, 'a> {
    offset: &'original usize,
    limit: &'original usize,
    cannot_infer_type: &'original String,
    result1: &'original usize,
    query: &'original String,
    words_limit: &'original Option<usize>,
    vector: &'a Vec<u8>,
    mut_number: &'original usize,
    ratio: &'original f32,
}
pub struct HybridSearchMut<'original, 'a> {
    offset: &'original mut usize,
    limit: &'original mut usize,
    cannot_infer_type: &'original mut String,
    result1: &'original mut usize,
    query: &'original mut String,
    words_limit: &'original mut Option<usize>,
    vector: &'a Vec<u8>,
    mut_number: &'original mut usize,
    ratio: &'original mut f32,
}
impl<'original, 'a> HybridSearch<'a> {
    pub fn as_ref(&'original self) -> HybridSearchRef<'original, 'a> {
        HybridSearchRef {
            offset: &self.offset,
            limit: &self.limit,
            cannot_infer_type: &self.cannot_infer_type,
            result1: &self.result1,
            query: &self.query,
            words_limit: &self.words_limit,
            vector: &self.vector,
            mut_number: &self.mut_number,
            ratio: &self.ratio,
        }
    }
    pub fn as_mut(&'original mut self) -> HybridSearchMut<'original, 'a> {
        HybridSearchMut {
            offset: &mut self.offset,
            limit: &mut self.limit,
            cannot_infer_type: &mut self.cannot_infer_type,
            result1: &mut self.result1,
            query: &mut self.query,
            words_limit: &mut self.words_limit,
            vector: &mut self.vector,
            mut_number: &mut self.mut_number,
            ratio: &mut self.ratio,
        }
    }
}
pub enum SearchVariant<'a> {
    KeywordSearch(KeywordSearch),
    SemanticSearch(SemanticSearch<'a>),
    HybridSearch(HybridSearch<'a>),
}
impl<'a> SearchVariant<'a> {
    pub fn offset(&self) -> &usize {
        match self {
            SearchVariant::KeywordSearch(view) => &view.offset,
            SearchVariant::SemanticSearch(view) => &view.offset,
            SearchVariant::HybridSearch(view) => &view.offset,
        }
    }
    pub fn vector(&self) -> Option<&Vec<u8>> {
        match self {
            SearchVariant::SemanticSearch(view) => Some(&view.vector),
            SearchVariant::HybridSearch(view) => Some(&view.vector),
            _ => None,
        }
    }
    pub fn words_limit(&self) -> Option<&usize> {
        match self {
            SearchVariant::KeywordSearch(view) => view.words_limit.as_ref(),
            SearchVariant::HybridSearch(view) => view.words_limit.as_ref(),
            _ => None,
        }
    }
    pub fn semantic_only_ref(&self) -> Option<&usize> {
        match self {
            SearchVariant::SemanticSearch(view) => Some(&view.semantic_only_ref),
            _ => None,
        }
    }
    pub fn cannot_infer_type(&self) -> &String {
        match self {
            SearchVariant::KeywordSearch(view) => &view.cannot_infer_type,
            SearchVariant::SemanticSearch(view) => &view.cannot_infer_type,
            SearchVariant::HybridSearch(view) => &view.cannot_infer_type,
        }
    }
    pub fn query(&self) -> Option<&String> {
        match self {
            SearchVariant::KeywordSearch(view) => Some(&view.query),
            SearchVariant::HybridSearch(view) => Some(&view.query),
            _ => None,
        }
    }
    pub fn ratio(&self) -> Option<&f32> {
        match self {
            SearchVariant::HybridSearch(view) => Some(&view.ratio),
            _ => None,
        }
    }
    pub fn result1(&self) -> &usize {
        match self {
            SearchVariant::KeywordSearch(view) => &view.result1,
            SearchVariant::SemanticSearch(view) => &view.result1,
            SearchVariant::HybridSearch(view) => &view.result1,
        }
    }
    pub fn limit(&self) -> &usize {
        match self {
            SearchVariant::KeywordSearch(view) => &view.limit,
            SearchVariant::SemanticSearch(view) => &view.limit,
            SearchVariant::HybridSearch(view) => &view.limit,
        }
    }
    pub fn mut_number(&self) -> Option<&usize> {
        match self {
            SearchVariant::SemanticSearch(view) => Some(&view.mut_number),
            SearchVariant::HybridSearch(view) => Some(&view.mut_number),
            _ => None,
        }
    }
}
impl<'original, 'a> Search<'a> {
    pub fn into_keyword_search(self) -> Option<KeywordSearch> {
        Some(KeywordSearch {
            offset: self.offset,
            limit: self.limit,
            cannot_infer_type: if let EnumVariant::Branch1(cannot_infer_type) =
                self.cannot_infer_type
            {
                cannot_infer_type
            } else {
                return None;
            },
            result1: if let Ok(result1) = self.result1 {
                result1
            } else {
                return None;
            },
            query: if let Some(query) = self.query {
                query
            } else {
                return None;
            },
            words_limit: self.words_limit,
        })
    }
    pub fn as_keyword_search(&'original self) -> Option<KeywordSearchRef<'original>> {
        Some(KeywordSearchRef {
            offset: &self.offset,
            limit: &self.limit,
            cannot_infer_type: if let EnumVariant::Branch1(cannot_infer_type) =
                &self.cannot_infer_type
            {
                cannot_infer_type
            } else {
                return None;
            },
            result1: if let Ok(result1) = &self.result1 {
                result1
            } else {
                return None;
            },
            query: if let Some(query) = &self.query {
                query
            } else {
                return None;
            },
            words_limit: &self.words_limit,
        })
    }
    pub fn as_keyword_search_mut(&'original mut self) -> Option<KeywordSearchMut<'original>> {
        Some(KeywordSearchMut {
            offset: {
                let offset = &mut self.offset;
                offset
            },
            limit: {
                let limit = &mut self.limit;
                limit
            },
            cannot_infer_type: if let EnumVariant::Branch1(cannot_infer_type) =
                &mut self.cannot_infer_type
            {
                cannot_infer_type
            } else {
                return None;
            },
            result1: if let Ok(result1) = &mut self.result1 {
                result1
            } else {
                return None;
            },
            query: if let Some(query) = &mut self.query {
                query
            } else {
                return None;
            },
            words_limit: {
                let words_limit = &mut self.words_limit;
                words_limit
            },
        })
    }
    pub fn into_semantic_search(self) -> Option<SemanticSearch<'a>> {
        Some(SemanticSearch {
            offset: self.offset,
            limit: self.limit,
            cannot_infer_type: if let EnumVariant::Branch1(cannot_infer_type) =
                self.cannot_infer_type
            {
                cannot_infer_type
            } else {
                return None;
            },
            result1: if let Ok(result1) = self.result1 {
                result1
            } else {
                return None;
            },
            vector: if let Some(vector) = self.vector {
                {
                    let vector = &vector;
                    if !(vector.len() == 768) {
                        return None;
                    }
                }
                vector
            } else {
                return None;
            },
            mut_number: self.mut_number,
            semantic_only_ref: self.semantic_only_ref,
        })
    }
    pub fn as_semantic_search(&'original self) -> Option<SemanticSearchRef<'original, 'a>> {
        Some(SemanticSearchRef {
            offset: &self.offset,
            limit: &self.limit,
            cannot_infer_type: if let EnumVariant::Branch1(cannot_infer_type) =
                &self.cannot_infer_type
            {
                cannot_infer_type
            } else {
                return None;
            },
            result1: if let Ok(result1) = &self.result1 {
                result1
            } else {
                return None;
            },
            vector: if let Some(vector) = &self.vector {
                if !(vector.len() == 768) {
                    return None;
                }
                vector
            } else {
                return None;
            },
            mut_number: &self.mut_number,
            semantic_only_ref: &self.semantic_only_ref,
        })
    }
    pub fn as_semantic_search_mut(&'original mut self) -> Option<SemanticSearchMut<'original, 'a>> {
        Some(SemanticSearchMut {
            offset: {
                let offset = &mut self.offset;
                offset
            },
            limit: {
                let limit = &mut self.limit;
                limit
            },
            cannot_infer_type: if let EnumVariant::Branch1(cannot_infer_type) =
                &mut self.cannot_infer_type
            {
                cannot_infer_type
            } else {
                return None;
            },
            result1: if let Ok(result1) = &mut self.result1 {
                result1
            } else {
                return None;
            },
            vector: if let Some(vector) = &mut self.vector {
                {
                    let vector = &*vector;
                    if !(vector.len() == 768) {
                        return None;
                    }
                }
                vector
            } else {
                return None;
            },
            mut_number: {
                let mut_number = &mut self.mut_number;
                &mut *mut_number
            },
            semantic_only_ref: {
                let semantic_only_ref = &mut self.semantic_only_ref;
                semantic_only_ref
            },
        })
    }
    pub fn into_hybrid_search(self) -> Option<HybridSearch<'a>> {
        Some(HybridSearch {
            offset: self.offset,
            limit: self.limit,
            cannot_infer_type: if let EnumVariant::Branch1(cannot_infer_type) =
                self.cannot_infer_type
            {
                cannot_infer_type
            } else {
                return None;
            },
            result1: if let Ok(result1) = self.result1 {
                result1
            } else {
                return None;
            },
            query: if let Some(query) = self.query {
                query
            } else {
                return None;
            },
            words_limit: self.words_limit,
            vector: if let Some(vector) = self.vector {
                {
                    let vector = &vector;
                    if !(vector.len() == 768) {
                        return None;
                    }
                }
                vector
            } else {
                return None;
            },
            mut_number: self.mut_number,
            ratio: if let Some(ratio) = self.ratio {
                {
                    let ratio = &ratio;
                    if !(validate_ratio(ratio)) {
                        return None;
                    }
                }
                ratio
            } else {
                return None;
            },
        })
    }
    pub fn as_hybrid_search(&'original self) -> Option<HybridSearchRef<'original, 'a>> {
        Some(HybridSearchRef {
            offset: &self.offset,
            limit: &self.limit,
            cannot_infer_type: if let EnumVariant::Branch1(cannot_infer_type) =
                &self.cannot_infer_type
            {
                cannot_infer_type
            } else {
                return None;
            },
            result1: if let Ok(result1) = &self.result1 {
                result1
            } else {
                return None;
            },
            query: if let Some(query) = &self.query {
                query
            } else {
                return None;
            },
            words_limit: &self.words_limit,
            vector: if let Some(vector) = &self.vector {
                if !(vector.len() == 768) {
                    return None;
                }
                vector
            } else {
                return None;
            },
            mut_number: &self.mut_number,
            ratio: if let Some(ratio) = &self.ratio {
                if !(validate_ratio(ratio)) {
                    return None;
                }
                ratio
            } else {
                return None;
            },
        })
    }
    pub fn as_hybrid_search_mut(&'original mut self) -> Option<HybridSearchMut<'original, 'a>> {
        Some(HybridSearchMut {
            offset: {
                let offset = &mut self.offset;
                offset
            },
            limit: {
                let limit = &mut self.limit;
                limit
            },
            cannot_infer_type: if let EnumVariant::Branch1(cannot_infer_type) =
                &mut self.cannot_infer_type
            {
                cannot_infer_type
            } else {
                return None;
            },
            result1: if let Ok(result1) = &mut self.result1 {
                result1
            } else {
                return None;
            },
            query: if let Some(query) = &mut self.query {
                query
            } else {
                return None;
            },
            words_limit: {
                let words_limit = &mut self.words_limit;
                words_limit
            },
            vector: if let Some(vector) = &mut self.vector {
                {
                    let vector = &*vector;
                    if !(vector.len() == 768) {
                        return None;
                    }
                }
                vector
            } else {
                return None;
            },
            mut_number: {
                let mut_number = &mut self.mut_number;
                &mut *mut_number
            },
            ratio: if let Some(ratio) = &mut self.ratio {
                {
                    let ratio = &*ratio;
                    if !(validate_ratio(ratio)) {
                        return None;
                    }
                }
                ratio
            } else {
                return None;
            },
        })
    }
}
```

</details>

### Fragment-Based Grouping

Fragments allow you to group related field extractions and reuse them across multiple views:

```rust,ignore
frag all {
    offset,                                           // Simple field extraction
    limit,
    EnumVariant::Branch1(cannot_infer_type: String), // Enum pattern matching
    Ok(result1),                                      // Result unwrapping
    Err(result2)
}
```

The macro supports conditional field extraction with custom validation:

```rust,ignore
frag semantic {
    Some(vector) if vector.len() == 768,  // Conditional extraction
    mut_number
}
```

### Views

Views are projections of the annotated structs data. They contain fragments and fields to be included in the projection.

```rust
// Annotations for the generated *Ref struct
#[Ref(
    #[derive(Clone)]
)]
// Annotations for the generated *Mut struct
#[Mut(
    #[derive(Debug)]
)]
#[derive(Debug)]
pub view HybridSearch<'a> {
    // fragment expansion
    ..all,
    ..keyword,
    ..semantic,
    // direct field inclusion with pattern matching and validation (same syntax as in a fragment)
    Some(ratio) if validate_ratio(ratio)
}
```
### Configuration
#### Variant
In addition to the structs generated for each view (each view has a owned, ref, and mut struct). There is also a generated enum variant of the views. e.g.
```rust
pub enum SearchVariant<'a> {
    KeywordSearch(KeywordSearch),
    SemanticSearch(SemanticSearch<'a>),
    HybridSearch(HybridSearch<'a>),
}
```
Annotations for this type can be applied with the `Variant` annotation directly on the original struct.
```rust
#[Variant(
    #[derive(Debug)]
)]
```

## Examples

### Example Using Monolith

```rust
use view_types::views;

fn validate_table_name(name: &str) -> bool {
    name.chars().all(|c| c.is_alphanumeric() || c == '_') && !name.is_empty()
}

fn validate_limit(limit: &u32) -> bool {
    *limit > 0 && *limit <= 10000
}

#[derive(Debug, Clone)]
pub struct JoinClause {
    pub table: String,
    pub condition: String,
}

#[views(
    frag base {
        Some(table) if validate_table_name(table),
        columns,
    }

    #[derive(Debug, Clone)]
    pub view SelectQuery {
        ..base
    }
    
    #[derive(Debug, Clone)]  
    pub view PaginatedQuery {
        ..base,
        Some(limit) if validate_limit(limit),
        Some(offset),
    }
    
    #[derive(Debug, Clone)]
    pub view JoinQuery {
        ..base,
        Some(join_clauses) if !join_clauses.is_empty(),
    }
)]
#[derive(Debug)]
pub struct QueryBuilder {
    table: Option<String>,
    columns: Vec<String>,
    where_clause: Option<String>,
    limit: Option<u32>,
    offset: Option<u32>,
    join_clauses: Option<Vec<JoinClause>>,
}


fn configure_select_query(query: &SelectQueryRef, sql: &mut String) {
    let cols = if query.columns.is_empty() { "*" } else { &query.columns.join(", ") };
    sql.push_str(&format!("SELECT {} FROM {} ", cols, query.table));
}

fn configure_join_query(query: &JoinQueryRef, sql: &mut String) {
    for join in query.join_clauses {
        sql.push_str(&format!(" JOIN {} ON {}", join.table, join.condition));
    }
}

fn configure_paginated_query(query: &PaginatedQueryRef, sql: &mut String) {
    sql.push_str(&format!(" LIMIT {} OFFSET {}", query.limit, query.offset));
}

fn main() {
    // Assume unknown query configuration (could come from API request, config file, etc.)
    let query_builder = QueryBuilder {
        table: Some("users".to_string()),
        columns: vec!["id".to_string(), "name".to_string(), "email".to_string()],
        where_clause: Some("active = true".to_string()),
        limit: Some(50),
        offset: Some(0),
        join_clauses: Some(vec![JoinClause {
            table: "profiles".to_string(),
            condition: "users.id = profiles.user_id".to_string(),
        }]),
    };
    
    let mut sql = String::new();
    if let Some(query) = query_builder.as_select_query() {
        configure_select_query(&query, &mut sql);
    }
    else {
        panic!("Not valid query");
    }
    if let Some(query) = query_builder.as_join_query() {
        configure_join_query(&query, &mut sql);
    }
    if let Some(query) = query_builder.as_paginated_query() {
        configure_paginated_query(&query, &mut sql);
    }
    if let Some(where_clause) = query_builder.where_clause {
        sql.push_str(&format!(" WHERE {}", where_clause));
    }

    println!("Generated SQL Query: {}", sql);
}
```

### Example Using Generated Variant Enum

```rust
use view_types::views;

// Debug only validation
#[inline]
fn validate_health(health: &f32) -> bool {
    #[cfg(debug_assertions)]
    { *health >= 0.0 && *health <= 100.0 }
    #[cfg(not(debug_assertions))]
    { true }
}

#[derive(Debug, Clone)]
pub enum Team { Blue, Red, Neutral }

#[derive(Debug, Clone)]
pub enum WeaponType { Sword, Bow, Staff }

#[views(
    frag positioned {
        entity_id,
        position_x,
        position_y,
    }
    
    frag living {
        Some(health) if validate_health(health),
        max_health,
        team,
    }
    
    #[derive(Debug, Clone)]
    pub view Player {
        ..positioned,
        ..living,
        player_name,
        level,
        weapon,
    }
    
    #[derive(Debug, Clone)]
    pub view Npc {
        ..positioned,
        ..living,
        npc_type,
        ai_state,
    }
    
    #[derive(Debug, Clone)]
    pub view Projectile {
        ..positioned,
        velocity_x,
        velocity_y,
        team,
        damage: u32,
    }
)]
pub struct GameEntity {
    entity_id: u64,
    position_x: f32,
    position_y: f32,
    health: Option<f32>,
    max_health: f32,
    team: Team,
    damage: u32,
    player_name: String,
    level: u32,
    weapon: WeaponType,
    npc_type: String,
    ai_state: String,
    velocity_x: f32,
    velocity_y: f32,
}

fn main() {
    // Simulate game entities
    let entities = vec![
        GameEntityVariant::Player(Player {
            entity_id: 1,
            position_x: 100.0,
            position_y: 200.0,
            health: 85.0,
            max_health: 100.0,
            team: Team::Blue,
            player_name: "Alice".to_string(),
            level: 12,
            weapon: WeaponType::Sword,
        }),
        
        GameEntityVariant::Npc(Npc {
            entity_id: 2,
            position_x: 300.0,
            position_y: 150.0,
            health: 60.0,
            max_health: 80.0,
            team: Team::Neutral,
            npc_type: "Merchant".to_string(),
            ai_state: "Idle".to_string(),
        }),
        
        GameEntityVariant::Projectile(Projectile {
            entity_id: 3,
            position_x: 120.0,
            position_y: 210.0,
            velocity_x: 200.0,
            velocity_y: -50.0,
            team: Team::Blue,
            damage: 25,
        }),
    ];
    
    // Use generated getters directly - no pattern matching required!
    for entity in &entities {
        // These are common for all so it is automatically generated without an option
        println!("Entity {} at ({:.0}, {:.0})", entity.entity_id(), entity.position_x(), entity.position_y());
        let near_center = entities.iter()
            .filter(|e| {
                let dx = e.position_x() - 200.0;
                let dy = e.position_y() - 175.0;
                dx * dx + dy * dy <= 100.0 * 100.0
            })
        .count();
        println!("\nEntities near center: {}", near_center);
        
        // Access fields that exist only in some variants (uses options)
        if let Some(health) = entity.health() {
            println!("  Health: {:.0}/{:.0}", health, entity.max_health().unwrap());
        }
        if let Some(player_name) = entity.player_name() {
            println!("  Player: {}", player_name);
        }
        if let Some(npc_type) = entity.npc_type() {
            println!("  NPC: {}", npc_type);
        }
    }
    
    // Pattern matching for type-specific behavior
    for entity in &entities {
        match entity {
            GameEntityVariant::Player(player) => {
                println!("Player {} with {:?}", player.player_name, player.weapon);
            },
            GameEntityVariant::Projectile(proj) => {
                println!("Projectile: damage {}, velocity ({:.0}, {:.0})", 
                    proj.damage, proj.velocity_x, proj.velocity_y);
            },
            _ => {
                println!("Other entity type");
            }
        }
    }
}
```