use std::path::Path; use std::str::FromStr; use serde::{Deserialize, Deserializer, Serialize}; use crate::{Id, InvalidId}; #[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)] pub struct InterfaceId(Id); impl FromStr for InterfaceId { type Err = InvalidId; fn from_str(s: &str) -> Result { Ok(Self(Id::try_from(s.to_owned())?)) } } impl From for InterfaceId { fn from(id: Id) -> Self { Self(id) } } impl std::fmt::Display for InterfaceId { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", &self.0) } } impl std::ops::Deref for InterfaceId { type Target = str; fn deref(&self) -> &Self::Target { &*self.0 } } impl AsRef for InterfaceId { fn as_ref(&self) -> &str { self.0.as_ref() } } impl<'de> Deserialize<'de> for InterfaceId { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { Ok(InterfaceId(Deserialize::deserialize(deserializer)?)) } } impl AsRef for InterfaceId { fn as_ref(&self) -> &Path { self.0.as_ref().as_ref() } } impl<'q> sqlx::Encode<'q, sqlx::Postgres> for InterfaceId { fn encode_by_ref( &self, buf: &mut >::ArgumentBuffer, ) -> sqlx::encode::IsNull { <&str as sqlx::Encode<'q, sqlx::Postgres>>::encode_by_ref(&&**self, buf) } } impl sqlx::Type for InterfaceId { fn type_info() -> sqlx::postgres::PgTypeInfo { <&str as sqlx::Type>::type_info() } fn compatible(ty: &sqlx::postgres::PgTypeInfo) -> bool { <&str as sqlx::Type>::compatible(ty) } }