use crate::common::error::{FlareError, Result};
use crate::common::protocol::{Frame, SerializationFormat};
use super::traits::Serializer;
pub struct ProtobufSerializer;
impl Serializer for ProtobufSerializer {
fn serialize(&self, frame: &Frame) -> Result<Vec<u8>> {
let mut buf = Vec::new();
prost::Message::encode(frame, &mut buf)
.map_err(|e| FlareError::encoding_error(format!("Protobuf encode error: {}", e)))?;
Ok(buf)
}
fn deserialize(&self, data: &[u8]) -> Result<Frame> {
prost::Message::decode(data)
.map_err(|e| FlareError::deserialization_error(format!("Protobuf decode error: {}", e)))
}
fn format(&self) -> SerializationFormat {
SerializationFormat::Protobuf
}
fn name(&self) -> &'static str {
"protobuf"
}
fn can_detect(&self, data: &[u8]) -> bool {
!data.is_empty()
}
}
pub struct JsonSerializer;
impl Serializer for JsonSerializer {
fn serialize(&self, frame: &Frame) -> Result<Vec<u8>> {
serde_json::to_vec(frame)
.map_err(|e| FlareError::serialization_error(format!("JSON encode error: {}", e)))
}
fn deserialize(&self, data: &[u8]) -> Result<Frame> {
serde_json::from_slice(data)
.map_err(|e| FlareError::deserialization_error(format!("JSON decode error: {}", e)))
}
fn format(&self) -> SerializationFormat {
SerializationFormat::Json
}
fn name(&self) -> &'static str {
"json"
}
fn can_detect(&self, data: &[u8]) -> bool {
if data.is_empty() {
return false;
}
let first = data[0];
first == b'{' || first == b'['
}
}