Skip to main content

Crate ctl_input

Crate ctl_input 

Source
Available on crate features json or toml or yaml only.
Expand description

Declared input: read a file, parse it, validate it once, and report every problem on its file and line.

Each format is its own feature. yaml parses YAML with serde-saphyr, json parses JSON with an own strict reader that links no parser crate, and toml parses TOML with the toml crate; none needs another. validate adds Input::check and Input::load, which validate with garde. This is the one boundary where a CLI’s config enters. Downstream code keeps the validated type and never checks it again. With no format feature the crate is empty.

A check that runs after the read, such as one that needs a file the config names, places its problem with a FieldPath and Input::problem_at, the way the loader places garde’s.

A text’s Format is chosen, never guessed. Input::open takes it from the file’s extension through the process’s Formats registry, Formats::open through a registry of the caller’s, and Input::new_with_format takes it for text in memory. With yaml, Input::new and Input::read are YAML.

§The tree, the format trait and the registry

tree holds the located tree: each node keeps its byte span, each scalar its processed value and style, and each mapping its key and value nodes. format::Format is the trait that reads a text into that tree and resolves one scalar for the type a field needs, and Formats is the registry a CLI adds its own format to. YAML, JSON and TOML are the built-in formats, each a value of its feature.

An Input reads its text into that tree once and keeps it. Parsing, placing a problem, listing the top-level entries and checking with garde all work on it, so every format, a CLI’s own included, is read, placed and worded the same way.

§The Deserializer, the messages and Tagged

de::deserialize reads a typed value from a tree, asking the format to resolve each scalar for the type its field needs. A problem is a format::Fault with the bytes it sits at, and Messages words it, in English by default and in another voice after set_messages. Tagged reads a node’s tag beside its value.

use ctl_input::Input;
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Config {
    name: String,
}

let config: Config = Input::new("toy.yaml", "name: release\n").parse()?;
assert_eq!(config.name, "release");

let error = Input::new("toy.yaml", "name: a\nname: b\n")
    .parse::<Config>()
    .unwrap_err();
assert!(error.to_string().starts_with("toy.yaml"));

§Cargo features

  • yaml — Declared input from YAML: the YAML format reads a text into the located tree with serde-saphyr, and every problem is placed on its file and line from that tree. Adds the YAML-only Limits fields and the repository’s .ctl/input.yaml. Needs no garde without validate.
  • toml — Declared input from TOML: the TOML format reads a text into the located tree with the toml crate, so Input::open reads .toml files and each problem is placed on its line and column. Needs no YAML crate, and no garde without validate.
  • json — Declared input from JSON: the JSON format reads a text into the located tree with its own strict RFC 8259 reader, so Input::open reads .json files and each problem is placed on its line and column. Needs no JSON crate, no YAML crate, and no garde without validate.
  • validate — Validate with garde: Input::check and Input::load.

Re-exports§

pub use de::Tagged;
pub use format::Format;
pub use format::JSON;json
pub use format::TOML;toml
pub use format::YAML;yaml

Modules§

de
Reads a typed value from the located tree a format read.
format
The input format interface: what a format reads a text into, and how it reads one scalar for the type a field needs.
tree
The located tree every format reads a text into.

Structs§

EnglishMessages
The English wording, which names no parser option.
Entry
One entry of the top-level mapping or table of a text.
FieldPath
The steps from a document’s root down to a field.
Formats
The input formats a CLI reads, in the order they were registered.
Input
A named text, ready to parse and validate.
InputError
Why an input was refused: every problem found, each placed on a line when the text allows it.
Limits
Overrides for the limits a text is held to, one per field. None leaves the field to the next tier.
Loosenedyaml
A limit a repository’s REPO_FILE raises above its default.
Place
The 1-based line and column where a field is written.
Problem
One thing wrong with an input.

Enums§

Step
One step down a FieldPath: a mapping key or a sequence index.
Written
How the value of an Entry is written.

Constants§

REPO_FILEyaml
Where a repository overrides limits, relative to its root.

Traits§

Messages
The words of every Fault, so a CLI can write them in another language or another voice.

Functions§

default_formats
The registry Input::open reads through: what set_default_formats installed, or else Formats::builtin.
set_default_formats
Set the process-wide registry, which Input::open reads through. Without a call, the process uses Formats::builtin. Formats::open reads through a registry of the caller’s instead.
set_default_limits
Set the process-wide default, which a call’s Input::limits beats, and with yaml a repository’s .ctl/input.yaml too.
set_messages
Set the wording the process uses for a Refusal’s text and for every problem an Input reports. Without a call, the process uses EnglishMessages.
use_repo_limitsyaml
Read limits: from root’s REPO_FILE and install it for every later parse in this process. A repository without the file overrides nothing. The file itself is read under the process default, not under whatever repository was installed before.