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§
Sourcefn read_as<T: DeserializeOwned>(
&mut self,
from: &Path,
codec: &dyn Codec,
) -> Result<Option<T>, Error>
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:
- Reads the Record from the store
- Parses it to a Value using the codec (if raw)
- Deserializes the Value to the target type
Sourcefn read_json(
&mut self,
from: &Path,
codec: &dyn Codec,
) -> Result<Option<Value>, Error>
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.
Sourcefn read_typed<T: DeserializeOwned>(
&mut self,
from: &Path,
) -> Result<Option<T>, Error>
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"))?;Sourcefn read_children_typed<T: DeserializeOwned>(
&mut self,
from: &Path,
) -> Result<Option<Vec<(String, T)>>, Error>
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".