cloudevents / sdk-rust

Rust library to interact with CloudEvents
Apache License 2.0
177 stars 61 forks source link

Implementation for KafkaRecord / blanket implementation #225

Open DirkRusche opened 7 months ago

DirkRusche commented 7 months ago

I have an AWS Lambda that consumes messages from Kafka topics, using the crate aws_lambda_events which provides the struct KafkaRecord. Unfortunately, I was unable to use this library out of the box. I ended up understanding what the library does and implementing a Deserializer similar to rdkafka.

While doing that, I noticed that the implementation impl BinaryDeserializer for ConsumerRecordDeserializer and impl<'a, T: Headers<'a>> BinaryDeserializer for Deserializer<'a, T> are almost the same. I was wondering if this library can provide a blanket implementation for some T that implements a given trait. The trait should provide methods to retrieve the headers, the payload and also provide the CE prefix (as it is ce_ with Kafka and ce- with http).

Something like that (just as a sketch):

trait CustomDeserializeTrait {
    fn headers(&self) -> HashMap<String, String>;
    fn payload(&self) -> Option<Vec<u8>>;

    fn prefix() -> &'static str;
}

impl BinaryDeserializer for T where T : CustomDeserializeTrait {
...
}

If this is from interest and I find some time, I could open a PR. Let me know 😊

Lazzaretti commented 6 months ago

Hey @DirkRusche, Sorry for the late response. It would be great if we could simplify this. It would be great if you could find some time to make a PR!

Also, an example with an AWS Lambda and Kafka would be really great :)

Feel free to ping me on Slack if you need help: https://cloud-native.slack.com/team/UTV7ZGPAR