#![cfg_attr(
not(feature = "yaml"),
doc = "",
doc = "[`YAML`]: crate#cargo-features"
)]
#![cfg_attr(
not(feature = "json"),
doc = "",
doc = "[`JSON`]: crate#cargo-features"
)]
#![cfg_attr(
not(feature = "toml"),
doc = "",
doc = "[`TOML`]: crate#cargo-features"
)]
use std::borrow::Cow;
use std::error::Error;
use std::fmt;
use std::ops::Range;
use super::Limits;
#[cfg(feature = "json")]
pub use super::json_format::{JSON, Json};
use super::messages::{EnglishMessages, Messages, messages};
#[cfg(feature = "toml")]
pub use super::toml_format::{TOML, Toml};
use super::tree::{Node, Scalar, Tag};
#[cfg(feature = "yaml")]
pub use super::yaml_format::{YAML, Yaml};
pub trait Format: Sync {
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>;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Reading {
Strict,
Listing,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Wanted {
Any,
Bool,
Int,
Float,
Str,
Null,
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Resolved<'a> {
Null,
Bool(bool),
Int(i128),
Uint(u128),
Float(f64),
Str(Cow<'a, str>),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Mismatch {
wanted: Wanted,
detail: Option<String>,
}
impl Mismatch {
#[must_use]
pub fn new(wanted: Wanted) -> Self {
Self {
wanted,
detail: None,
}
}
#[must_use]
pub fn with_detail(mut self, detail: impl Into<String>) -> Self {
self.detail = Some(detail.into());
self
}
#[must_use]
pub fn wanted(&self) -> Wanted {
self.wanted
}
#[must_use]
pub fn detail(&self) -> Option<&str> {
self.detail.as_deref()
}
}
impl fmt::Display for Mismatch {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(match self.wanted {
Wanted::Any => "expected a value",
Wanted::Bool => "expected true or false",
Wanted::Int => "expected an integer",
Wanted::Float => "expected a float",
Wanted::Str => "expected text",
Wanted::Null => "expected null",
})
}
}
impl Error for Mismatch {}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Refusal {
fault: Fault,
span: Option<Range<usize>>,
defined_at: Option<Range<usize>>,
}
impl Refusal {
#[must_use]
pub fn new(fault: Fault, span: Option<Range<usize>>, defined_at: Option<Range<usize>>) -> Self {
Self {
fault,
span,
defined_at,
}
}
#[must_use]
pub fn fault(&self) -> &Fault {
&self.fault
}
#[must_use]
pub fn span(&self) -> Option<Range<usize>> {
self.span.clone()
}
#[must_use]
pub fn defined_at(&self) -> Option<Range<usize>> {
self.defined_at.clone()
}
}
impl fmt::Display for Refusal {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&messages().message(&self.fault))
}
}
impl Error for Refusal {}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Fault {
Syntax {
detail: String,
},
DuplicateKey {
key: String,
},
MergeKey,
Limit {
name: &'static str,
value: String,
raise: Raise,
},
TooLarge {
bytes: u64,
limit: u64,
raise: Raise,
},
ExpectedKind {
expected: String,
found: String,
},
InvalidValue {
detail: String,
},
UnknownField {
field: String,
expected: Vec<&'static str>,
},
UnknownVariant {
variant: String,
expected: Vec<&'static str>,
},
MissingField {
field: &'static str,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Raise {
#[cfg(feature = "yaml")]
InRepository,
#[cfg(feature = "yaml")]
InProcess,
#[cfg(not(feature = "yaml"))]
InTool,
}
impl Fault {
#[must_use]
#[expect(
clippy::needless_pass_by_value,
reason = "a caller hands over the number or text it has, as it would to Into"
)]
pub fn limit(name: &'static str, value: impl ToString) -> Self {
Self::Limit {
name,
value: value.to_string(),
raise: Raise::FOR_TEXT,
}
}
}
impl Raise {
#[cfg(feature = "yaml")]
pub(crate) const FOR_TEXT: Self = Self::InRepository;
#[cfg(not(feature = "yaml"))]
pub(crate) const FOR_TEXT: Self = Self::InTool;
}
impl fmt::Display for Fault {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&EnglishMessages.message(self))
}
}
#[cfg(test)]
mod tests {
use super::{Mismatch, Wanted};
#[test]
fn a_mismatch_says_what_was_expected() {
let expected = [
(Wanted::Any, "expected a value"),
(Wanted::Bool, "expected true or false"),
(Wanted::Int, "expected an integer"),
(Wanted::Float, "expected a float"),
(Wanted::Str, "expected text"),
(Wanted::Null, "expected null"),
];
for (wanted, message) in expected {
assert_eq!(Mismatch::new(wanted).to_string(), message);
}
}
}