pub trait Codec: Send + Sync {
// Required methods
fn decode(&self, bytes: &Bytes, format: &Format) -> Result<Value, Error>;
fn encode(&self, value: &Value, format: &Format) -> Result<Bytes, Error>;
fn supports(&self, format: &Format) -> bool;
}Expand description
Codec for converting between Value and bytes.
Codecs handle the parsing (decode) and serialization (encode) of data. The Core layer doesn’t care about specific formats - that’s the codec’s job.
§Implementing Custom Codecs
use structfs_core_store::{Codec, Value, Format, Error};
use bytes::Bytes;
struct MyProtobufCodec {
// schema, etc.
}
impl Codec for MyProtobufCodec {
fn decode(&self, bytes: &Bytes, format: &Format) -> Result<Value, Error> {
if format != &Format::PROTOBUF {
return Err(Error::UnsupportedFormat(format.clone()));
}
// Parse protobuf bytes into Value...
todo!()
}
fn encode(&self, value: &Value, format: &Format) -> Result<Bytes, Error> {
if format != &Format::PROTOBUF {
return Err(Error::UnsupportedFormat(format.clone()));
}
// Serialize Value to protobuf bytes...
todo!()
}
fn supports(&self, format: &Format) -> bool {
format == &Format::PROTOBUF
}
}Required Methods§
Sourcefn decode(&self, bytes: &Bytes, format: &Format) -> Result<Value, Error>
fn decode(&self, bytes: &Bytes, format: &Format) -> Result<Value, Error>
Decode raw bytes into a Value.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".