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: theYAMLformat 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-onlyLimitsfields and the repository’s.ctl/input.yaml. Needs no garde withoutvalidate.toml— Declared input from TOML: theTOMLformat reads a text into the located tree with thetomlcrate, soInput::openreads.tomlfiles and each problem is placed on its line and column. Needs no YAML crate, and no garde withoutvalidate.json— Declared input from JSON: theJSONformat reads a text into the located tree with its own strict RFC 8259 reader, soInput::openreads.jsonfiles and each problem is placed on its line and column. Needs no JSON crate, no YAML crate, and no garde withoutvalidate.validate— Validate with garde:Input::checkandInput::load.
Re-exports§
pub use de::Tagged;pub use format::Format;pub use format::JSON;jsonpub use format::TOML;tomlpub 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§
- English
Messages - The English wording, which names no parser option.
- Entry
- One entry of the top-level mapping or table of a text.
- Field
Path - 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.
- Input
Error - 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.
Noneleaves the field to the next tier. - Loosened
yaml - A limit a repository’s
REPO_FILEraises 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
Entryis written.
Constants§
- REPO_
FILE yaml - Where a repository overrides limits, relative to its root.
Traits§
Functions§
- default_
formats - The registry
Input::openreads through: whatset_default_formatsinstalled, or elseFormats::builtin. - set_
default_ formats - Set the process-wide registry, which
Input::openreads through. Without a call, the process usesFormats::builtin.Formats::openreads through a registry of the caller’s instead. - set_
default_ limits - Set the process-wide default, which a call’s
Input::limitsbeats, and withyamla repository’s.ctl/input.yamltoo. - set_
messages - Set the wording the process uses for a
Refusal’s text and for every problem anInputreports. Without a call, the process usesEnglishMessages. - use_
repo_ limits yaml - Read
limits:fromroot’sREPO_FILEand 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.