use super::{Error, Field, OpaqueOpen, ReadContext, WriteContext, admit_scope};
use core::cell::Cell;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_bytes::Bytes;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Recoverable<'a>(Option<&'a [u8]>);
impl<'a> Recoverable<'a> {
#[must_use]
pub const fn absent() -> Self {
Self(None)
}
pub fn write<T: Serialize + ?Sized>(
context: &WriteContext<'_>,
value: &T,
scratch: &'a mut [u8],
) -> Result<Self, Error> {
let body = context.child();
let size = body.write_once(value, scratch)?;
Ok(Self(Some(&scratch[..size])))
}
pub fn project<T: Deserialize<'a>, R>(
self,
context: &ReadContext<'_>,
project: impl FnOnce(T, &RecoveryContext<'_>) -> Result<R, Error>,
) -> Result<Option<R>, Error> {
let branch = context.child();
admit_scope(branch.budget, branch.depth)?;
let Some(bytes) = self.0 else {
return Ok(None);
};
let field = Field {
bytes,
context: branch,
};
field.project(|value, read| {
let incompatible = Cell::new(false);
let scope = RecoveryContext {
read,
incompatible: &incompatible,
};
let projected = project(value, &scope)?;
Ok((!incompatible.get()).then_some(projected))
})
}
}
impl Serialize for Recoverable<'_> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.0.map(Bytes::new).serialize(serializer)
}
}
impl<'de: 'a, 'a> Deserialize<'de> for Recoverable<'a> {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Option::<&Bytes>::deserialize(deserializer).map(|bytes| Self(bytes.map(AsRef::as_ref)))
}
}
pub struct RecoveryContext<'a> {
read: &'a ReadContext<'a>,
incompatible: &'a Cell<bool>,
}
impl RecoveryContext<'_> {
pub fn check<R>(
context: &ReadContext<'_>,
project: impl FnOnce(&RecoveryContext<'_>) -> Result<R, Error>,
) -> Result<R, Error> {
let incompatible = Cell::new(false);
let scope = RecoveryContext {
read: context,
incompatible: &incompatible,
};
let value = project(&scope)?;
context.budget.check()?;
if incompatible.get() {
return Err(Error::Incompatible);
}
Ok(value)
}
#[must_use]
pub const fn read_context(&self) -> &ReadContext<'_> {
self.read
}
pub fn nested<R>(
&self,
project: impl FnOnce(&RecoveryContext<'_>) -> Result<R, Error>,
) -> Result<R, Error> {
self.read.nested(|read| {
project(&RecoveryContext {
read,
incompatible: self.incompatible,
})
})
}
pub fn project_field<'de, T: Deserialize<'de>, R>(
&self,
field: Field<'_, 'de>,
project: impl FnOnce(T, &RecoveryContext<'_>) -> Result<R, Error>,
) -> Result<R, Error> {
if !core::ptr::eq(self.read.budget, field.context.budget) {
return Err(Error::Malformed);
}
field.project(|value, read| {
project(
value,
&RecoveryContext {
read,
incompatible: self.incompatible,
},
)
})
}
pub fn missing_required(&self) {
self.incompatible.set(true);
}
pub fn required_open<'de, R>(
&self,
open: OpaqueOpen<'de>,
visit: impl FnOnce(u32, Field<'_, 'de>) -> Result<Option<R>, Error>,
) -> Result<Option<R>, Error> {
let value = open.visit(self.read, visit)?.ok_or(Error::Malformed)?;
if value.is_none() {
self.incompatible.set(true);
}
Ok(value)
}
}