zvariant/
complete_type.rs

1use core::fmt::{self, Debug, Display, Formatter};
2use serde::de::{Deserialize, Deserializer};
3use static_assertions::assert_impl_all;
4
5use crate::{Error, Result, Signature, Type};
6
7/// [`Signature`] that identifies a complete type.
8#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, Type)]
9pub struct CompleteType<'a>(Signature<'a>);
10
11assert_impl_all!(CompleteType<'_>: Send, Sync, Unpin);
12
13impl<'a> CompleteType<'a> {
14    /// Returns the underlying [`Signature`]
15    pub fn signature(&self) -> &Signature<'a> {
16        &self.0
17    }
18}
19
20impl<'a> Display for CompleteType<'a> {
21    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
22        std::fmt::Display::fmt(&self.0.as_str(), f)
23    }
24}
25
26impl<'a> TryFrom<Signature<'a>> for CompleteType<'a> {
27    type Error = Error;
28
29    fn try_from(sig: Signature<'a>) -> Result<Self> {
30        if sig.n_complete_types() != Ok(1) {
31            return Err(Error::IncorrectType);
32        }
33        Ok(Self(sig))
34    }
35}
36
37impl<'de: 'a, 'a> Deserialize<'de> for CompleteType<'a> {
38    fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
39    where
40        D: Deserializer<'de>,
41    {
42        let val = Signature::deserialize(deserializer)?;
43
44        Self::try_from(val).map_err(serde::de::Error::custom)
45    }
46}