use super::steps::Steps;
use super::unexpected;
use crate::codes;
use crate::decoder::Decoder;
use crate::issue::{Issue, Issues};
use crate::path::Path;
use serde_json::Value;
#[derive(Clone, Debug, Default)]
pub struct BoolDecoder {
steps: Steps<bool>,
}
pub fn bool() -> BoolDecoder {
BoolDecoder::default()
}
impl Decoder<Value> for BoolDecoder {
type Output = bool;
fn decode_at(&self, input: &Value, path: &Path<'_>) -> Result<bool, Issues> {
match input {
Value::Bool(b) => self.steps.run(*b, path),
other => Err(self.steps.base_issue(unexpected(path, "boolean", other))),
}
}
}
impl BoolDecoder {
fn exactly(mut self, expected: bool) -> Self {
self.steps.require(
move |b| *b == expected,
move |b| {
Issue::new(codes::INVALID_VALUE)
.with_meta("expected", expected)
.with_meta("actual", *b)
},
);
self
}
pub fn message(mut self, message: impl Into<String>) -> Self {
self.steps.set_message(message.into());
self
}
pub fn is_true(self) -> Self {
self.exactly(true)
}
pub fn is_false(self) -> Self {
self.exactly(false)
}
}