ctl-input 0.8.0-rc.2

Declared input for CLIs: parse YAML, JSON or TOML, validate once, and report every problem on its file and line
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! 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.
//!
//! ```
//! # #[cfg(all(feature = "yaml", feature = "validate"))]
//! # {
//! 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"));
//! # }
//! # Ok::<(), ctl_input::InputError>(())
//! ```
//!
//! ## Cargo features
#![cfg_attr(
    not(any(feature = "yaml", feature = "json", feature = "toml")),
    doc = "",
    doc = "[`FieldPath`]: crate#cargo-features",
    doc = "[`Input::problem_at`]: crate#cargo-features",
    doc = "[`Format`]: crate#cargo-features",
    doc = "[`Formats`]: crate#cargo-features",
    doc = "[`Formats::open`]: crate#cargo-features",
    doc = "[`Input`]: crate#cargo-features",
    doc = "[`Input::open`]: crate#cargo-features",
    doc = "[`Input::new_with_format`]: crate#cargo-features"
)]
#![doc = document_features::document_features!()]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg(any(feature = "yaml", feature = "json", feature = "toml"))]

use std::fmt;
use std::path::Path;

#[cfg(feature = "validate")]
use garde::Validate;
use serde::de::DeserializeOwned;

pub mod de;
mod entries;
mod exceeded;
mod field_path;
pub mod format;
mod formats;
mod input_error;
#[cfg(feature = "json")]
mod json;
#[cfg(feature = "json")]
mod json_format;
mod kept;
mod limits;
mod messages;
mod placing;
#[cfg(feature = "toml")]
mod toml_format;
pub mod tree;
#[cfg(feature = "yaml")]
mod yaml_format;

pub use de::Tagged;
pub use entries::{Entry, Written};
pub use field_path::{FieldPath, Place, Step};
pub use format::Format;
#[cfg(feature = "json")]
pub use format::JSON;
#[cfg(feature = "toml")]
pub use format::TOML;
#[cfg(feature = "yaml")]
pub use format::YAML;
use format::{Fault, Raise, Reading, Refusal};
pub use formats::{Formats, default_formats, set_default_formats};
use input_error::read_text;
pub use input_error::{InputError, Problem};
use kept::Kept;
pub use limits::{Limits, set_default_limits};
#[cfg(feature = "yaml")]
pub use limits::{Loosened, REPO_FILE, use_repo_limits};
use messages::messages;
pub use messages::{EnglishMessages, Messages, set_messages};
use placing::At;

/// A named text, ready to parse and validate.
///
/// The name is usually the file's path, and appears in every problem. Build
/// one with [`Input::new_with_format`] for text in memory, or [`Input::open`]
/// for a file. With `yaml`, `Input::new` and `Input::read` build a YAML
/// one.
#[derive(Clone)]
pub struct Input {
    name: String,
    text: String,
    lines_before: usize,
    limits: Limits,
    format: &'static dyn Format,
    /// The schema fields serde flattens into their parents, which
    /// [`Input::flattened`] declares.
    #[cfg(feature = "validate")]
    flattened: Vec<FieldPath>,
    /// The tree the text was read into, for every operation to share.
    kept: Kept,
}

/// A format has no `Debug`, so it shows by its name, and the kept tree is
/// left out.
impl fmt::Debug for Input {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut shown = formatter.debug_struct("Input");
        shown
            .field("name", &self.name)
            .field("text", &self.text)
            .field("lines_before", &self.lines_before)
            .field("limits", &self.limits)
            .field("format", &self.format.name());
        #[cfg(feature = "validate")]
        shown.field("flattened", &self.flattened);
        shown.finish_non_exhaustive()
    }
}

/// Two inputs are equal when they read the same text the same way. A format
/// is compared by its name, and the kept tree is derived from the rest, so
/// placing on one input does not make it differ from its clone.
impl PartialEq for Input {
    fn eq(&self, other: &Self) -> bool {
        let same = self.name == other.name
            && self.text == other.text
            && self.lines_before == other.lines_before
            && self.limits == other.limits
            && self.format.name() == other.format.name();
        #[cfg(feature = "validate")]
        let same = same && self.flattened == other.flattened;
        same
    }
}

impl Eq for Input {}

impl Input {
    /// A YAML input from text already in memory, whatever the extension of
    /// `name`. Set another format with [`Input::format`].
    #[cfg(feature = "yaml")]
    #[must_use]
    pub fn new(name: impl Into<String>, text: impl Into<String>) -> Self {
        Self::new_with_format(name, text, &YAML)
    }

    /// An input of `format` from text already in memory, whatever the
    /// extension of `name`. The format is any [`Format`], one a CLI wrote
    /// included, and a build needs only its own feature: a build without YAML
    /// builds text in memory with it.
    #[must_use]
    pub fn new_with_format(
        name: impl Into<String>,
        text: impl Into<String>,
        format: &'static dyn Format,
    ) -> Self {
        Self {
            name: name.into(),
            text: text.into(),
            lines_before: 0,
            limits: Limits::NONE,
            format,
            #[cfg(feature = "validate")]
            flattened: Vec::new(),
            kept: Kept::default(),
        }
    }

    /// Read a YAML file, named by its path, whatever its extension.
    ///
    /// # Errors
    ///
    /// An [`InputError`] with the I/O failure.
    #[cfg(feature = "yaml")]
    pub fn read(path: &Path) -> Result<Self, InputError> {
        let (name, text) = read_text(path)?;
        Ok(Self::new(name, text))
    }

    /// Read a file and take its [`Format`] from its extension, through the
    /// registry the process uses: what [`set_default_formats`] installed, or
    /// else [`Formats::builtin`]. [`Formats::open`] reads through a registry
    /// of the caller's instead.
    ///
    /// # Errors
    ///
    /// An [`InputError`] for an extension [`Formats::of`] refuses, which is
    /// checked before the file is read, or with the I/O failure.
    pub fn open(path: &Path) -> Result<Self, InputError> {
        formats::default_formats().open(path)
    }

    /// Set how the text is written, over what the constructor took.
    #[must_use]
    pub fn format(mut self, format: &'static dyn Format) -> Self {
        self.format = format;
        self.kept = Kept::default();
        self
    }

    /// The name given at construction, usually the file's path.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// The text as read, unchanged.
    #[must_use]
    pub fn text(&self) -> &str {
        &self.text
    }

    /// Split YAML frontmatter from a Markdown body.
    ///
    /// The frontmatter is the text between a first line of `---` and the next
    /// line of `---`. It is YAML whatever this input's format, and problems in
    /// it keep the line numbers of the whole file. The second item is the body
    /// after the closing line. Returns `None` when the text does not start with
    /// a `---` line, or never closes it.
    #[cfg(feature = "yaml")]
    #[must_use]
    pub fn frontmatter(&self) -> Option<(Self, &str)> {
        let text = self.text.strip_prefix('\u{feff}').unwrap_or(&self.text);
        let rest = text
            .strip_prefix("---\n")
            .or_else(|| text.strip_prefix("---\r\n"))?;
        let mut offset = 0;
        for line in rest.split_inclusive('\n') {
            if line.trim_end() == "---" {
                let matter = Self {
                    name: self.name.clone(),
                    text: rest[..offset].to_owned(),
                    lines_before: self.lines_before + 1,
                    limits: self.limits.clone(),
                    format: &YAML,
                    #[cfg(feature = "validate")]
                    flattened: self.flattened.clone(),
                    kept: Kept::default(),
                };
                return Some((matter, &rest[offset + line.len()..]));
            }
            offset += line.len();
        }
        None
    }

    /// Override limits for this input alone, above every other tier. See
    /// [`Limits`] for the order.
    #[must_use]
    pub fn limits(mut self, limits: Limits) -> Self {
        self.limits = limits;
        self
    }

    /// Parse the text into `T`, without validating it.
    ///
    /// The text is read into the input's tree, which the input keeps for
    /// placing and listing, and `T` is read from that tree. The first problem
    /// found is the one reported, in the order the text is written. The format
    /// decides what a scalar can be: in YAML only `true` and `false` are
    /// booleans, and a TOML string fills only a string. A duplicate key or a
    /// merge key (`<<`) is refused.
    ///
    /// The text must fit the limits in force, decided field by field through
    /// the tiers [`Limits`] lists. TOML and JSON read only
    /// `max_reader_input_bytes`, `max_depth`, and `max_nodes`, because the
    /// other fields name YAML syntax.
    ///
    /// # Errors
    ///
    /// An [`InputError`] with the problem and its line. A limit problem names
    /// the limit, its value, and where to raise it, or, without `yaml`, that
    /// the tool sets it.
    pub fn parse<T: DeserializeOwned>(&self) -> Result<T, InputError> {
        self.parse_within(&limits::effective(&self.limits), Raise::FOR_TEXT)
    }

    /// Lend the root node of the text's tree to `walk`, read as
    /// [`Self::parse`] reads it, and return what `walk` returns.
    ///
    /// The read is strict, under the limits in force, so a duplicate key, a
    /// merge key, a second document and a text past a limit are refused the
    /// same way, each on its line. The node is the tree the input keeps for
    /// placing, lent without a copy. A caller that walks the tree itself, such
    /// as one that turns a document into command-line arguments, places its
    /// own problems with [`Input::place`] and [`Input::place_key`].
    ///
    /// # Errors
    ///
    /// An [`InputError`] with the problem and its line, as [`Self::parse`]
    /// reports it.
    pub fn tree<T>(&self, walk: impl FnOnce(&tree::Node) -> T) -> Result<T, InputError> {
        let limits = limits::effective(&self.limits);
        self.fits(&limits, Raise::FOR_TEXT)?;
        let read = self
            .kept
            .tree(self.format, &self.text, &limits, Reading::Strict);
        match read.as_ref() {
            Ok(root) => Ok(walk(root)),
            Err(refusal) => Err(self.refused(refusal, Raise::FOR_TEXT)),
        }
    }

    /// [`Self::parse`] under exactly `limits`, whatever the tiers say.
    /// `raise` is where a person raises a limit the text goes past.
    fn parse_within<T: DeserializeOwned>(
        &self,
        limits: &Limits,
        raise: Raise,
    ) -> Result<T, InputError> {
        self.fits(limits, raise)?;
        let read = self
            .kept
            .tree(self.format, &self.text, limits, Reading::Strict);
        let tree = read
            .as_ref()
            .as_ref()
            .map_err(|refusal| self.refused(refusal, raise))?;
        de::deserialize(tree, self.format).map_err(|refusal| self.refused(&refusal, raise))
    }

    /// The input's problem for `refusal`, placed on its line and column, and
    /// worded by [`Messages`]. A refusal in a node an alias replayed also
    /// names where its value is defined, counted from the file.
    fn refused(&self, refusal: &Refusal, raise: Raise) -> InputError {
        let fault = if let Fault::Limit { name, value, .. } = refusal.fault().clone() {
            Fault::Limit { name, value, raise }
        } else {
            refusal.fault().clone()
        };
        let mut message = worded(&fault);
        if let Some(defined) = refusal.defined_at() {
            let (line, column) = tree::position(&self.text, defined.start);
            let line = line + self.lines_before;
            message = format!("{message} (defined at line {line}, column {column})");
        }
        let place = refusal
            .span()
            .map(|span| tree::position(&self.text, span.start));
        InputError::one(
            self.name.clone(),
            Problem {
                line: place.map(|(line, _)| line + self.lines_before),
                column: place.map(|(_, column)| column),
                field: String::new(),
                message,
            },
        )
    }

    /// Whether the text is within the size `limits` allow.
    fn fits(&self, limits: &Limits, raise: Raise) -> Result<(), InputError> {
        let bounds = limits.bounds();
        if self.text.len() > bounds.input_bytes {
            let fault = Fault::TooLarge {
                bytes: u64::try_from(self.text.len()).unwrap_or(u64::MAX),
                limit: u64::try_from(bounds.input_bytes).unwrap_or(u64::MAX),
                raise,
            };
            return Err(InputError::whole(self.name.clone(), worded(&fault)));
        }
        Ok(())
    }

    /// Parse the text into `T`, then validate it with `T`'s default context.
    ///
    /// # Errors
    ///
    /// As [`Input::parse`] and [`Input::check`].
    #[cfg(feature = "validate")]
    pub fn load<T>(&self) -> Result<T, InputError>
    where
        T: DeserializeOwned + Validate,
        T::Context: Default,
    {
        let value = self.parse::<T>()?;
        self.check(&value, &T::Context::default())?;
        Ok(value)
    }
}

/// The words of `fault` from the process's [`Messages`], with each control
/// character escaped so a name or a value the text wrote cannot move a
/// terminal that prints the problem.
fn worded(fault: &Fault) -> String {
    messages::printable(&messages().message(fault))
}