events-macros: Get rid of lots of unwrapping
This commit is contained in:
parent
9103ad74bb
commit
fd15dcda9f
@ -416,9 +416,9 @@ fn expand_redact_event(
|
||||
) -> TokenStream {
|
||||
let ruma_identifiers = quote! { #ruma_events::exports::ruma_identifiers };
|
||||
|
||||
let redacted_type = kind.to_event_ident(var.to_redacted().unwrap()).unwrap();
|
||||
let redacted_type = kind.to_event_ident(var.to_redacted());
|
||||
let redacted_content_trait =
|
||||
format_ident!("{}Content", kind.to_event_ident(EventKindVariation::Redacted).unwrap());
|
||||
format_ident!("{}Content", kind.to_event_ident(EventKindVariation::Redacted));
|
||||
let ident = &input.ident;
|
||||
|
||||
let mut generics = input.generics.clone();
|
||||
@ -489,7 +489,7 @@ fn expand_sync_from_into_full(
|
||||
let ruma_identifiers = quote! { #ruma_events::exports::ruma_identifiers };
|
||||
|
||||
let ident = &input.ident;
|
||||
let full_struct = kind.to_event_ident(var.to_full().unwrap()).unwrap();
|
||||
let full_struct = kind.to_event_ident(var.to_full());
|
||||
let (impl_generics, ty_gen, where_clause) = input.generics.split_for_impl();
|
||||
let fields: Vec<_> = fields.iter().flat_map(|f| &f.ident).collect();
|
||||
|
||||
|
@ -350,7 +350,7 @@ fn generate_event_type_aliases(
|
||||
EventKindVariation::RedactedSync,
|
||||
]
|
||||
.iter()
|
||||
.filter_map(|&kind| Some((kind, event_kind.to_event_ident(kind)?)))
|
||||
.filter_map(|&kind| Some((kind, event_kind.try_to_event_ident(kind)?)))
|
||||
.map(|(kind, ev_struct)| {
|
||||
let ev_type = format_ident!("{}{}", kind, ev_type_s);
|
||||
|
||||
|
@ -65,8 +65,8 @@ fn expand_event_enum(
|
||||
) -> TokenStream {
|
||||
let serde = quote! { #ruma_events::exports::serde };
|
||||
|
||||
let event_struct = kind.to_event_ident(var).unwrap();
|
||||
let ident = kind.to_event_enum_ident(var).unwrap();
|
||||
let event_struct = kind.to_event_ident(var);
|
||||
let ident = kind.to_event_enum_ident(var);
|
||||
|
||||
let variant_decls = variants.iter().map(|v| v.decl());
|
||||
let content: Vec<_> =
|
||||
@ -121,8 +121,8 @@ fn expand_deserialize_impl(
|
||||
let serde = quote! { #ruma_events::exports::serde };
|
||||
let serde_json = quote! { #ruma_events::exports::serde_json };
|
||||
|
||||
let ident = kind.to_event_enum_ident(var).unwrap();
|
||||
let event_struct = kind.to_event_ident(var).unwrap();
|
||||
let ident = kind.to_event_enum_ident(var);
|
||||
let event_struct = kind.to_event_ident(var);
|
||||
|
||||
let variant_attrs = variants.iter().map(|v| {
|
||||
let attrs = &v.attrs;
|
||||
@ -211,8 +211,8 @@ fn expand_from_full_event(
|
||||
var: EventKindVariation,
|
||||
variants: &[EventEnumVariant],
|
||||
) -> TokenStream {
|
||||
let ident = kind.to_event_enum_ident(var).unwrap();
|
||||
let sync = kind.to_event_enum_ident(var.to_sync().unwrap()).unwrap();
|
||||
let ident = kind.to_event_enum_ident(var);
|
||||
let sync = kind.to_event_enum_ident(var.to_sync());
|
||||
|
||||
let ident_variants = variants.iter().map(|v| v.match_arm(&ident));
|
||||
let self_variants = variants.iter().map(|v| v.ctor(quote! { Self }));
|
||||
@ -244,8 +244,8 @@ fn expand_into_full_event(
|
||||
) -> TokenStream {
|
||||
let ruma_identifiers = quote! { #ruma_events::exports::ruma_identifiers };
|
||||
|
||||
let ident = kind.to_event_enum_ident(var).unwrap();
|
||||
let full = kind.to_event_enum_ident(var.to_full().unwrap()).unwrap();
|
||||
let ident = kind.to_event_enum_ident(var);
|
||||
let full = kind.to_event_enum_ident(var.to_full());
|
||||
|
||||
let self_variants = variants.iter().map(|v| v.match_arm(quote! { Self }));
|
||||
let full_variants = variants.iter().map(|v| v.ctor(&full));
|
||||
@ -361,8 +361,8 @@ fn expand_redact(
|
||||
) -> TokenStream {
|
||||
let ruma_identifiers = quote! { #ruma_events::exports::ruma_identifiers };
|
||||
|
||||
let ident = kind.to_event_enum_ident(var).unwrap();
|
||||
let redacted_enum = kind.to_event_enum_ident(var.to_redacted().unwrap()).unwrap();
|
||||
let ident = kind.to_event_enum_ident(var);
|
||||
let redacted_enum = kind.to_event_enum_ident(var.to_redacted());
|
||||
|
||||
let self_variants = variants.iter().map(|v| v.match_arm(quote! { Self }));
|
||||
let redacted_variants = variants.iter().map(|v| v.ctor(&redacted_enum));
|
||||
@ -400,9 +400,9 @@ fn expand_possibly_redacted_enum(
|
||||
let serde = quote! { #ruma_events::exports::serde };
|
||||
let serde_json = quote! { #ruma_events::exports::serde_json };
|
||||
|
||||
let ident = format_ident!("AnyPossiblyRedacted{}", kind.to_event_ident(var).unwrap());
|
||||
let regular_enum_ident = kind.to_event_enum_ident(var).unwrap();
|
||||
let redacted_enum_ident = kind.to_event_enum_ident(var.to_redacted().unwrap()).unwrap();
|
||||
let ident = format_ident!("AnyPossiblyRedacted{}", kind.to_event_ident(var));
|
||||
let regular_enum_ident = kind.to_event_enum_ident(var);
|
||||
let redacted_enum_ident = kind.to_event_enum_ident(var.to_redacted());
|
||||
|
||||
quote! {
|
||||
/// An enum that holds either regular un-redacted events or redacted events.
|
||||
@ -461,7 +461,7 @@ fn expand_accessor_methods(
|
||||
variants: &[EventEnumVariant],
|
||||
ruma_events: &TokenStream,
|
||||
) -> TokenStream {
|
||||
let ident = kind.to_event_enum_ident(var).unwrap();
|
||||
let ident = kind.to_event_enum_ident(var);
|
||||
let self_variants: Vec<_> = variants.iter().map(|v| v.match_arm(quote! { Self })).collect();
|
||||
|
||||
let content_accessors = (!var.is_redacted()).then(|| {
|
||||
|
@ -49,27 +49,27 @@ impl EventKindVariation {
|
||||
matches!(self, Self::Sync | Self::RedactedSync)
|
||||
}
|
||||
|
||||
pub fn to_redacted(self) -> Option<Self> {
|
||||
pub fn to_redacted(self) -> Self {
|
||||
match self {
|
||||
EventKindVariation::Full => Some(EventKindVariation::Redacted),
|
||||
EventKindVariation::Sync => Some(EventKindVariation::RedactedSync),
|
||||
_ => None,
|
||||
EventKindVariation::Full => EventKindVariation::Redacted,
|
||||
EventKindVariation::Sync => EventKindVariation::RedactedSync,
|
||||
_ => panic!("No redacted form of {:?}", self),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_sync(self) -> Option<Self> {
|
||||
pub fn to_sync(self) -> Self {
|
||||
match self {
|
||||
EventKindVariation::Full => Some(EventKindVariation::Sync),
|
||||
EventKindVariation::Redacted => Some(EventKindVariation::RedactedSync),
|
||||
_ => None,
|
||||
EventKindVariation::Full => EventKindVariation::Sync,
|
||||
EventKindVariation::Redacted => EventKindVariation::RedactedSync,
|
||||
_ => panic!("No sync form of {:?}", self),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_full(self) -> Option<Self> {
|
||||
pub fn to_full(self) -> Self {
|
||||
match self {
|
||||
EventKindVariation::Sync => Some(EventKindVariation::Full),
|
||||
EventKindVariation::RedactedSync => Some(EventKindVariation::Redacted),
|
||||
_ => None,
|
||||
EventKindVariation::Sync => EventKindVariation::Full,
|
||||
EventKindVariation::RedactedSync => EventKindVariation::Redacted,
|
||||
_ => panic!("No full form of {:?}", self),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -125,10 +125,9 @@ impl IdentFragment for EventKindVariation {
|
||||
}
|
||||
|
||||
impl EventKind {
|
||||
pub fn to_event_ident(self, var: EventKindVariation) -> Option<Ident> {
|
||||
pub fn try_to_event_ident(self, var: EventKindVariation) -> Option<Ident> {
|
||||
use EventKindVariation as V;
|
||||
|
||||
// this match is only used to validate the input
|
||||
match (self, var) {
|
||||
(_, V::Full)
|
||||
| (Self::Message | Self::RoomRedaction | Self::State | Self::Ephemeral, V::Sync)
|
||||
@ -138,8 +137,14 @@ impl EventKind {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_event_enum_ident(self, var: EventKindVariation) -> Option<Ident> {
|
||||
Some(format_ident!("Any{}", self.to_event_ident(var)?))
|
||||
pub fn to_event_ident(self, var: EventKindVariation) -> Ident {
|
||||
self.try_to_event_ident(var).unwrap_or_else(|| {
|
||||
panic!("({:?}, {:?}) is not a valid event kind / variation combination", self, var);
|
||||
})
|
||||
}
|
||||
|
||||
pub fn to_event_enum_ident(self, var: EventKindVariation) -> Ident {
|
||||
format_ident!("Any{}", self.to_event_ident(var))
|
||||
}
|
||||
|
||||
/// `Any[kind]EventContent`
|
||||
|
Loading…
x
Reference in New Issue
Block a user