Skip to main content

ephemeris_documents/yaml/source/read/
fit.rs

1//! What the reader returns for a piece of the source.
2
3use super::Entry;
4use crate::error::{DocumentError, YamlError};
5use crate::path::KeyPath;
6
7/// A parsed value and where its text ends.
8pub(super) struct Parsed<T> {
9    pub(super) value: T,
10    pub(super) end: usize,
11}
12
13impl<T> Parsed<T> {
14    pub(super) fn map<U>(self, convert: impl FnOnce(T) -> U) -> Parsed<U> {
15        Parsed {
16            value: convert(self.value),
17            end: self.end,
18        }
19    }
20}
21
22/// The entries of one mapping, in source order and with unique keys.
23#[derive(Default)]
24pub(super) struct Collected(Vec<Entry>);
25
26impl Collected {
27    /// An error when an entry with `key` is already there.
28    pub(super) fn reject_duplicate(&self, key: &str) -> Result<(), DocumentError> {
29        if self.0.iter().any(|seen| seen.key() == key) {
30            return Err(YamlError::DuplicateKey {
31                path: KeyPath::root(),
32                key: key.to_owned(),
33            }
34            .into());
35        }
36        Ok(())
37    }
38
39    pub(super) fn push(&mut self, entry: Entry) {
40        self.0.push(entry);
41    }
42
43    pub(super) fn into_vec(self) -> Vec<Entry> {
44        self.0
45    }
46}