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
macro_rules! common_impls {
    ($id:ident, $try_from:ident, $desc:literal) => {
        impl ::std::convert::From<$id<Box<str>>> for ::std::string::String {
            fn from(id: $id<Box<str>>) -> Self {
                id.full_id.into()
            }
        }

        impl<'a> ::std::convert::TryFrom<&'a str> for $id<&'a str> {
            type Error = crate::error::Error;

            fn try_from(s: &'a str) -> Result<Self, Self::Error> {
                $try_from(s)
            }
        }

        impl ::std::convert::TryFrom<&str> for $id<Box<str>> {
            type Error = crate::error::Error;

            fn try_from(s: &str) -> Result<Self, Self::Error> {
                $try_from(s)
            }
        }

        impl ::std::convert::TryFrom<String> for $id<Box<str>> {
            type Error = crate::error::Error;

            fn try_from(s: String) -> Result<Self, Self::Error> {
                $try_from(s)
            }
        }

        impl<T: ::std::convert::AsRef<str>> ::std::convert::AsRef<str> for $id<T> {
            fn as_ref(&self) -> &str {
                self.full_id.as_ref()
            }
        }

        impl<T: ::std::fmt::Display> ::std::fmt::Display for $id<T> {
            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
                write!(f, "{}", self.full_id)
            }
        }

        impl<T: ::std::cmp::PartialEq> ::std::cmp::PartialEq for $id<T> {
            fn eq(&self, other: &Self) -> bool {
                self.full_id == other.full_id
            }
        }

        impl<T: ::std::cmp::Eq> ::std::cmp::Eq for $id<T> {}

        impl<T: ::std::cmp::PartialOrd> ::std::cmp::PartialOrd for $id<T> {
            fn partial_cmp(&self, other: &Self) -> Option<::std::cmp::Ordering> {
                ::std::cmp::PartialOrd::partial_cmp(&self.full_id, &other.full_id)
            }
        }

        impl<T: ::std::cmp::Ord> ::std::cmp::Ord for $id<T> {
            fn cmp(&self, other: &Self) -> ::std::cmp::Ordering {
                ::std::cmp::Ord::cmp(&self.full_id, &other.full_id)
            }
        }

        impl<T: ::std::hash::Hash> ::std::hash::Hash for $id<T> {
            fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
                self.full_id.hash(state);
            }
        }

        #[cfg(feature = "serde")]
        impl<T: AsRef<str>> ::serde::Serialize for $id<T> {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: ::serde::Serializer,
            {
                serializer.serialize_str(self.full_id.as_ref())
            }
        }

        #[cfg(feature = "serde")]
        impl<'de> ::serde::Deserialize<'de> for $id<Box<str>> {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: ::serde::Deserializer<'de>,
            {
                crate::deserialize_id(deserializer, $desc)
            }
        }

        impl<T: AsRef<str>> ::std::cmp::PartialEq<&str> for $id<T> {
            fn eq(&self, other: &&str) -> bool {
                self.full_id.as_ref() == *other
            }
        }

        impl<T: AsRef<str>> ::std::cmp::PartialEq<$id<T>> for &str {
            fn eq(&self, other: &$id<T>) -> bool {
                *self == other.full_id.as_ref()
            }
        }

        impl<T: AsRef<str>> ::std::cmp::PartialEq<::std::string::String> for $id<T> {
            fn eq(&self, other: &::std::string::String) -> bool {
                self.full_id.as_ref() == &other[..]
            }
        }

        impl<T: AsRef<str>> ::std::cmp::PartialEq<$id<T>> for ::std::string::String {
            fn eq(&self, other: &$id<T>) -> bool {
                &self[..] == other.full_id.as_ref()
            }
        }
    };
}