Skip to main content

Format

Trait Format 

Source
pub trait Format: Sync {
    // Required methods
    fn name(&self) -> &'static str;
    fn extensions(&self) -> &'static [&'static str];
    fn read(
        &self,
        text: &str,
        limits: &Limits,
        reading: Reading,
    ) -> Result<Node, Refusal>;
    fn resolve<'a>(
        &self,
        scalar: &'a Scalar,
        tag: Option<&Tag>,
        wanted: Wanted,
    ) -> Result<Resolved<'a>, Mismatch>;
}
Available on crate features json or toml or yaml only.
Expand description

A way to write input, read into the located tree.

The trait is object safe: Formats holds &'static dyn Format, as ctl-core’s output registry does.

use ctl_input::Limits;
use ctl_input::format::{Format, Reading, YAML};

let formats: Vec<&'static dyn Format> = vec![&YAML];
let tree = formats[0].read("a: 1\n", &Limits::NONE, Reading::Strict)?;
assert_eq!(tree.span(), 0..4);

Required Methods§

Source

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

The name the registry finds the format by, such as yaml.

Source

fn extensions(&self) -> &'static [&'static str]

The file extensions the format reads, in lower case and without the dot, such as yaml and yml.

Source

fn read( &self, text: &str, limits: &Limits, reading: Reading, ) -> Result<Node, Refusal>

Read text into the tree, under limits.

The read refuses a second document and anything past a limit, and reading says whether it also refuses duplicate and merge keys. A format with aliases expands each one while it reads, so the tree holds no alias node.

§Errors

A Refusal naming the fault and where it is.

Source

fn resolve<'a>( &self, scalar: &'a Scalar, tag: Option<&Tag>, wanted: Wanted, ) -> Result<Resolved<'a>, Mismatch>

Read one scalar, with the tag its node carries, as the type a field wanted.

This applies the rules of a Reading::Strict read, whichever reading built the tree: YAML, for one, refuses .inf and .nan for Wanted::Any. A listing asks of each value only whether it is text, with Wanted::Str.

§Errors

A Mismatch when the scalar cannot be read as wanted.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§

Source§

impl Format for Json

Available on crate feature json only.
Source§

impl Format for Toml

Available on crate feature toml only.
Source§

impl Format for Yaml

Available on crate feature yaml only.