1 | use core::fmt::{self, Debug, Display, Formatter}; |
2 | use serde::de::{Deserialize, Deserializer}; |
3 | use static_assertions::assert_impl_all; |
4 | |
5 | use crate::{Error, Result, Signature, Type}; |
6 | |
7 | /// [`Signature`] that identifies a complete type. |
8 | #[derive (Debug, Clone, PartialEq, Eq, serde::Serialize, Type)] |
9 | pub struct CompleteType<'a>(Signature<'a>); |
10 | |
11 | assert_impl_all!(CompleteType<'_>: Send, Sync, Unpin); |
12 | |
13 | impl<'a> CompleteType<'a> { |
14 | /// Returns the underlying [`Signature`] |
15 | pub fn signature(&self) -> &Signature<'a> { |
16 | &self.0 |
17 | } |
18 | } |
19 | |
20 | impl<'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 | |
26 | impl<'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 | |
37 | impl<'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<'_> = Signature::deserialize(deserializer)?; |
43 | |
44 | Self::try_from(val).map_err(op:serde::de::Error::custom) |
45 | } |
46 | } |
47 | |