use std::fmt;
use crate::json::quote;
#[derive(Clone, Default, PartialEq, Eq, Debug)]
#[must_use]
pub struct Place(Vec<Step>);
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Step {
Field(String),
Element(usize),
Key(String),
Constructor(String),
}
impl Place {
pub(crate) fn field(&mut self, name: &str) {
self.0.push(Step::Field(name.to_owned()));
}
pub(crate) fn constructor(&mut self, name: &str) {
self.0.push(Step::Constructor(name.to_owned()));
}
pub(crate) fn index(&mut self, index: usize) {
self.0.push(Step::Element(index));
}
pub(crate) fn key(&mut self, key: &str) {
self.0.push(Step::Key(key.to_owned()));
}
pub(crate) fn push(&mut self, step: Step) {
self.0.push(step);
}
pub(crate) fn pop(&mut self) {
self.0.pop();
}
pub fn within(mut self, outer: impl IntoIterator<Item = Step>) -> Place {
let mut steps: Vec<Step> = outer.into_iter().collect();
steps.append(&mut self.0);
Place(steps)
}
pub fn steps(&self) -> &[Step] {
&self.0
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl fmt::Display for Place {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (at, step) in self.0.iter().enumerate() {
match step {
Step::Field(name) | Step::Constructor(name) if at == 0 => f.write_str(name)?,
Step::Field(name) | Step::Constructor(name) => write!(f, ".{name}")?,
Step::Element(index) => write!(f, "[{index}]")?,
Step::Key(key) => write!(f, "[{}]", quote(key))?,
}
}
Ok(())
}
}
impl PartialEq<&str> for Place {
fn eq(&self, text: &&str) -> bool {
struct Rest<'a>(&'a str);
impl fmt::Write for Rest<'_> {
fn write_str(&mut self, piece: &str) -> fmt::Result {
self.0 = self.0.strip_prefix(piece).ok_or(fmt::Error)?;
Ok(())
}
}
let mut rest = Rest(text);
fmt::write(&mut rest, format_args!("{self}")).is_ok() && rest.0.is_empty()
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct DecodeError {
pub path: Place,
pub problem: String,
pub kind: DecodeErrorKind,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum DecodeErrorKind {
Syntax {
at: usize,
},
Missing,
Unknown,
Duplicate,
Mismatch,
TooLarge,
}
impl DecodeError {
pub(crate) fn of(path: &Place, kind: DecodeErrorKind, problem: String) -> Self {
DecodeError {
path: path.clone(),
problem,
kind,
}
}
}
impl DecodeError {
pub fn for_subject(&self, subject: &str) -> String {
if self.path.is_empty() {
format!("{subject} {}", self.problem)
} else {
format!("`{}` in {subject} {}", self.path, self.problem)
}
}
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.path.is_empty() {
write!(f, "the value {}", self.problem)
} else {
write!(f, "`{}` {}", self.path, self.problem)
}
}
}
impl std::error::Error for DecodeError {}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct EncodeError {
pub path: Place,
pub problem: String,
pub kind: EncodeErrorKind,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum EncodeErrorKind {
Mismatch,
NoEncoding,
TooLarge,
Invalid,
}
impl EncodeError {
pub(crate) fn new(path: &Place, kind: EncodeErrorKind, problem: impl Into<String>) -> Self {
EncodeError {
path: path.clone(),
problem: problem.into(),
kind,
}
}
}
impl From<DecodeError> for EncodeError {
fn from(error: DecodeError) -> Self {
EncodeError {
path: error.path,
problem: error.problem,
kind: EncodeErrorKind::Invalid,
}
}
}
impl fmt::Display for EncodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.path.is_empty() {
write!(f, "the value {}", self.problem)
} else {
write!(f, "`{}` {}", self.path, self.problem)
}
}
}
impl std::error::Error for EncodeError {}