Skip to main content

Codec

Trait Codec 

Source
pub trait Codec: Send + Sync {
    // Required methods
    fn name(&self) -> &'static str;
    fn write<D: Serialize + ?Sized>(
        &self,
        document: &D,
    ) -> Result<String, CodecError>;
    fn read<'de, S: DeserializeSeed<'de>>(
        &self,
        text: &'de str,
        seed: S,
    ) -> Result<S::Value, CodecError>;

    // Provided method
    fn presentation(&self) -> Presentation { ... }
}
Expand description

A serde text format a FileTable keeps its records in. The kit names no format: the caller implements this over the format crate it picks.

use coqui_access_kit::file::{Codec, CodecError};
use serde::Serialize;
use serde::de::DeserializeSeed;

struct Json;

impl Codec for Json {
    fn name(&self) -> &'static str {
        "JSON"
    }

    fn write<D: Serialize + ?Sized>(&self, document: &D) -> Result<String, CodecError> {
        serde_json::to_string_pretty(document).map_err(|_| CodecError::new())
    }

    fn read<'de, S: DeserializeSeed<'de>>(
        &self,
        text: &'de str,
        seed: S,
    ) -> Result<S::Value, CodecError> {
        let mut deserializer = serde_json::Deserializer::from_str(text);
        let read = seed.deserialize(&mut deserializer).map_err(|_| CodecError::new())?;
        deserializer.end().map_err(|_| CodecError::new())?;
        Ok(read)
    }
}

Required Methods§

Source

fn name(&self) -> &'static str

The format’s name, as an error message shows it: TOML, JSON.

Source

fn write<D: Serialize + ?Sized>( &self, document: &D, ) -> Result<String, CodecError>

Writes document as text.

§Errors

CodecError when the format cannot write it.

Source

fn read<'de, S: DeserializeSeed<'de>>( &self, text: &'de str, seed: S, ) -> Result<S::Value, CodecError>

Reads text through seed, refusing anything after the document.

§Errors

CodecError when text is not a document of this format or seed refuses it.

Provided Methods§

Source

fn presentation(&self) -> Presentation

How bytes and numbers are spelled in this format. Default [Presentation::new].

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§