From 7a4f6fbbbf91f4e3f29d1a3f1a4c3b783a16b43b Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 7 May 2021 02:15:21 -0400 Subject: [PATCH] new enum type --- src/proto_utils.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/proto_utils.rs b/src/proto_utils.rs index fa70b50785..1151be0666 100644 --- a/src/proto_utils.rs +++ b/src/proto_utils.rs @@ -10,6 +10,39 @@ use prost::Message; // TODO: add some type safety w/ generics and type bounds +/// Sources of events +pub enum InboundEventSource { + Twitch(IngestionTwitchMetadata), +} + +/// Events +pub enum InboundEvent { + TwitchMessage(IngestionTwitchMessage), +} + +impl InboundEventSource { + pub fn parse_from_payload(event_payload: &PubsubEventPayloadV1) + -> AnyhowResult + { + match Self::parse_type_proto(event_payload) { + Some(IngestionSourceType::IstTwitch) => { + IngestionTwitchMetadata::decode(event_payload.ingestion_source_data()) + .map(|m| Self::Twitch(m)) + .map_err(|_| anyhow!("error decoding source proto")) + }, + None => Err(anyhow!("invalid source type")), + Some(_) => Err(anyhow!("type is not yet handled")), + } + } + + fn parse_type_proto(event_payload: &PubsubEventPayloadV1) + -> Option + { + event_payload.ingestion_payload_type + .and_then(|num| IngestionSourceType::from_i32(num)) + } +} + pub fn get_source_type(event_payload: &PubsubEventPayloadV1) -> Option {