ruma-macros 0.18.0

Procedural macros used by the Ruma crates.
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
//! Parsing helpers for the `EventContent` derive macro.

use as_variant::as_variant;
use proc_macro2::Span;
use syn::{
    meta::ParseNestedMeta,
    parse::{Parse, ParseStream},
    parse_quote,
};

use super::{EventContent, EventContentField, EventContentKind};
use crate::{
    events::common::{CommonEventKind, EventType, EventTypes},
    util::{ParseNestedMetaExt, RumaEvents, SerdeMetaItem, StructFieldExt},
};

impl EventContent {
    pub(super) fn parse(input: syn::DeriveInput) -> syn::Result<Self> {
        let ruma_events = RumaEvents::new();

        let mut event_content_attrs = EventContentAttrs::default();

        for attr in &input.attrs {
            if !attr.path().is_ident("ruma_event") {
                continue;
            }

            attr.parse_nested_meta(|meta| event_content_attrs.try_merge(meta, attr))?;
        }

        let EventContentAttrs {
            event_type,
            aliases,
            kind,
            state_key_type,
            unsigned_type,
            has_custom_redacted,
            has_custom_possibly_redacted,
            has_without_relation,
        } = event_content_attrs;

        let event_type = event_type.ok_or_else(|| {
            syn::Error::new(
                Span::call_site(),
                "missing `type` attribute, \
                 add `#[ruma_event(type = \"m.event_type\", kind = Kind)]` \
                 below the event content derive",
            )
        })?;
        let types = EventTypes::try_from_parts(event_type, aliases)?;

        let fields =
            as_variant!(input.data, syn::Data::Struct(syn::DataStruct { fields, ..}) => fields)
                .map(|fields| {
                    fields.into_iter().map(|field| field.try_into()).collect::<syn::Result<_>>()
                })
                .transpose()?;

        let kind = EventContentKind::try_from_parts(
            kind,
            state_key_type,
            unsigned_type,
            has_custom_redacted,
            has_custom_possibly_redacted,
            &ruma_events,
        )?;

        let event_content = Self {
            types,
            ident: input.ident,
            vis: input.vis,
            fields,
            kind,
            has_without_relation,
            ruma_events,
        };

        event_content.validate()?;

        Ok(event_content)
    }
}

impl EventContent {
    /// Validate the data inside this
    fn validate(&self) -> syn::Result<()> {
        // Ident check.
        if !self.ident.to_string().ends_with("Content") {
            return Err(syn::Error::new_spanned(
                &self.ident,
                "event content struct name must end with `Content`",
            ));
        }

        // Type suffix checks.
        let has_type_suffix = self.types.is_prefix();

        if has_type_suffix && !self.kind.is_account_data() {
            return Err(syn::Error::new_spanned(
                &self.types.ev_type,
                "only account data events may contain a `.*` suffix",
            ));
        }

        if let Some(fields) = &self.fields {
            let type_fragment_fields_count =
                fields.iter().filter(|field| field.is_type_fragment).count();

            if has_type_suffix && type_fragment_fields_count == 0 {
                return Err(syn::Error::new_spanned(
                    &self.types.ev_type,
                    "event type with a `.*` suffix requires there to be a \
                     `#[ruma_event(type_fragment)]` field",
                ));
            }

            if !has_type_suffix && type_fragment_fields_count > 0 {
                return Err(syn::Error::new(
                    Span::call_site(),
                    "`#[ruma_event(type_fragment)]` field is only valid for an event type with a `.*` suffix",
                ));
            }

            if type_fragment_fields_count > 1 {
                return Err(syn::Error::new(
                    Span::call_site(),
                    "There can only be one `#[ruma_event(type_fragment)]` field",
                ));
            }
        } else if has_type_suffix {
            return Err(syn::Error::new_spanned(
                &self.types.ev_type,
                "event type with a `.*` suffix is required to be a struct",
            ));
        }

        // Checks for generated structs.
        if self.kind.should_generate_redacted() && self.fields.is_none() {
            return Err(syn::Error::new(
                Span::call_site(),
                "To generate a redacted event content, \
                 the event content type needs to be a struct. \
                 Disable this with the `custom_redacted` attribute",
            ));
        }

        if self.kind.should_generate_possibly_redacted() {
            if self
                .fields
                .as_ref()
                .is_none_or(|fields| fields.iter().any(|field| field.inner.ident.is_none()))
            {
                return Err(syn::Error::new(
                    Span::call_site(),
                    "To generate a possibly redacted event content, \
                     the event content type needs to be a struct with named fields. \
                     Disable this with the `custom_possibly_redacted` attribute",
                ));
            }

            if let Some(fields) = &self.fields
                && let Some(field_with_unsupported_serde_attribute) = fields.iter().find(|field| {
                    if field.keep_in_possibly_redacted() {
                        return false;
                    }

                    field.inner.serde_meta_items().any(|serde_meta| {
                        serde_meta != SerdeMetaItem::Rename && serde_meta != SerdeMetaItem::Alias
                    })
                })
            {
                return Err(syn::Error::new_spanned(
                    field_with_unsupported_serde_attribute,
                    "To generate a possibly redacted event content, \
                     the fields that are redacted must either use the `default` \
                     serde attribute with any other attribute, or only the \
                     following serde attributes: `rename` or `alias`. \
                     Disable this with the `custom_possibly_redacted` attribute",
                ));
            }
        }

        if self.has_without_relation
            && self.fields.as_ref().is_none_or(|fields| {
                !fields.iter().any(|field| {
                    field.inner.ident.as_ref().is_some_and(|ident| *ident == "relates_to")
                })
            })
        {
            return Err(syn::Error::new(
                Span::call_site(),
                "To generate an event content without relation, \
                 the event content type needs to be a struct with a field named `relates_to`. \
                 Disable this by removing the `without_relation` attribute",
            ));
        }

        Ok(())
    }
}

/// Parsed container attributes for the `EventContent` macro.
#[derive(Default)]
pub struct EventContentAttrs {
    /// The main event type.
    event_type: Option<EventType>,

    /// The alternative event types.
    aliases: Vec<EventType>,

    /// The event content kind.
    kind: Option<EventContentKindAttr>,

    /// The type of the state key.
    state_key_type: Option<syn::Type>,

    /// The type of the unsigned data.
    unsigned_type: Option<syn::Type>,

    /// Whether the `Redacted*EventContent` type is implemented manually rather than generated by
    /// this macro.
    has_custom_redacted: bool,

    /// Whether the `PossiblyRedacted*EventContent` type is implemented manually rather than
    /// generated by this macro.
    has_custom_possibly_redacted: bool,

    /// Whether this macro should generate an `*EventContentWithoutRelation` type.
    has_without_relation: bool,
}

impl EventContentAttrs {
    /// Set the main event type.
    ///
    /// Returns an error if it is already set.
    fn set_event_type(&mut self, event_type: EventType, attr: &syn::Attribute) -> syn::Result<()> {
        if self.event_type.is_some() {
            return Err(syn::Error::new_spanned(
                attr,
                "cannot have multiple values for `type` attribute",
            ));
        }

        self.event_type = Some(event_type);
        Ok(())
    }

    /// Add an alternative event type.
    fn push_alias(&mut self, event_type: EventType) {
        self.aliases.push(event_type);
    }

    /// Set the event content kind.
    ///
    /// Returns an error if it is already set.
    fn set_kind(&mut self, kind: EventContentKindAttr, attr: &syn::Attribute) -> syn::Result<()> {
        if self.kind.is_some() {
            return Err(syn::Error::new_spanned(
                attr,
                "cannot have multiple values for `kind` attribute",
            ));
        }

        self.kind = Some(kind);
        Ok(())
    }

    /// Set the type of the state key.
    ///
    /// Returns an error if it is already set.
    fn set_state_key_type(
        &mut self,
        state_key_type: syn::Type,
        attr: &syn::Attribute,
    ) -> syn::Result<()> {
        if self.state_key_type.is_some() {
            return Err(syn::Error::new_spanned(
                attr,
                "cannot have multiple values for `state_key_type` attribute",
            ));
        }

        self.state_key_type = Some(state_key_type);
        Ok(())
    }

    /// Set the type of the unsigned data.
    ///
    /// Returns an error if it is already set.
    fn set_unsigned_type(
        &mut self,
        unsigned_type: syn::Type,
        attr: &syn::Attribute,
    ) -> syn::Result<()> {
        if self.unsigned_type.is_some() {
            return Err(syn::Error::new_spanned(
                attr,
                "cannot have multiple values for `unsigned_type` attribute",
            ));
        }

        self.unsigned_type = Some(unsigned_type);
        Ok(())
    }

    /// Set that the `Redacted*EventContent` type is implemented manually rather than generated by
    /// this macro.
    ///
    /// Returns an error if it is already set.
    fn set_custom_redacted(&mut self, attr: &syn::Attribute) -> syn::Result<()> {
        if self.has_custom_redacted {
            return Err(syn::Error::new_spanned(
                attr,
                "cannot have multiple occurrences of `custom_redacted` attribute",
            ));
        }

        self.has_custom_redacted = true;
        Ok(())
    }

    /// Set that the `PossiblyRedacted*EventContent` type is implemented manually rather than
    /// generated by this macro.
    ///
    /// Returns an error if it is already set.
    fn set_custom_possibly_redacted(&mut self, attr: &syn::Attribute) -> syn::Result<()> {
        if self.has_custom_possibly_redacted {
            return Err(syn::Error::new_spanned(
                attr,
                "cannot have multiple occurrences of `custom_possibly_redacted` attribute",
            ));
        }

        self.has_custom_possibly_redacted = true;
        Ok(())
    }

    /// Set that this macro should generate an `*EventContentWithoutRelation` type.
    ///
    /// Returns an error if it is already set.
    fn set_without_relation(&mut self, attr: &syn::Attribute) -> syn::Result<()> {
        if self.has_without_relation {
            return Err(syn::Error::new_spanned(
                attr,
                "cannot have multiple occurrences of `without_relation` attribute",
            ));
        }

        self.has_without_relation = true;
        Ok(())
    }

    fn try_merge(&mut self, meta: ParseNestedMeta<'_>, attr: &syn::Attribute) -> syn::Result<()> {
        if meta.path.is_ident("type") {
            return self.set_event_type(meta.value()?.parse()?, attr);
        }

        if meta.path.is_ident("alias") {
            self.push_alias(meta.value()?.parse()?);
            return Ok(());
        }

        if meta.path.is_ident("kind") {
            return self.set_kind(meta.value()?.parse()?, attr);
        }

        if meta.path.is_ident("state_key_type") {
            return self.set_state_key_type(meta.value()?.parse()?, attr);
        }

        if meta.path.is_ident("unsigned_type") {
            return self.set_unsigned_type(meta.value()?.parse()?, attr);
        }

        if meta.path.is_ident("custom_redacted") {
            if meta.has_value() {
                return Err(meta.error("`custom_redacted` attribute doesn't expect a value"));
            }

            return self.set_custom_redacted(attr);
        }

        if meta.path.is_ident("custom_possibly_redacted") {
            if meta.has_value() {
                return Err(
                    meta.error("`custom_possibly_redacted` attribute doesn't expect a value")
                );
            }

            return self.set_custom_possibly_redacted(attr);
        }

        if meta.path.is_ident("without_relation") {
            if meta.has_value() {
                return Err(meta.error("`without_relation` attribute doesn't expect a value"));
            }

            return self.set_without_relation(attr);
        }

        Err(meta.error("unsupported `ruma_event` attribute"))
    }
}

impl TryFrom<syn::Field> for EventContentField {
    type Error = syn::Error;

    fn try_from(mut inner: syn::Field) -> Result<Self, Self::Error> {
        let mut field_attrs = EventContentFieldAttrs::default();

        for attr in inner.attrs.extract_if(.., |attr| attr.path().is_ident("ruma_event")) {
            attr.parse_nested_meta(|meta| field_attrs.try_merge(meta, &attr))?;
        }

        let EventContentFieldAttrs { skip_redaction, is_type_fragment } = field_attrs;

        if skip_redaction && is_type_fragment {
            return Err(syn::Error::new_spanned(
                inner,
                "the `skip_redaction` attribute is not valid on a field with the `type_fragment` attribute",
            ));
        }

        Ok(Self { inner, skip_redaction, is_type_fragment })
    }
}

/// Parsed field attributes for the `EventContent` macro.
#[derive(Default)]
struct EventContentFieldAttrs {
    /// Whether this field should be kept during redaction.
    skip_redaction: bool,

    /// Whether this field represents the suffix of the event type.
    is_type_fragment: bool,
}

impl EventContentFieldAttrs {
    /// Set that this field should be kept during redaction.
    ///
    /// Returns an error if it is already set.
    fn set_skip_redaction(&mut self, attr: &syn::Attribute) -> syn::Result<()> {
        if self.skip_redaction {
            return Err(syn::Error::new_spanned(
                attr,
                "cannot have multiple occurrences of `skip_redaction` attribute",
            ));
        }

        self.skip_redaction = true;
        Ok(())
    }

    /// Set that this field represents the suffix of the event type.
    ///
    /// Returns an error if it is already set.
    fn set_type_fragment(&mut self, attr: &syn::Attribute) -> syn::Result<()> {
        if self.is_type_fragment {
            return Err(syn::Error::new_spanned(
                attr,
                "cannot have multiple occurrences of `type_fragment` attribute",
            ));
        }

        self.is_type_fragment = true;
        Ok(())
    }

    fn try_merge(&mut self, meta: ParseNestedMeta<'_>, attr: &syn::Attribute) -> syn::Result<()> {
        if meta.path.is_ident("skip_redaction") {
            if meta.has_value() {
                return Err(meta.error("`skip_redaction` attribute doesn't expect a value"));
            }

            return self.set_skip_redaction(attr);
        }

        if meta.path.is_ident("type_fragment") {
            if meta.has_value() {
                return Err(meta.error("`type_fragment` attribute doesn't expect a value"));
            }

            return self.set_type_fragment(attr);
        }

        Err(meta.error("unsupported `ruma_event` attribute"))
    }
}

impl EventContentKind {
    fn try_from_parts(
        kind: Option<EventContentKindAttr>,
        state_key_type: Option<syn::Type>,
        unsigned_type: Option<syn::Type>,
        has_custom_redacted: bool,
        has_custom_possibly_redacted: bool,
        ruma_events: &RumaEvents,
    ) -> syn::Result<Self> {
        let kind = kind.ok_or_else(|| {
            syn::Error::new(
                Span::call_site(),
                "missing `kind` attribute, \
                 add `#[ruma_event(type = \"m.event_type\", kind = Kind)]` \
                 below the event content derive",
            )
        })?;

        let is_state = matches!(kind, EventContentKindAttr::Single(CommonEventKind::State));
        let is_message_like =
            matches!(kind, EventContentKindAttr::Single(CommonEventKind::MessageLike));

        if let Some(state_key_type) = &state_key_type
            && !is_state
        {
            return Err(syn::Error::new_spanned(
                state_key_type,
                "the `state_key_type` attribute is only valid for the State kind",
            ));
        }

        if let Some(unsigned_type) = &unsigned_type
            && !is_state
        {
            return Err(syn::Error::new_spanned(
                unsigned_type,
                "the `unsigned_type` attribute is only valid for the State kind",
            ));
        }

        if has_custom_redacted && !is_state && !is_message_like {
            return Err(syn::Error::new(
                Span::call_site(),
                "the `custom_redacted` attribute is only valid for the State and MessageLike kinds",
            ));
        }

        if has_custom_possibly_redacted && !is_state {
            return Err(syn::Error::new(
                Span::call_site(),
                "the `custom_possibly_redacted` attribute is only valid for the State kind",
            ));
        }

        Ok(match kind {
            EventContentKindAttr::Single(kind) => match kind {
                CommonEventKind::GlobalAccountData => EventContentKind::GlobalAccountData,
                CommonEventKind::RoomAccountData => EventContentKind::RoomAccountData,
                CommonEventKind::EphemeralRoom => EventContentKind::EphemeralRoom,
                CommonEventKind::MessageLike => {
                    EventContentKind::MessageLike { has_custom_redacted }
                }
                CommonEventKind::State => {
                    let state_key_type = state_key_type.ok_or_else(|| {
                        syn::Error::new(Span::call_site(), "missing `state_key_type` attribute")
                    })?;
                    let unsigned_type = unsigned_type.unwrap_or_else(|| {
                        parse_quote! {
                            #ruma_events::StateUnsigned<Self::PossiblyRedacted>
                        }
                    });

                    EventContentKind::State {
                        state_key_type,
                        unsigned_type,
                        has_custom_redacted,
                        has_custom_possibly_redacted,
                    }
                }
                CommonEventKind::ToDevice => EventContentKind::ToDevice,
            },
            EventContentKindAttr::BothAccountData => EventContentKind::BothAccountData,
        })
    }
}

/// The possible values of the `kind` attribute of an event content.
///
/// This is a wrapper around [`EventKind`] that allows to provide two kinds for the same event
/// content.
#[derive(Clone, Copy)]
enum EventContentKindAttr {
    /// The event content has a single kind.
    Single(CommonEventKind),

    /// The event content is of the two account data kinds.
    BothAccountData,
}

impl Parse for EventContentKindAttr {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let first_event_kind: CommonEventKind = input.parse()?;

        let second_event_kind = input
            .peek(syn::Token![+])
            .then(|| {
                let _: syn::Token![+] = input.parse()?;
                input.parse::<CommonEventKind>()
            })
            .transpose()?;

        match (first_event_kind, second_event_kind) {
            (event_kind, None) => Ok(Self::Single(event_kind)),
            (CommonEventKind::GlobalAccountData, Some(CommonEventKind::RoomAccountData))
            | (CommonEventKind::RoomAccountData, Some(CommonEventKind::GlobalAccountData)) => {
                Ok(Self::BothAccountData)
            }
            _ => Err(syn::Error::new(Span::call_site(), "only account data can have two kinds")),
        }
    }
}