1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use serde::ser::{Serialize, Serializer};
use std::convert::TryFrom;

use crate::Error;
use crate::{Signature, Type, Value};

/// A helper type to wrap Option<T> (GVariant's Maybe type) in [`Value`].
///
/// API is provided to convert from, and to Option<T>.
///
/// [`Value`]: enum.Value.html
#[derive(Debug, Clone, PartialEq)]
pub struct Maybe<'a> {
    value: Box<Option<Value<'a>>>,
    value_signature: Signature<'a>,
    signature: Signature<'static>,
}

impl<'a> Maybe<'a> {
    /// Get a reference to underlying value.
    pub fn inner(&self) -> &Option<Value<'a>> {
        &self.value
    }

    /// Create a new Just (Some) `Maybe`.
    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)),
        }
    }

    /// Create a new Nothing (None) `Maybe`, given the signature of the type.
    pub fn nothing<'s: 'a>(value_signature: Signature<'s>) -> Self {
        let signature = create_signature(&value_signature);
        Self {
            value_signature,
            signature,
            value: Box::new(None),
        }
    }

    /// Get the inner value as a concrete type
    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()
    }

    /// Get the signature of `Maybe`.
    pub fn signature(&self) -> Signature<'static> {
        self.signature.clone()
    }

    /// Get the signature of the potential value in the `Maybe`.
    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())),
            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()))
    }
}

// This would be great but somehow it conflicts with some blanket generic implementations from
// core:
//
// impl<'a, T> TryFrom<Maybe<'a>> for Option<T>

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))
}