use serde::ser::{Serialize, Serializer};
use static_assertions::assert_impl_all;
use std::convert::TryFrom;
use crate::{Error, Signature, Type, Value};
#[derive(Debug, Clone, PartialEq)]
pub struct Maybe<'a> {
value: Box<Option<Value<'a>>>,
value_signature: Signature<'a>,
signature: Signature<'a>,
}
assert_impl_all!(Maybe<'_>: Send, Sync, Unpin);
impl<'a> Maybe<'a> {
pub fn inner(&self) -> &Option<Value<'a>> {
&self.value
}
pub fn just(value: Value<'a>) -> Self {
let value_signature = value.value_signature().to_owned();
let signature = create_signature(&value_signature);
Self {
value_signature,
signature,
value: Box::new(Some(value)),
}
}
pub(crate) fn just_full_signature<'v: 'a, 's: 'a>(
value: Value<'v>,
signature: Signature<'s>,
) -> Self {
Self {
value_signature: signature.slice(1..),
signature,
value: Box::new(Some(value)),
}
}
pub fn nothing<'s: 'a>(value_signature: Signature<'s>) -> Self {
let signature = create_signature(&value_signature);
Self {
value_signature,
signature,
value: Box::new(None),
}
}
pub(crate) fn nothing_full_signature<'s: 'a>(signature: Signature<'s>) -> Self {
Self {
value_signature: signature.slice(1..),
signature,
value: Box::new(None),
}
}
pub fn get<T>(self) -> core::result::Result<Option<T>, Error>
where
T: TryFrom<Value<'a>>,
{
self.value
.map(|v| v.downcast().ok_or(Error::IncorrectType))
.transpose()
}
pub fn signature(&self) -> Signature<'static> {
self.signature.to_owned()
}
pub fn full_signature(&self) -> &Signature<'_> {
&self.signature
}
pub fn value_signature(&self) -> &Signature<'_> {
&self.value_signature
}
pub(crate) fn to_owned(&self) -> Maybe<'static> {
Maybe {
value_signature: self.value_signature.to_owned(),
value: Box::new(self.value.clone().map(|v| v.to_owned().into())),
signature: self.signature.to_owned(),
}
}
}
impl<'a, T> From<Option<T>> for Maybe<'a>
where
T: Type + Into<Value<'a>>,
{
fn from(value: Option<T>) -> Self {
value
.map(|v| Self::just(Value::new(v)))
.unwrap_or_else(|| Self::nothing(T::signature()))
}
}
impl<'a, T> From<&Option<T>> for Maybe<'a>
where
T: Type + Into<Value<'a>> + Clone,
{
fn from(value: &Option<T>) -> Self {
value
.as_ref()
.map(|v| Self::just(Value::new(v.clone())))
.unwrap_or_else(|| Self::nothing(T::signature()))
}
}
impl<'a> Serialize for Maybe<'a> {
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
match &*self.value {
Some(value) => value.serialize_value_as_some(serializer),
None => serializer.serialize_none(),
}
}
}
fn create_signature(value_signature: &Signature<'_>) -> Signature<'static> {
Signature::from_string_unchecked(format!("m{}", value_signature))
}