pub mod decoder;
pub mod helpers;
pub mod schema;
pub mod serialize;
pub use schema::{ParsedSchema, SchemaError};
#[derive(Debug, Clone)]
pub struct RenderOpts {
pub assume_binary: bool,
pub include_annotations: bool,
pub indent: usize,
}
impl Default for RenderOpts {
fn default() -> Self {
RenderOpts {
assume_binary: false,
include_annotations: false,
indent: 1,
}
}
}
#[non_exhaustive]
#[derive(Debug)]
pub enum CodecError {
DecodeFailed(String),
TextDecodeFailed(String),
}
impl std::fmt::Display for CodecError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CodecError::DecodeFailed(msg) => write!(f, "decode failed: {msg}"),
CodecError::TextDecodeFailed(msg) => write!(f, "text decode failed: {msg}"),
}
}
}
impl std::error::Error for CodecError {}
pub fn render_as_text(
data: &[u8],
schema: Option<&ParsedSchema>,
opts: RenderOpts,
) -> Result<Vec<u8>, CodecError> {
if !opts.assume_binary && serialize::render_text::is_prototext_text(data) {
return Ok(data.to_vec());
}
Ok(serialize::render_text::decode_and_render(
data,
schema,
opts.include_annotations,
opts.indent,
))
}
pub fn render_as_bytes(data: &[u8], opts: RenderOpts) -> Result<Vec<u8>, CodecError> {
if opts.assume_binary || !serialize::render_text::is_prototext_text(data) {
Ok(data.to_vec())
} else {
Ok(serialize::encode_text::encode_text_to_binary(data))
}
}
pub fn parse_schema(schema_bytes: &[u8], root_message: &str) -> Result<ParsedSchema, SchemaError> {
schema::parse_schema(schema_bytes, root_message)
}