Skip to main content

Codec

Trait Codec 

Source
pub trait Codec:
    Debug
    + Send
    + Sync
    + 'static {
    // Required methods
    fn name(&self) -> &'static str;
    fn encode_value(&self, value: &Value) -> AppResult<String>;
    fn decode_value(&self, contents: &str) -> AppResult<Value>;
}
Expand description

Object-safe contract for a structured-text codec over the Value model.

Implementations encode and decode one on-disk/text representation (TOML, JSON, …) to and from serde_json::Value. The trait is intentionally object-safe so a codec can be held as Arc<dyn Codec> and selected at runtime; type-driven conversions live in the free functions encode and decode.

Required Methods§

Source

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

A short identifier for diagnostics (for example "toml").

Source

fn encode_value(&self, value: &Value) -> AppResult<String>

Encode a value tree into this codec’s textual representation.

§Errors

Returns a typed AppError (cause preserved) when value cannot be represented in this format.

Source

fn decode_value(&self, contents: &str) -> AppResult<Value>

Decode text into a value tree.

§Errors

Returns a typed AppError (cause preserved) when contents is malformed for this format.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§