#![cfg_attr(not(feature = "std"), no_std)]
#![forbid(unsafe_code)]
#![warn(missing_docs)]
#[cfg(not(feature = "alloc"))]
compile_error!("serde_field_result requires either the `std` feature or the `alloc` feature");
extern crate alloc;
macro_rules! impl_common_field_methods {
(
map_return: $map_return:ty;
map_missing: $map_missing:path;
map_valid: $map_valid:path;
invalid_ignore: $invalid_ignore:pat,
invalid_error: $invalid_error:pat => $error:expr;
invalid_map: $invalid_map:pat => $mapped_invalid:expr;
$(into_result_extra: $into_result_extra:literal;)?
) => {
#[must_use]
pub const fn as_ref(&self) -> Option<&T> {
match self {
Self::Valid(value) => Some(value),
Self::Missing => None,
$invalid_ignore => None,
}
}
#[must_use]
pub fn as_mut(&mut self) -> Option<&mut T> {
match self {
Self::Valid(value) => Some(value),
Self::Missing => None,
$invalid_ignore => None,
}
}
#[must_use]
pub fn into_option(self) -> Option<T> {
match self {
Self::Valid(value) => Some(value),
Self::Missing => None,
$invalid_ignore => None,
}
}
$(#[doc = $into_result_extra])?
#[must_use = "inspect the result to distinguish missing, valid, and invalid fields"]
pub fn into_result(self) -> Result<Option<T>, FieldError> {
match self {
Self::Missing => Ok(None),
Self::Valid(value) => Ok(Some(value)),
$invalid_error => Err($error),
}
}
#[must_use = "inspect the result to distinguish missing, valid, and invalid fields"]
pub const fn as_result(&self) -> Result<Option<&T>, &FieldError> {
match self {
Self::Missing => Ok(None),
Self::Valid(value) => Ok(Some(value)),
$invalid_error => Err($error),
}
}
#[must_use]
pub const fn error(&self) -> Option<&FieldError> {
match self {
$invalid_error => Some($error),
Self::Missing | Self::Valid(_) => None,
}
}
#[must_use]
pub const fn is_valid(&self) -> bool {
matches!(self, Self::Valid(_))
}
#[must_use]
pub const fn is_missing(&self) -> bool {
matches!(self, Self::Missing)
}
#[must_use]
pub const fn is_invalid(&self) -> bool {
matches!(self, $invalid_ignore)
}
#[must_use]
pub fn map<U>(self, map: impl FnOnce(T) -> U) -> $map_return {
match self {
Self::Missing => $map_missing,
Self::Valid(value) => $map_valid(map(value)),
$invalid_map => $mapped_invalid,
}
}
};
}
mod field;
pub use field::{
Field, FieldDecode, FieldError, ScalarFieldDecode, drain_map, drain_seq, invalid_map,
invalid_seq,
};
#[cfg(feature = "json")]
mod json;
#[cfg(feature = "json")]
pub use json::{BorrowedJsonField, JsonField};