icydb-model-macros 0.216.1

Procedural macros for IcyDB application models
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
//! Module: trait_kind
//! Responsibility: generated application-trait classification.
//! Does not own: runtime schema semantics.
//! Boundary: macro input to generated tokens.

use crate::prelude::*;
use darling::{Error as DarlingError, FromMeta, ast::NestedMeta};
use derive_more::IntoIterator;
use std::{collections::HashSet, hash::Hash, str::FromStr};

//
// TraitKind
//

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum TraitKind {
    // inherent impl
    Inherent,

    // rust + third party
    CandidType,
    Clone,
    Copy,
    Debug,
    Default,
    Deserialize,
    Deref,
    DerefMut,
    Display,
    Eq,
    Hash,
    Ord,
    PartialEq,
    PartialOrd,

    // math
    Add,
    AddAssign,
    Div,
    DivAssign,
    Mul,
    MulAssign,
    Rem,
    Sub,
    SubAssign,
    Sum,

    // application model
    Collection,
    From,
    Inner,
    MapCollection,
    NumericValue,
    Path,
    Sorted,
    NormalizeAuto,
    NormalizeCustom,
    ValidateAuto,
    ValidateCustom,
    Visitable,
}

impl FromStr for TraitKind {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "Inherent" => Ok(Self::Inherent),
            "CandidType" => Ok(Self::CandidType),
            "Clone" => Ok(Self::Clone),
            "Copy" => Ok(Self::Copy),
            "Debug" => Ok(Self::Debug),
            "Default" => Ok(Self::Default),
            "Deserialize" => Ok(Self::Deserialize),
            "Deref" => Ok(Self::Deref),
            "DerefMut" => Ok(Self::DerefMut),
            "Display" => Ok(Self::Display),
            "Eq" => Ok(Self::Eq),
            "Hash" => Ok(Self::Hash),
            "Ord" => Ok(Self::Ord),
            "PartialEq" => Ok(Self::PartialEq),
            "PartialOrd" => Ok(Self::PartialOrd),
            "Add" => Ok(Self::Add),
            "AddAssign" => Ok(Self::AddAssign),
            "Div" => Ok(Self::Div),
            "DivAssign" => Ok(Self::DivAssign),
            "Mul" => Ok(Self::Mul),
            "MulAssign" => Ok(Self::MulAssign),
            "Rem" => Ok(Self::Rem),
            "Sub" => Ok(Self::Sub),
            "SubAssign" => Ok(Self::SubAssign),
            "Sum" => Ok(Self::Sum),
            "Collection" => Ok(Self::Collection),
            "From" => Ok(Self::From),
            "Inner" => Ok(Self::Inner),
            "MapCollection" => Ok(Self::MapCollection),
            "NumericValue" => Ok(Self::NumericValue),
            "Path" => Ok(Self::Path),
            "Sorted" => Ok(Self::Sorted),
            "NormalizeAuto" => Ok(Self::NormalizeAuto),
            "NormalizeCustom" => Ok(Self::NormalizeCustom),
            "ValidateAuto" => Ok(Self::ValidateAuto),
            "ValidateCustom" => Ok(Self::ValidateCustom),
            "Visitable" => Ok(Self::Visitable),
            _ => Err("unknown TraitKind"),
        }
    }
}

const DEFAULT_TRAITS: &[TraitKind] = &[TraitKind::Clone, TraitKind::Debug, TraitKind::Path];

const DEFAULT_CONFIGURABLE_TYPE_TRAITS: &[TraitKind] = &[
    TraitKind::From,
    TraitKind::NormalizeCustom,
    TraitKind::ValidateCustom,
];

const REQUIRED_TYPE_TRAITS: &[TraitKind] = &[
    TraitKind::CandidType,
    TraitKind::Clone,
    TraitKind::Debug,
    TraitKind::Deserialize,
    TraitKind::Eq,
    TraitKind::PartialEq,
    TraitKind::Path,
    TraitKind::NormalizeAuto,
    TraitKind::ValidateAuto,
    TraitKind::Visitable,
];

// path_to_string
#[must_use]
fn path_to_string(path: &syn::Path) -> String {
    path.to_token_stream()
        .to_string()
        .replace(' ', "")
        .trim_matches(':')
        .to_string()
}

impl TraitKind {
    /// NOTE: even if we have our own impl versions, the derives may still
    /// be used by other types (PartialEq for instance)
    #[must_use]
    #[remain::check]
    pub(crate) fn derive_path(self) -> Option<TokenStream> {
        #[remain::sorted]
        match self {
            Self::Add => Some(quote!(::icydb_model::__reexports::icydb_model_macros::Add)),
            Self::AddAssign => Some(quote!(
                ::icydb_model::__reexports::icydb_model_macros::AddAssign
            )),
            Self::CandidType => Some(quote!(::icydb_model::__reexports::candid::CandidType)),
            Self::Clone => Some(quote!(Clone)),
            Self::Copy => Some(quote!(Copy)),
            Self::Debug => Some(quote!(Debug)),
            Self::Default => Some(quote!(Default)),
            Self::Deref => Some(quote!(
                ::icydb_model::__reexports::icydb_model_macros::Deref
            )),
            Self::DerefMut => Some(quote!(
                ::icydb_model::__reexports::icydb_model_macros::DerefMut
            )),
            Self::Deserialize => Some(quote!(::icydb_model::__reexports::serde::Deserialize)),
            Self::Display => Some(quote!(
                ::icydb_model::__reexports::icydb_model_macros::Display
            )),
            Self::Div => Some(quote!(::icydb_model::__reexports::icydb_model_macros::Div)),
            Self::DivAssign => Some(quote!(
                ::icydb_model::__reexports::icydb_model_macros::DivAssign
            )),
            Self::Eq => Some(quote!(Eq)),
            Self::Hash => Some(quote!(Hash)),
            Self::Inner => Some(quote!(
                ::icydb_model::__reexports::icydb_model_macros::Inner
            )),
            Self::Mul => Some(quote!(::icydb_model::__reexports::icydb_model_macros::Mul)),
            Self::MulAssign => Some(quote!(
                ::icydb_model::__reexports::icydb_model_macros::MulAssign
            )),
            Self::Ord => Some(quote!(Ord)),
            Self::PartialEq => Some(quote!(PartialEq)),
            Self::PartialOrd => Some(quote!(PartialOrd)),
            Self::Rem => Some(quote!(::icydb_model::__reexports::icydb_model_macros::Rem)),
            Self::Sub => Some(quote!(::icydb_model::__reexports::icydb_model_macros::Sub)),
            Self::SubAssign => Some(quote!(
                ::icydb_model::__reexports::icydb_model_macros::SubAssign
            )),
            Self::Sum => Some(quote!(::icydb_model::__reexports::icydb_model_macros::Sum)),

            _ => {
                // NOTE: Not all TraitKind variants have derive paths.
                None
            }
        }
    }
}

impl FromMeta for TraitKind {
    fn from_nested_meta(item: &NestedMeta) -> Result<Self, DarlingError> {
        match item {
            NestedMeta::Meta(syn::Meta::Path(path)) => {
                let path_str = path_to_string(path);

                Self::from_str(&path_str).map_err(DarlingError::custom)
            }

            _ => Err(DarlingError::custom(format!(
                "expected Meta Path, got {item:?}"
            ))),
        }
    }
}

impl ToTokens for TraitKind {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        match self {
            Self::Clone => quote!(::core::clone::Clone).to_tokens(tokens),
            Self::Copy => quote!(::core::marker::Copy).to_tokens(tokens),
            Self::Debug => quote!(::core::fmt::Debug).to_tokens(tokens),
            Self::Default => quote!(::core::default::Default).to_tokens(tokens),
            Self::Eq => quote!(::core::cmp::Eq).to_tokens(tokens),
            Self::Ord => quote!(::core::cmp::Ord).to_tokens(tokens),
            Self::PartialEq => quote!(::core::cmp::PartialEq).to_tokens(tokens),
            Self::PartialOrd => quote!(::core::cmp::PartialOrd).to_tokens(tokens),
            Self::Add
            | Self::AddAssign
            | Self::Deref
            | Self::DerefMut
            | Self::Div
            | Self::DivAssign
            | Self::Mul
            | Self::MulAssign
            | Self::Rem
            | Self::Sub
            | Self::SubAssign => {
                let trait_name = format_ident!("{self:?}");
                quote!(::std::ops::#trait_name).to_tokens(tokens);
            }
            Self::Display => quote!(::std::fmt::Display).to_tokens(tokens),
            Self::From => quote!(::std::convert::From).to_tokens(tokens),
            Self::Hash => quote!(::std::hash::Hash).to_tokens(tokens),
            Self::Collection | Self::Inner | Self::MapCollection | Self::Path => {
                let trait_name = format_ident!("{self:?}");
                quote!(::icydb_model::#trait_name).to_tokens(tokens);
            }
            Self::NumericValue => {
                quote!(::icydb_model::schema::NumericValue).to_tokens(tokens);
            }
            Self::Sum => quote!(::std::iter::Sum).to_tokens(tokens),
            Self::CandidType => {
                quote!(::icydb_model::__reexports::candid::CandidType).to_tokens(tokens);
            }
            Self::Deserialize => {
                quote!(::icydb_model::__reexports::serde::Deserialize).to_tokens(tokens);
            }
            Self::NormalizeAuto
            | Self::NormalizeCustom
            | Self::ValidateAuto
            | Self::ValidateCustom
            | Self::Visitable => {
                let trait_name = format_ident!("{self:?}");
                quote!(::icydb_model::visitor::#trait_name).to_tokens(tokens);
            }
            Self::Inherent | Self::Sorted => {}
        }
    }
}

//
// TraitSet
//

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct TraitSet(pub HashSet<TraitKind>);

impl TraitSet {
    pub(crate) fn add(&mut self, tr: TraitKind) {
        self.insert(tr);
    }

    pub(crate) fn insert(&mut self, tr: TraitKind) -> bool {
        self.0.insert(tr)
    }

    pub(crate) fn remove(&mut self, tr: TraitKind) -> bool {
        self.0.remove(&tr)
    }

    pub(crate) fn extend<I: IntoIterator<Item = TraitKind>>(&mut self, traits: I) {
        self.0.extend(traits);
    }

    pub(crate) fn into_vec(self) -> Vec<TraitKind> {
        self.0.into_iter().collect()
    }
}

impl From<Vec<TraitKind>> for TraitSet {
    fn from(v: Vec<TraitKind>) -> Self {
        Self(v.into_iter().collect())
    }
}

impl FromIterator<TraitKind> for TraitSet {
    fn from_iter<I: IntoIterator<Item = TraitKind>>(iter: I) -> Self {
        Self(iter.into_iter().collect())
    }
}

impl ToTokens for TraitSet {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        if !self.0.is_empty() {
            let derive_paths = self.0.iter().filter_map(|tr| tr.derive_path());

            tokens.extend(quote! {
                #[derive(#(#derive_paths),*)]
            });
        }
    }
}

//
// TraitBuilder
//
// Collects trait additions/removals from schema attributes.
// After parsing, it should be treated as immutable and resolved via `.build()`.
//

#[derive(Clone, Debug, Default, FromMeta)]
pub struct TraitBuilder {
    #[darling(default)]
    pub(crate) add: TraitListMeta,

    #[darling(default)]
    pub(crate) remove: TraitListMeta,
}

impl TraitBuilder {
    pub(crate) fn explicitly_adds(&self, tr: TraitKind) -> bool {
        self.add.iter().any(|candidate| *candidate == tr)
    }

    /// Validate author directives for a generated application value type.
    ///
    /// Required traits are compiler-owned output rather than configurable
    /// defaults. Optional and override traits continue through the maintained
    /// generic add/remove validation.
    pub(crate) fn validate_for_type(&self) -> Result<(), DarlingError> {
        for tr in self.add.iter() {
            if REQUIRED_TYPE_TRAITS.contains(tr) {
                return Err(DarlingError::custom(format!(
                    "required trait '{tr:?}' is generated automatically and cannot be added explicitly"
                )));
            }
        }

        for tr in self.remove.iter() {
            if REQUIRED_TYPE_TRAITS.contains(tr) {
                return Err(DarlingError::custom(format!(
                    "required trait '{tr:?}' is generated automatically and cannot be removed"
                )));
            }
        }

        self.resolve(type_trait_set()).map(|_| ())
    }

    fn resolve(&self, mut set: TraitSet) -> Result<TraitSet, DarlingError> {
        for tr in self.add.iter() {
            if !set.insert(*tr) {
                return Err(DarlingError::custom(format!(
                    "adding duplicate trait '{tr:?}'"
                )));
            }
        }

        for tr in self.remove.iter() {
            if !set.remove(*tr) {
                return Err(DarlingError::custom(format!(
                    "cannot remove trait '{tr:?}' because it is not enabled"
                )));
            }
        }

        Ok(set)
    }

    /// Resolve the required and default traits for an application value type.
    ///
    /// This is called only after [`Self::validate_for_type`] succeeds.
    pub(crate) fn build_for_type(&self) -> TraitSet {
        self.resolve(type_trait_set())
            .unwrap_or_else(|_| panic!("validated application type traits must resolve"))
    }

    // Generates the TraitList based on the defaults plus traits that have been
    // added or removed. This is called only after validation succeeds.
    pub(crate) fn build(&self) -> TraitSet {
        self.resolve(default_trait_set())
            .unwrap_or_else(|_| panic!("validated generated traits must resolve"))
    }
}

fn default_trait_set() -> TraitSet {
    DEFAULT_TRAITS.iter().copied().collect()
}

fn type_trait_set() -> TraitSet {
    REQUIRED_TYPE_TRAITS
        .iter()
        .chain(DEFAULT_CONFIGURABLE_TYPE_TRAITS.iter())
        .copied()
        .collect()
}

//
// TraitListMeta
// Used only for parsing trait lists from schema attributes via darling.
//

#[derive(Clone, Debug, Default, IntoIterator)]
pub struct TraitListMeta(pub Vec<TraitKind>);

impl TraitListMeta {
    pub(crate) fn push(&mut self, tr: TraitKind) {
        self.0.push(tr);
    }

    pub(crate) fn iter(&self) -> std::slice::Iter<'_, TraitKind> {
        self.0.iter()
    }
}

impl FromMeta for TraitListMeta {
    fn from_list(items: &[NestedMeta]) -> Result<Self, DarlingError> {
        let mut traits = Self::default();

        for item in items {
            let tr = TraitKind::from_nested_meta(item)?;
            traits.push(tr);
        }

        Ok(traits)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn required_type_traits_are_emitted_without_author_directives() {
        let traits = TraitBuilder::default().build_for_type();

        for required in REQUIRED_TYPE_TRAITS {
            assert!(traits.0.contains(required));
        }
    }

    #[test]
    fn required_type_traits_reject_explicit_addition_and_removal() {
        for required in REQUIRED_TYPE_TRAITS.iter().copied() {
            let adds_required = TraitBuilder {
                add: TraitListMeta(vec![required]),
                remove: TraitListMeta::default(),
            };
            assert!(adds_required.validate_for_type().is_err());

            let removes_required = TraitBuilder {
                add: TraitListMeta::default(),
                remove: TraitListMeta(vec![required]),
            };
            assert!(removes_required.validate_for_type().is_err());
        }
    }

    #[test]
    fn optional_and_override_type_traits_remain_configurable() {
        for configurable in [
            TraitKind::From,
            TraitKind::NormalizeCustom,
            TraitKind::ValidateCustom,
        ] {
            let builder = TraitBuilder {
                add: TraitListMeta::default(),
                remove: TraitListMeta(vec![configurable]),
            };
            assert!(builder.validate_for_type().is_ok());
            assert!(!builder.build_for_type().0.contains(&configurable));
        }

        let default = TraitBuilder {
            add: TraitListMeta(vec![TraitKind::Default]),
            remove: TraitListMeta::default(),
        };
        assert!(default.validate_for_type().is_ok());
        assert!(default.build_for_type().0.contains(&TraitKind::Default));
    }
}