martin-g commented on code in PR #190:
URL: https://github.com/apache/avro-rs/pull/190#discussion_r2064388162


##########
avro/src/headers.rs:
##########
@@ -0,0 +1,163 @@
+//! Handling of Avro magic headers
+use uuid::Uuid;
+
+use crate::{rabin::Rabin, schema::SchemaFingerprint, AvroResult, Schema};
+
+/// This trait represents that an object is able to construct an Avro message 
header. It is
+/// implemented for some known header types already. If you need a header type 
that is not already
+/// included here, then you can create your own struct and implement this 
trait.
+pub trait HeaderBuilder {
+    fn build_header(&self) -> Vec<u8>;
+}
+
+/// HeaderBuilder based on the Rabin schema fingerprint
+///
+/// This is the default, and will be used automatically by the `new` impls in
+/// `GenericSingleObjectReader` and `GenericSingleObjectWriter`.
+pub struct RabinFingerprintHeader {
+    fingerprint: SchemaFingerprint,
+}
+
+impl RabinFingerprintHeader {
+    /// Use this helper to build an instance from an existing Avro `Schema`.
+    pub fn from_schema(schema: &Schema) -> Self {
+        let fingerprint = schema.fingerprint::<Rabin>();
+        RabinFingerprintHeader { fingerprint }
+    }
+}
+
+impl HeaderBuilder for RabinFingerprintHeader {
+    fn build_header(&self) -> Vec<u8> {
+        vec![
+            0xC3,
+            0x01,
+            self.fingerprint.bytes[0],
+            self.fingerprint.bytes[1],
+            self.fingerprint.bytes[2],
+            self.fingerprint.bytes[3],
+            self.fingerprint.bytes[4],
+            self.fingerprint.bytes[5],
+            self.fingerprint.bytes[6],
+            self.fingerprint.bytes[7],
+        ]
+    }
+}
+
+/// HeaderBuilder based on Glue schema UUID
+///
+/// See the function docs for usage details
+pub struct GlueSchemaUuidHeader {
+    schema_uuid: Uuid,
+}
+
+impl GlueSchemaUuidHeader {
+    /// Create an instance of the struct from a Glue Schema UUID
+    ///
+    /// Code for writing messages will most likely want to use this. You will 
need to determine
+    /// via other means the correct Glue schema UUID and use it with this 
method to be able to
+    /// create Avro-encoded messages with the correct headers.
+    pub fn from_uuid(schema_uuid: Uuid) -> Self {
+        GlueSchemaUuidHeader { schema_uuid }
+    }
+
+    /// Create an instance of the struct based on parsing the UUID out of the 
header of a raw
+    /// message
+    ///
+    /// Code for reading messages will most likely want to use this. Once you 
receive the raw bytes
+    /// of a message, use this function to build the struct from it. That 
struct can then be used
+    /// with the below `schema_uuid` function to retrieve the UUID in order to 
retrieve the correct
+    /// schema for the message. You can then use the raw message, the schema, 
and the struct
+    /// instance to read the message.
+    pub fn parse_from_raw_avro(message_payload: &[u8]) -> AvroResult<Self> {
+        if message_payload.len() < 19 {

Review Comment:
   Why `19` ?
   The special prefix (`3, 0`) needs two bytes + 16 for the Uuid. What is the 
19th ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to