Dillon McMahon
14 min read

Categories

Tags

In the previous article, Patterns for Modeling Overlapping Variant Data in Rust, six different approaches to modeling overlapping data structures were explored, each with their own trade-offs between type safety, code duplication, and API flexibility. Today, I want to introduce a solution that addresses many of these challenges: the view-types macro.

Recap: The Challenge

When building complex systems, we often need to model data that has overlapping but not identical field requirements. In the search engine example, we had:

  • Keyword Search: Needs query, filter, and common pagination fields
  • Semantic Search: Needs vector embeddings and common fields
  • Hybrid Search: Needs most of keyword and semantic fields plus a ratio

The six approaches discussed had their own benefits and drawbacks.

Enter View-Types: A Macro-Driven Solution

The view-types crate was created to address some of the drawbacks of the previously discussed approaches. The crate provides a views macro for a declarative way to solve this problem by generating type-safe projections from a single source-of-truth data structure. The macro generates the code to quickly implement Approach 4 (enum with complete structs) and Approach 5 (monolithic with kind), as well as associated structures and methods.

For detailed documentation on how the macro works, see the README.

Application

Instead of choosing between the various manual approaches, you define your complete data structure once and declare which fields belong to which “views”:

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
use view_types::views;

#[views(
    // Define fragments - reusable field groupings
    frag common {
        offset,
        limit,
        time_budget,
        ranking_score_threshold,
    }
    
    frag keyword_fields {
        Some(query),
        terms_matching_strategy,
        scoring_strategy,
        locales,
    }
    
    frag semantic_fields {
        Some(semantic),
    }
    
    // Define views - specific projections of your data
    pub view KeywordSearch<'a> {
        ..common,
        ..keyword_fields,
        Some(filter),
    }
    
    pub view SemanticSearch {
        ..common,
        ..semantic_fields,
    }
    
    pub view HybridSearch {
        ..common,
        ..keyword_fields,
        ..semantic_fields,
        Some(semantic_ratio),
    }
)]
pub struct Search<'a> {
    query: Option<String>,
    filter: Option<Filter<'a>>,
    offset: usize,
    limit: usize,
    time_budget: TimeBudget,
    ranking_score_threshold: Option<f64>,
    terms_matching_strategy: TermsMatchingStrategy,
    scoring_strategy: ScoringStrategy,
    locales: Option<Vec<Language>>,
    semantic: Option<Vec<u8>>,
    semantic_ratio: Option<f32>,
    // kind: SearchKind // optional kind for "kind" approach, but not needed for macro
}

// pub enum SearchKind {
//     Keyword,
//     Semantic,
//     Hybrid,
// }
Macro Expansion
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
// Recursive expansion of views macro
// ===================================

pub struct Search<'a> {
    query: Option<String>,
    filter: Option<Filter<'a>>,
    offset: usize,
    limit: usize,
    time_budget: TimeBudget,
    ranking_score_threshold: Option<f64>,
    terms_matching_strategy: TermsMatchingStrategy,
    scoring_strategy: ScoringStrategy,
    locales: Option<Vec<Language>>,
    semantic: Option<Vec<u8>>,
    semantic_ratio: Option<f32>,
}
pub struct KeywordSearch<'a> {
    offset: usize,
    limit: usize,
    time_budget: TimeBudget,
    ranking_score_threshold: Option<f64>,
    query: String,
    terms_matching_strategy: TermsMatchingStrategy,
    scoring_strategy: ScoringStrategy,
    locales: Option<Vec<Language>>,
    filter: Filter<'a>,
}
pub struct KeywordSearchRef<'original, 'a> {
    offset: &'original usize,
    limit: &'original usize,
    time_budget: &'original TimeBudget,
    ranking_score_threshold: &'original Option<f64>,
    query: &'original String,
    terms_matching_strategy: &'original TermsMatchingStrategy,
    scoring_strategy: &'original ScoringStrategy,
    locales: &'original Option<Vec<Language>>,
    filter: &'original Filter<'a>,
}
pub struct KeywordSearchMut<'original, 'a> {
    offset: &'original mut usize,
    limit: &'original mut usize,
    time_budget: &'original mut TimeBudget,
    ranking_score_threshold: &'original mut Option<f64>,
    query: &'original mut String,
    terms_matching_strategy: &'original mut TermsMatchingStrategy,
    scoring_strategy: &'original mut ScoringStrategy,
    locales: &'original mut Option<Vec<Language>>,
    filter: &'original mut Filter<'a>,
}
impl<'original, 'a> KeywordSearch<'a> {
    pub fn as_ref(&'original self) -> KeywordSearchRef<'original, 'a> {
        KeywordSearchRef {
            offset: &self.offset,
            limit: &self.limit,
            time_budget: &self.time_budget,
            ranking_score_threshold: &self.ranking_score_threshold,
            query: &self.query,
            terms_matching_strategy: &self.terms_matching_strategy,
            scoring_strategy: &self.scoring_strategy,
            locales: &self.locales,
            filter: &self.filter,
        }
    }
    pub fn as_mut(&'original mut self) -> KeywordSearchMut<'original, 'a> {
        KeywordSearchMut {
            offset: &mut self.offset,
            limit: &mut self.limit,
            time_budget: &mut self.time_budget,
            ranking_score_threshold: &mut self.ranking_score_threshold,
            query: &mut self.query,
            terms_matching_strategy: &mut self.terms_matching_strategy,
            scoring_strategy: &mut self.scoring_strategy,
            locales: &mut self.locales,
            filter: &mut self.filter,
        }
    }
}
pub struct SemanticSearch {
    offset: usize,
    limit: usize,
    time_budget: TimeBudget,
    ranking_score_threshold: Option<f64>,
    semantic: Vec<u8>,
}
pub struct SemanticSearchRef<'original> {
    offset: &'original usize,
    limit: &'original usize,
    time_budget: &'original TimeBudget,
    ranking_score_threshold: &'original Option<f64>,
    semantic: &'original Vec<u8>,
}
pub struct SemanticSearchMut<'original> {
    offset: &'original mut usize,
    limit: &'original mut usize,
    time_budget: &'original mut TimeBudget,
    ranking_score_threshold: &'original mut Option<f64>,
    semantic: &'original mut Vec<u8>,
}
impl<'original> SemanticSearch {
    pub fn as_ref(&'original self) -> SemanticSearchRef<'original> {
        SemanticSearchRef {
            offset: &self.offset,
            limit: &self.limit,
            time_budget: &self.time_budget,
            ranking_score_threshold: &self.ranking_score_threshold,
            semantic: &self.semantic,
        }
    }
    pub fn as_mut(&'original mut self) -> SemanticSearchMut<'original> {
        SemanticSearchMut {
            offset: &mut self.offset,
            limit: &mut self.limit,
            time_budget: &mut self.time_budget,
            ranking_score_threshold: &mut self.ranking_score_threshold,
            semantic: &mut self.semantic,
        }
    }
}
pub struct HybridSearch {
    offset: usize,
    limit: usize,
    time_budget: TimeBudget,
    ranking_score_threshold: Option<f64>,
    query: String,
    terms_matching_strategy: TermsMatchingStrategy,
    scoring_strategy: ScoringStrategy,
    locales: Option<Vec<Language>>,
    semantic: Vec<u8>,
    semantic_ratio: f32,
}
pub struct HybridSearchRef<'original> {
    offset: &'original usize,
    limit: &'original usize,
    time_budget: &'original TimeBudget,
    ranking_score_threshold: &'original Option<f64>,
    query: &'original String,
    terms_matching_strategy: &'original TermsMatchingStrategy,
    scoring_strategy: &'original ScoringStrategy,
    locales: &'original Option<Vec<Language>>,
    semantic: &'original Vec<u8>,
    semantic_ratio: &'original f32,
}
pub struct HybridSearchMut<'original> {
    offset: &'original mut usize,
    limit: &'original mut usize,
    time_budget: &'original mut TimeBudget,
    ranking_score_threshold: &'original mut Option<f64>,
    query: &'original mut String,
    terms_matching_strategy: &'original mut TermsMatchingStrategy,
    scoring_strategy: &'original mut ScoringStrategy,
    locales: &'original mut Option<Vec<Language>>,
    semantic: &'original mut Vec<u8>,
    semantic_ratio: &'original mut f32,
}
impl<'original> HybridSearch {
    pub fn as_ref(&'original self) -> HybridSearchRef<'original> {
        HybridSearchRef {
            offset: &self.offset,
            limit: &self.limit,
            time_budget: &self.time_budget,
            ranking_score_threshold: &self.ranking_score_threshold,
            query: &self.query,
            terms_matching_strategy: &self.terms_matching_strategy,
            scoring_strategy: &self.scoring_strategy,
            locales: &self.locales,
            semantic: &self.semantic,
            semantic_ratio: &self.semantic_ratio,
        }
    }
    pub fn as_mut(&'original mut self) -> HybridSearchMut<'original> {
        HybridSearchMut {
            offset: &mut self.offset,
            limit: &mut self.limit,
            time_budget: &mut self.time_budget,
            ranking_score_threshold: &mut self.ranking_score_threshold,
            query: &mut self.query,
            terms_matching_strategy: &mut self.terms_matching_strategy,
            scoring_strategy: &mut self.scoring_strategy,
            locales: &mut self.locales,
            semantic: &mut self.semantic,
            semantic_ratio: &mut self.semantic_ratio,
        }
    }
}
pub enum SearchVariant<'a> {
    KeywordSearch(KeywordSearch<'a>),
    SemanticSearch(SemanticSearch),
    HybridSearch(HybridSearch),
}
impl<'a> SearchVariant<'a> {
    pub fn ranking_score_threshold(&self) -> Option<&f64> {
        match self {
            SearchVariant::KeywordSearch(view) => view.ranking_score_threshold.as_ref(),
            SearchVariant::SemanticSearch(view) => view.ranking_score_threshold.as_ref(),
            SearchVariant::HybridSearch(view) => view.ranking_score_threshold.as_ref(),
            _ => None,
        }
    }
    pub fn locales(&self) -> Option<&Vec<Language>> {
        match self {
            SearchVariant::KeywordSearch(view) => view.locales.as_ref(),
            SearchVariant::HybridSearch(view) => view.locales.as_ref(),
            _ => None,
        }
    }
    pub fn time_budget(&self) -> &TimeBudget {
        match self {
            SearchVariant::KeywordSearch(view) => &view.time_budget,
            SearchVariant::SemanticSearch(view) => &view.time_budget,
            SearchVariant::HybridSearch(view) => &view.time_budget,
        }
    }
    pub fn filter(&self) -> Option<&Filter<'a>> {
        match self {
            SearchVariant::KeywordSearch(view) => Some(&view.filter),
            _ => None,
        }
    }
    pub fn semantic_ratio(&self) -> Option<&f32> {
        match self {
            SearchVariant::HybridSearch(view) => Some(&view.semantic_ratio),
            _ => None,
        }
    }
    pub fn limit(&self) -> &usize {
        match self {
            SearchVariant::KeywordSearch(view) => &view.limit,
            SearchVariant::SemanticSearch(view) => &view.limit,
            SearchVariant::HybridSearch(view) => &view.limit,
        }
    }
    pub fn query(&self) -> Option<&String> {
        match self {
            SearchVariant::KeywordSearch(view) => Some(&view.query),
            SearchVariant::HybridSearch(view) => Some(&view.query),
            _ => None,
        }
    }
    pub fn terms_matching_strategy(&self) -> Option<&TermsMatchingStrategy> {
        match self {
            SearchVariant::KeywordSearch(view) => Some(&view.terms_matching_strategy),
            SearchVariant::HybridSearch(view) => Some(&view.terms_matching_strategy),
            _ => None,
        }
    }
    pub fn scoring_strategy(&self) -> Option<&ScoringStrategy> {
        match self {
            SearchVariant::KeywordSearch(view) => Some(&view.scoring_strategy),
            SearchVariant::HybridSearch(view) => Some(&view.scoring_strategy),
            _ => None,
        }
    }
    pub fn semantic(&self) -> Option<&Vec<u8>> {
        match self {
            SearchVariant::SemanticSearch(view) => Some(&view.semantic),
            SearchVariant::HybridSearch(view) => Some(&view.semantic),
            _ => None,
        }
    }
    pub fn offset(&self) -> &usize {
        match self {
            SearchVariant::KeywordSearch(view) => &view.offset,
            SearchVariant::SemanticSearch(view) => &view.offset,
            SearchVariant::HybridSearch(view) => &view.offset,
        }
    }
}
impl<'original, 'a> Search<'a> {
    pub fn into_keyword_search(self) -> Option<KeywordSearch<'a>> {
        Some(KeywordSearch {
            offset: self.offset,
            limit: self.limit,
            time_budget: self.time_budget,
            ranking_score_threshold: self.ranking_score_threshold,
            query: if let Some(query) = self.query {
                query
            } else {
                return None;
            },
            terms_matching_strategy: self.terms_matching_strategy,
            scoring_strategy: self.scoring_strategy,
            locales: self.locales,
            filter: if let Some(filter) = self.filter {
                filter
            } else {
                return None;
            },
        })
    }
    pub fn as_keyword_search(&'original self) -> Option<KeywordSearchRef<'original, 'a>> {
        Some(KeywordSearchRef {
            offset: &self.offset,
            limit: &self.limit,
            time_budget: &self.time_budget,
            ranking_score_threshold: &self.ranking_score_threshold,
            query: if let Some(query) = &self.query {
                query
            } else {
                return None;
            },
            terms_matching_strategy: &self.terms_matching_strategy,
            scoring_strategy: &self.scoring_strategy,
            locales: &self.locales,
            filter: if let Some(filter) = &self.filter {
                filter
            } else {
                return None;
            },
        })
    }
    pub fn as_keyword_search_mut(&'original mut self) -> Option<KeywordSearchMut<'original, 'a>> {
        Some(KeywordSearchMut {
            offset: {
                let offset = &mut self.offset;
                offset
            },
            limit: {
                let limit = &mut self.limit;
                limit
            },
            time_budget: {
                let time_budget = &mut self.time_budget;
                time_budget
            },
            ranking_score_threshold: {
                let ranking_score_threshold = &mut self.ranking_score_threshold;
                ranking_score_threshold
            },
            query: if let Some(query) = &mut self.query {
                query
            } else {
                return None;
            },
            terms_matching_strategy: {
                let terms_matching_strategy = &mut self.terms_matching_strategy;
                terms_matching_strategy
            },
            scoring_strategy: {
                let scoring_strategy = &mut self.scoring_strategy;
                scoring_strategy
            },
            locales: {
                let locales = &mut self.locales;
                locales
            },
            filter: if let Some(filter) = &mut self.filter {
                filter
            } else {
                return None;
            },
        })
    }
    pub fn into_semantic_search(self) -> Option<SemanticSearch> {
        Some(SemanticSearch {
            offset: self.offset,
            limit: self.limit,
            time_budget: self.time_budget,
            ranking_score_threshold: self.ranking_score_threshold,
            semantic: if let Some(semantic) = self.semantic {
                semantic
            } else {
                return None;
            },
        })
    }
    pub fn as_semantic_search(&'original self) -> Option<SemanticSearchRef<'original>> {
        Some(SemanticSearchRef {
            offset: &self.offset,
            limit: &self.limit,
            time_budget: &self.time_budget,
            ranking_score_threshold: &self.ranking_score_threshold,
            semantic: if let Some(semantic) = &self.semantic {
                semantic
            } else {
                return None;
            },
        })
    }
    pub fn as_semantic_search_mut(&'original mut self) -> Option<SemanticSearchMut<'original>> {
        Some(SemanticSearchMut {
            offset: {
                let offset = &mut self.offset;
                offset
            },
            limit: {
                let limit = &mut self.limit;
                limit
            },
            time_budget: {
                let time_budget = &mut self.time_budget;
                time_budget
            },
            ranking_score_threshold: {
                let ranking_score_threshold = &mut self.ranking_score_threshold;
                ranking_score_threshold
            },
            semantic: if let Some(semantic) = &mut self.semantic {
                semantic
            } else {
                return None;
            },
        })
    }
    pub fn into_hybrid_search(self) -> Option<HybridSearch> {
        Some(HybridSearch {
            offset: self.offset,
            limit: self.limit,
            time_budget: self.time_budget,
            ranking_score_threshold: self.ranking_score_threshold,
            query: if let Some(query) = self.query {
                query
            } else {
                return None;
            },
            terms_matching_strategy: self.terms_matching_strategy,
            scoring_strategy: self.scoring_strategy,
            locales: self.locales,
            semantic: if let Some(semantic) = self.semantic {
                semantic
            } else {
                return None;
            },
            semantic_ratio: if let Some(semantic_ratio) = self.semantic_ratio {
                semantic_ratio
            } else {
                return None;
            },
        })
    }
    pub fn as_hybrid_search(&'original self) -> Option<HybridSearchRef<'original>> {
        Some(HybridSearchRef {
            offset: &self.offset,
            limit: &self.limit,
            time_budget: &self.time_budget,
            ranking_score_threshold: &self.ranking_score_threshold,
            query: if let Some(query) = &self.query {
                query
            } else {
                return None;
            },
            terms_matching_strategy: &self.terms_matching_strategy,
            scoring_strategy: &self.scoring_strategy,
            locales: &self.locales,
            semantic: if let Some(semantic) = &self.semantic {
                semantic
            } else {
                return None;
            },
            semantic_ratio: if let Some(semantic_ratio) = &self.semantic_ratio {
                semantic_ratio
            } else {
                return None;
            },
        })
    }
    pub fn as_hybrid_search_mut(&'original mut self) -> Option<HybridSearchMut<'original>> {
        Some(HybridSearchMut {
            offset: {
                let offset = &mut self.offset;
                offset
            },
            limit: {
                let limit = &mut self.limit;
                limit
            },
            time_budget: {
                let time_budget = &mut self.time_budget;
                time_budget
            },
            ranking_score_threshold: {
                let ranking_score_threshold = &mut self.ranking_score_threshold;
                ranking_score_threshold
            },
            query: if let Some(query) = &mut self.query {
                query
            } else {
                return None;
            },
            terms_matching_strategy: {
                let terms_matching_strategy = &mut self.terms_matching_strategy;
                terms_matching_strategy
            },
            scoring_strategy: {
                let scoring_strategy = &mut self.scoring_strategy;
                scoring_strategy
            },
            locales: {
                let locales = &mut self.locales;
                locales
            },
            semantic: if let Some(semantic) = &mut self.semantic {
                semantic
            } else {
                return None;
            },
            semantic_ratio: if let Some(semantic_ratio) = &mut self.semantic_ratio {
                semantic_ratio
            } else {
                return None;
            },
        })
    }
}

Included in the generated code is

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
pub struct KeywordSearch<'a> {
    offset: usize,
    limit: usize,
    time_budget: TimeBudget,
    ranking_score_threshold: Option<f64>,
    query: String,
    terms_matching_strategy: TermsMatchingStrategy,
    scoring_strategy: ScoringStrategy,
    locales: Option<Vec<Language>>,
    filter: Filter<'a>,
}

pub struct SemanticSearch {
    offset: usize,
    limit: usize,
    time_budget: TimeBudget,
    ranking_score_threshold: Option<f64>,
    semantic: Vec<u8>,
}

pub struct HybridSearch {
    query: String,
    offset: usize,
    limit: usize,
    time_budget: TimeBudget,
    ranking_score_threshold: Option<f64>,
    terms_matching_strategy: TermsMatchingStrategy,
    scoring_strategy: ScoringStrategy,
    locales: Option<Vec<Language>>,
    semantic: Vec<u8>,
    semantic_ratio: f32,
}

with *Ref and *Mut versions of each, as well as the methods to convert into these types (see full expansion drop down above for inclusion). This effectively allows implementing a “monolith with kind approach” (Approach 5) with minimum boilerplate.

In addition to the previously mentioned generated code, code for “enum with complete structs” (Approach 4) is also generated.

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
pub enum SearchVariant<'a> {
    KeywordSearch(KeywordSearch<'a>),
    SemanticSearch(SemanticSearch),
    HybridSearch(HybridSearch),
}

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 ranking_score_threshold(&self) -> Option<&f64> {
        match self {
            SearchVariant::KeywordSearch(view) => view.ranking_score_threshold.as_ref(),
            SearchVariant::SemanticSearch(view) => view.ranking_score_threshold.as_ref(),
            SearchVariant::HybridSearch(view) => view.ranking_score_threshold.as_ref(),
            _ => None,
        }
    }
    pub fn limit(&self) -> &usize {
        match self {
            SearchVariant::KeywordSearch(view) => &view.limit,
            SearchVariant::SemanticSearch(view) => &view.limit,
            SearchVariant::HybridSearch(view) => &view.limit,
        }
    }
    pub fn semantic_ratio(&self) -> Option<&f32> {
        match self {
            SearchVariant::HybridSearch(view) => Some(&view.semantic_ratio),
            _ => None,
        }
    }
    pub fn filter(&self) -> Option<&Filter<'a>> {
        match self {
            SearchVariant::KeywordSearch(view) => Some(&view.filter),
            _ => None,
        }
    }
    pub fn locales(&self) -> Option<&Vec<Language>> {
        match self {
            SearchVariant::KeywordSearch(view) => view.locales.as_ref(),
            SearchVariant::HybridSearch(view) => view.locales.as_ref(),
            _ => None,
        }
    }
    pub fn semantic(&self) -> Option<&Vec<u8>> {
        match self {
            SearchVariant::SemanticSearch(view) => Some(&view.semantic),
            SearchVariant::HybridSearch(view) => Some(&view.semantic),
            _ => None,
        }
    }
    pub fn query(&self) -> Option<&String> {
        match self {
            SearchVariant::KeywordSearch(view) => Some(&view.query),
            SearchVariant::HybridSearch(view) => Some(&view.query),
            _ => None,
        }
    }
    pub fn time_budget(&self) -> &TimeBudget {
        match self {
            SearchVariant::KeywordSearch(view) => &view.time_budget,
            SearchVariant::SemanticSearch(view) => &view.time_budget,
            SearchVariant::HybridSearch(view) => &view.time_budget,
        }
    }
    pub fn terms_matching_strategy(&self) -> Option<&TermsMatchingStrategy> {
        match self {
            SearchVariant::KeywordSearch(view) => Some(&view.terms_matching_strategy),
            SearchVariant::HybridSearch(view) => Some(&view.terms_matching_strategy),
            _ => None,
        }
    }
    pub fn scoring_strategy(&self) -> Option<&ScoringStrategy> {
        match self {
            SearchVariant::KeywordSearch(view) => Some(&view.scoring_strategy),
            SearchVariant::HybridSearch(view) => Some(&view.scoring_strategy),
            _ => None,
        }
    }
}

Real World Examples

For the error_set crate, internal error representations were implemented using Approach 4 by hand. By using view-types, almost all the boilerplate can be removed PR.

As an example, for the same crate, the original hand written approach was replaced with Approach 5 and the view-types crate. However, since it was originally implemented with Approach 4, a few more code changes around the codebase were needed to implement Approach 5 - PR.

Conclusion

The view-types macro solves the data modeling challenges outlined in the previous article by generating boilerplate for both Approach 4 (enum with complete structs) and Approach 5 (monolithic with kind). This provides flexibility to choose your preferred API style without sacrificing type safety or maintainability.

The macro delivers zero code duplication, compile-time type safety, API flexibility, and easy maintenance when adding new fields or views. It excels for complex data structures with overlapping field requirements that may evolve over time. view-types transforms challenging data modeling into straightforward declarations of intent.

The view-types crate is available on crates.io and the source code can be found on GitHub.