Skip to main content

TypedReader

Trait TypedReader 

Source
pub trait TypedReader: Reader {
    // Provided methods
    fn read_as<T: DeserializeOwned>(
        &mut self,
        from: &Path,
        codec: &dyn Codec,
    ) -> Result<Option<T>, Error> { ... }
    fn read_json(
        &mut self,
        from: &Path,
        codec: &dyn Codec,
    ) -> Result<Option<Value>, Error> { ... }
    fn read_typed<T: DeserializeOwned>(
        &mut self,
        from: &Path,
    ) -> Result<Option<T>, Error> { ... }
    fn read_children_typed<T: DeserializeOwned>(
        &mut self,
        from: &Path,
    ) -> Result<Option<Vec<(String, T)>>, Error> { ... }
}
Expand description

Extension trait for typed reads.

This trait is automatically implemented for all Reader implementations. It provides convenience methods for reading data directly into Rust types.

§Example

use structfs_serde_store::{TypedReader, JsonCodec};
use serde::Deserialize;

#[derive(Deserialize)]
struct Config {
    debug: bool,
    port: u16,
}

fn read_config(store: &mut dyn Reader) -> Result<Config, Error> {
    let codec = JsonCodec;
    store.read_as(&path!("config"), &codec)?
        .ok_or_else(|| Error::Other { message: "config not found".into() })
}

Provided Methods§

Source

fn read_as<T: DeserializeOwned>( &mut self, from: &Path, codec: &dyn Codec, ) -> Result<Option<T>, Error>

Read a value and deserialize it into a Rust type.

This method:

  1. Reads the Record from the store
  2. Parses it to a Value using the codec (if raw)
  3. Deserializes the Value to the target type
Source

fn read_json( &mut self, from: &Path, codec: &dyn Codec, ) -> Result<Option<Value>, Error>

Read a value as a serde_json::Value.

Convenience method when you don’t know the exact type.

Source

fn read_typed<T: DeserializeOwned>( &mut self, from: &Path, ) -> Result<Option<T>, Error>

Codec-free typed read.

Most stores return Record::Parsed values, where a codec argument is meaningless; this method deserializes them directly via serde. Raw JSON records are parsed with the bounded JSON profile; other raw formats are an UnsupportedFormat error — use TypedReader::read_as with a codec for those.

let config: Option<Config> = store.read_typed(&path!("config"))?;
Source

fn read_children_typed<T: DeserializeOwned>( &mut self, from: &Path, ) -> Result<Option<Vec<(String, T)>>, Error>

Enumerate children at a prefix and deserialize each into T.

Returns pairs of (child_name, value). Children that are absent between the enumeration and the read are skipped.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§

Source§

impl<R: Reader + ?Sized> TypedReader for R