use crate::deserialize_key::DeserializeKey;
use crate::error::{DeserializeError, SerializeError};
#[cfg(feature = "introspection")]
use crate::introspection::{
BuiltInType, Introspectable, KeyType, KeyTypeOf, Layout, LexicalId, References,
};
use crate::serialize_key::SerializeKey;
use crate::value_deserializer::{Deserialize, Deserializer};
use crate::value_serializer::{AsSerializeArg, Serialize, Serializer};
use std::fmt;
use std::str::FromStr;
use uuid::{Error as UuidError, Uuid};
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "kebab-case")
)]
pub struct ObjectId {
pub uuid: ObjectUuid,
pub cookie: ObjectCookie,
}
impl ObjectId {
pub const NIL: Self = Self::new(ObjectUuid::NIL, ObjectCookie::NIL);
pub const fn new(uuid: ObjectUuid, cookie: ObjectCookie) -> Self {
Self { uuid, cookie }
}
pub const fn is_nil(self) -> bool {
self.uuid.is_nil() && self.cookie.is_nil()
}
}
impl Serialize for ObjectId {
fn serialize(&self, serializer: Serializer) -> Result<(), SerializeError> {
serializer.serialize_object_id(*self);
Ok(())
}
}
impl Deserialize for ObjectId {
fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
deserializer.deserialize_object_id()
}
}
impl AsSerializeArg for ObjectId {
type SerializeArg<'a> = Self;
fn as_serialize_arg<'a>(&'a self) -> Self::SerializeArg<'a>
where
Self: 'a,
{
*self
}
}
#[cfg(feature = "introspection")]
impl Introspectable for ObjectId {
fn layout() -> Layout {
BuiltInType::ObjectId.into()
}
fn lexical_id() -> LexicalId {
LexicalId::OBJECT_ID
}
fn add_references(_references: &mut References) {}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(transparent)
)]
#[repr(transparent)]
pub struct ObjectUuid(pub Uuid);
impl ObjectUuid {
pub const NIL: Self = Self(Uuid::nil());
#[cfg(feature = "new-v4-ids")]
pub fn new_v4() -> Self {
Self(Uuid::new_v4())
}
pub const fn is_nil(self) -> bool {
self.0.is_nil()
}
}
impl Serialize for ObjectUuid {
fn serialize(&self, serializer: Serializer) -> Result<(), SerializeError> {
serializer.serialize_uuid(self.0);
Ok(())
}
}
impl Deserialize for ObjectUuid {
fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
deserializer.deserialize_uuid().map(Self)
}
}
impl AsSerializeArg for ObjectUuid {
type SerializeArg<'a> = Self;
fn as_serialize_arg<'a>(&'a self) -> Self::SerializeArg<'a>
where
Self: 'a,
{
*self
}
}
#[cfg(feature = "introspection")]
impl Introspectable for ObjectUuid {
fn layout() -> Layout {
BuiltInType::Uuid.into()
}
fn lexical_id() -> LexicalId {
LexicalId::UUID
}
fn add_references(_references: &mut References) {}
}
impl SerializeKey for ObjectUuid {
type Impl<'a> = Uuid;
fn as_impl(&self) -> Self::Impl<'_> {
self.0
}
}
impl DeserializeKey for ObjectUuid {
type Impl = Uuid;
fn try_from_impl(key: Self::Impl) -> Result<Self, DeserializeError> {
Ok(Self(key))
}
}
#[cfg(feature = "introspection")]
impl KeyTypeOf for ObjectUuid {
const KEY_TYPE: KeyType = KeyType::Uuid;
}
impl From<Uuid> for ObjectUuid {
fn from(uuid: Uuid) -> Self {
Self(uuid)
}
}
impl From<ObjectUuid> for Uuid {
fn from(uuid: ObjectUuid) -> Self {
uuid.0
}
}
impl fmt::Display for ObjectUuid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl FromStr for ObjectUuid {
type Err = UuidError;
fn from_str(s: &str) -> Result<Self, UuidError> {
s.parse().map(Self)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(transparent)
)]
#[repr(transparent)]
pub struct ObjectCookie(pub Uuid);
impl ObjectCookie {
pub const NIL: Self = Self(Uuid::nil());
#[cfg(feature = "new-v4-ids")]
pub fn new_v4() -> Self {
Self(Uuid::new_v4())
}
pub const fn is_nil(self) -> bool {
self.0.is_nil()
}
}
impl Serialize for ObjectCookie {
fn serialize(&self, serializer: Serializer) -> Result<(), SerializeError> {
serializer.serialize_uuid(self.0);
Ok(())
}
}
impl Deserialize for ObjectCookie {
fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
deserializer.deserialize_uuid().map(Self)
}
}
impl AsSerializeArg for ObjectCookie {
type SerializeArg<'a> = Self;
fn as_serialize_arg<'a>(&'a self) -> Self::SerializeArg<'a>
where
Self: 'a,
{
*self
}
}
#[cfg(feature = "introspection")]
impl Introspectable for ObjectCookie {
fn layout() -> Layout {
BuiltInType::Uuid.into()
}
fn lexical_id() -> LexicalId {
LexicalId::UUID
}
fn add_references(_references: &mut References) {}
}
impl SerializeKey for ObjectCookie {
type Impl<'a> = Uuid;
fn as_impl(&self) -> Self::Impl<'_> {
self.0
}
}
impl DeserializeKey for ObjectCookie {
type Impl = Uuid;
fn try_from_impl(key: Self::Impl) -> Result<Self, DeserializeError> {
Ok(Self(key))
}
}
#[cfg(feature = "introspection")]
impl KeyTypeOf for ObjectCookie {
const KEY_TYPE: KeyType = KeyType::Uuid;
}
impl From<Uuid> for ObjectCookie {
fn from(cookie: Uuid) -> Self {
Self(cookie)
}
}
impl From<ObjectCookie> for Uuid {
fn from(cookie: ObjectCookie) -> Self {
cookie.0
}
}
impl fmt::Display for ObjectCookie {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "kebab-case")
)]
pub struct ServiceId {
pub object_id: ObjectId,
pub uuid: ServiceUuid,
pub cookie: ServiceCookie,
}
impl ServiceId {
pub const NIL: Self = Self::new(ObjectId::NIL, ServiceUuid::NIL, ServiceCookie::NIL);
pub const fn new(object_id: ObjectId, uuid: ServiceUuid, cookie: ServiceCookie) -> Self {
Self {
object_id,
uuid,
cookie,
}
}
pub const fn is_nil(self) -> bool {
self.object_id.is_nil() && self.uuid.is_nil() && self.cookie.is_nil()
}
}
impl Serialize for ServiceId {
fn serialize(&self, serializer: Serializer) -> Result<(), SerializeError> {
serializer.serialize_service_id(*self);
Ok(())
}
}
impl Deserialize for ServiceId {
fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
deserializer.deserialize_service_id()
}
}
impl AsSerializeArg for ServiceId {
type SerializeArg<'a> = Self;
fn as_serialize_arg<'a>(&'a self) -> Self::SerializeArg<'a>
where
Self: 'a,
{
*self
}
}
#[cfg(feature = "introspection")]
impl Introspectable for ServiceId {
fn layout() -> Layout {
BuiltInType::ServiceId.into()
}
fn lexical_id() -> LexicalId {
LexicalId::SERVICE_ID
}
fn add_references(_references: &mut References) {}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(transparent)
)]
#[repr(transparent)]
pub struct ServiceUuid(pub Uuid);
impl ServiceUuid {
pub const NIL: Self = Self(Uuid::nil());
#[cfg(feature = "new-v4-ids")]
pub fn new_v4() -> Self {
Self(Uuid::new_v4())
}
pub const fn is_nil(self) -> bool {
self.0.is_nil()
}
}
impl Serialize for ServiceUuid {
fn serialize(&self, serializer: Serializer) -> Result<(), SerializeError> {
serializer.serialize_uuid(self.0);
Ok(())
}
}
impl Deserialize for ServiceUuid {
fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
deserializer.deserialize_uuid().map(Self)
}
}
impl AsSerializeArg for ServiceUuid {
type SerializeArg<'a> = Self;
fn as_serialize_arg<'a>(&'a self) -> Self::SerializeArg<'a>
where
Self: 'a,
{
*self
}
}
#[cfg(feature = "introspection")]
impl Introspectable for ServiceUuid {
fn layout() -> Layout {
BuiltInType::Uuid.into()
}
fn lexical_id() -> LexicalId {
LexicalId::UUID
}
fn add_references(_references: &mut References) {}
}
impl SerializeKey for ServiceUuid {
type Impl<'a> = Uuid;
fn as_impl(&self) -> Self::Impl<'_> {
self.0
}
}
impl DeserializeKey for ServiceUuid {
type Impl = Uuid;
fn try_from_impl(key: Self::Impl) -> Result<Self, DeserializeError> {
Ok(Self(key))
}
}
#[cfg(feature = "introspection")]
impl KeyTypeOf for ServiceUuid {
const KEY_TYPE: KeyType = KeyType::Uuid;
}
impl From<Uuid> for ServiceUuid {
fn from(uuid: Uuid) -> Self {
Self(uuid)
}
}
impl From<ServiceUuid> for Uuid {
fn from(uuid: ServiceUuid) -> Self {
uuid.0
}
}
impl fmt::Display for ServiceUuid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl FromStr for ServiceUuid {
type Err = UuidError;
fn from_str(s: &str) -> Result<Self, UuidError> {
s.parse().map(Self)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(transparent)
)]
#[repr(transparent)]
pub struct ServiceCookie(pub Uuid);
impl ServiceCookie {
pub const NIL: Self = Self(Uuid::nil());
#[cfg(feature = "new-v4-ids")]
pub fn new_v4() -> Self {
Self(Uuid::new_v4())
}
pub const fn is_nil(self) -> bool {
self.0.is_nil()
}
}
impl Serialize for ServiceCookie {
fn serialize(&self, serializer: Serializer) -> Result<(), SerializeError> {
serializer.serialize_uuid(self.0);
Ok(())
}
}
impl Deserialize for ServiceCookie {
fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
deserializer.deserialize_uuid().map(Self)
}
}
impl AsSerializeArg for ServiceCookie {
type SerializeArg<'a> = Self;
fn as_serialize_arg<'a>(&'a self) -> Self::SerializeArg<'a>
where
Self: 'a,
{
*self
}
}
#[cfg(feature = "introspection")]
impl Introspectable for ServiceCookie {
fn layout() -> Layout {
BuiltInType::Uuid.into()
}
fn lexical_id() -> LexicalId {
LexicalId::UUID
}
fn add_references(_references: &mut References) {}
}
impl SerializeKey for ServiceCookie {
type Impl<'a> = Uuid;
fn as_impl(&self) -> Self::Impl<'_> {
self.0
}
}
impl DeserializeKey for ServiceCookie {
type Impl = Uuid;
fn try_from_impl(key: Self::Impl) -> Result<Self, DeserializeError> {
Ok(Self(key))
}
}
#[cfg(feature = "introspection")]
impl KeyTypeOf for ServiceCookie {
const KEY_TYPE: KeyType = KeyType::Uuid;
}
impl From<Uuid> for ServiceCookie {
fn from(cookie: Uuid) -> Self {
Self(cookie)
}
}
impl From<ServiceCookie> for Uuid {
fn from(cookie: ServiceCookie) -> Self {
cookie.0
}
}
impl fmt::Display for ServiceCookie {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(transparent)
)]
#[repr(transparent)]
pub struct ChannelCookie(pub Uuid);
impl ChannelCookie {
pub const NIL: Self = Self(Uuid::nil());
#[cfg(feature = "new-v4-ids")]
pub fn new_v4() -> Self {
Self(Uuid::new_v4())
}
pub const fn is_nil(self) -> bool {
self.0.is_nil()
}
}
impl Serialize for ChannelCookie {
fn serialize(&self, serializer: Serializer) -> Result<(), SerializeError> {
serializer.serialize_uuid(self.0);
Ok(())
}
}
impl Deserialize for ChannelCookie {
fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
deserializer.deserialize_uuid().map(Self)
}
}
impl AsSerializeArg for ChannelCookie {
type SerializeArg<'a> = Self;
fn as_serialize_arg<'a>(&'a self) -> Self::SerializeArg<'a>
where
Self: 'a,
{
*self
}
}
#[cfg(feature = "introspection")]
impl Introspectable for ChannelCookie {
fn layout() -> Layout {
BuiltInType::Uuid.into()
}
fn lexical_id() -> LexicalId {
LexicalId::UUID
}
fn add_references(_references: &mut References) {}
}
impl SerializeKey for ChannelCookie {
type Impl<'a> = Uuid;
fn as_impl(&self) -> Self::Impl<'_> {
self.0
}
}
impl DeserializeKey for ChannelCookie {
type Impl = Uuid;
fn try_from_impl(key: Self::Impl) -> Result<Self, DeserializeError> {
Ok(Self(key))
}
}
#[cfg(feature = "introspection")]
impl KeyTypeOf for ChannelCookie {
const KEY_TYPE: KeyType = KeyType::Uuid;
}
impl From<Uuid> for ChannelCookie {
fn from(cookie: Uuid) -> Self {
Self(cookie)
}
}
impl From<ChannelCookie> for Uuid {
fn from(cookie: ChannelCookie) -> Self {
cookie.0
}
}
impl fmt::Display for ChannelCookie {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(transparent)
)]
#[repr(transparent)]
pub struct BusListenerCookie(pub Uuid);
impl BusListenerCookie {
pub const NIL: Self = Self(Uuid::nil());
#[cfg(feature = "new-v4-ids")]
pub fn new_v4() -> Self {
Self(Uuid::new_v4())
}
pub const fn is_nil(self) -> bool {
self.0.is_nil()
}
}
impl From<Uuid> for BusListenerCookie {
fn from(cookie: Uuid) -> Self {
Self(cookie)
}
}
impl From<BusListenerCookie> for Uuid {
fn from(cookie: BusListenerCookie) -> Self {
cookie.0
}
}
impl fmt::Display for BusListenerCookie {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
#[cfg_attr(feature = "fuzzing", derive(arbitrary::Arbitrary))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(transparent)
)]
#[repr(transparent)]
pub struct TypeId(pub Uuid);
impl TypeId {
pub const NIL: Self = Self(Uuid::nil());
pub const fn is_nil(self) -> bool {
self.0.is_nil()
}
}
impl Serialize for TypeId {
fn serialize(&self, serializer: Serializer) -> Result<(), SerializeError> {
serializer.serialize_uuid(self.0);
Ok(())
}
}
impl Deserialize for TypeId {
fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
deserializer.deserialize_uuid().map(Self)
}
}
impl AsSerializeArg for TypeId {
type SerializeArg<'a> = Self;
fn as_serialize_arg<'a>(&'a self) -> Self::SerializeArg<'a>
where
Self: 'a,
{
*self
}
}
#[cfg(feature = "introspection")]
impl Introspectable for TypeId {
fn layout() -> Layout {
BuiltInType::Uuid.into()
}
fn lexical_id() -> LexicalId {
LexicalId::UUID
}
fn add_references(_references: &mut References) {}
}
impl SerializeKey for TypeId {
type Impl<'a> = Uuid;
fn as_impl(&self) -> Self::Impl<'_> {
self.0
}
}
impl DeserializeKey for TypeId {
type Impl = Uuid;
fn try_from_impl(key: Self::Impl) -> Result<Self, DeserializeError> {
Ok(Self(key))
}
}
#[cfg(feature = "introspection")]
impl KeyTypeOf for TypeId {
const KEY_TYPE: KeyType = KeyType::Uuid;
}
impl From<Uuid> for TypeId {
fn from(uuid: Uuid) -> Self {
Self(uuid)
}
}
impl From<TypeId> for Uuid {
fn from(id: TypeId) -> Self {
id.0
}
}
impl fmt::Display for TypeId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl FromStr for TypeId {
type Err = UuidError;
fn from_str(s: &str) -> Result<Self, UuidError> {
s.parse().map(Self)
}
}