zbus_names/
property_name.rsuse crate::{
utils::{impl_str_basic, impl_try_from},
Error, Result,
};
use serde::{de, Deserialize, Serialize};
use static_assertions::assert_impl_all;
use std::{
borrow::{Borrow, Cow},
fmt::{self, Debug, Display, Formatter},
ops::Deref,
sync::Arc,
};
use zvariant::{NoneValue, OwnedValue, Str, Type, Value};
#[derive(
Clone, Debug, Hash, PartialEq, Eq, Serialize, Type, Value, PartialOrd, Ord, OwnedValue,
)]
pub struct PropertyName<'name>(Str<'name>);
assert_impl_all!(PropertyName<'_>: Send, Sync, Unpin);
impl_str_basic!(PropertyName<'_>);
impl<'name> PropertyName<'name> {
pub fn as_ref(&self) -> PropertyName<'_> {
PropertyName(self.0.as_ref())
}
pub fn as_str(&self) -> &str {
self.0.as_str()
}
pub fn from_str_unchecked(name: &'name str) -> Self {
Self(Str::from(name))
}
pub fn from_static_str(name: &'static str) -> Result<Self> {
ensure_correct_property_name(name)?;
Ok(Self(Str::from_static(name)))
}
pub const fn from_static_str_unchecked(name: &'static str) -> Self {
Self(Str::from_static(name))
}
pub fn from_string_unchecked(name: String) -> Self {
Self(Str::from(name))
}
pub fn to_owned(&self) -> PropertyName<'static> {
PropertyName(self.0.to_owned())
}
pub fn into_owned(self) -> PropertyName<'static> {
PropertyName(self.0.into_owned())
}
}
impl Deref for PropertyName<'_> {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
impl Borrow<str> for PropertyName<'_> {
fn borrow(&self) -> &str {
self.as_str()
}
}
impl Display for PropertyName<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(&self.as_str(), f)
}
}
impl PartialEq<str> for PropertyName<'_> {
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}
}
impl PartialEq<&str> for PropertyName<'_> {
fn eq(&self, other: &&str) -> bool {
self.as_str() == *other
}
}
impl PartialEq<OwnedPropertyName> for PropertyName<'_> {
fn eq(&self, other: &OwnedPropertyName) -> bool {
*self == other.0
}
}
impl<'de: 'name, 'name> Deserialize<'de> for PropertyName<'name> {
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let name = <Cow<'name, str>>::deserialize(deserializer)?;
Self::try_from(name).map_err(|e| de::Error::custom(e.to_string()))
}
}
impl<'name> From<PropertyName<'name>> for Str<'name> {
fn from(value: PropertyName<'name>) -> Self {
value.0
}
}
impl_try_from! {
ty: PropertyName<'s>,
owned_ty: OwnedPropertyName,
validate_fn: ensure_correct_property_name,
try_from: [&'s str, String, Arc<str>, Cow<'s, str>, Str<'s>],
}
fn ensure_correct_property_name(name: &str) -> Result<()> {
if name.is_empty() {
return Err(Error::InvalidPropertyName(format!(
"`{}` is {} characters long, which is smaller than minimum allowed (1)",
name,
name.len(),
)));
} else if name.len() > 255 {
return Err(Error::InvalidPropertyName(format!(
"`{}` is {} characters long, which is longer than maximum allowed (255)",
name,
name.len(),
)));
}
Ok(())
}
impl TryFrom<()> for PropertyName<'_> {
type Error = Error;
fn try_from(_value: ()) -> Result<Self> {
unreachable!("Conversion from `()` is not meant to actually work");
}
}
impl<'name> From<&PropertyName<'name>> for PropertyName<'name> {
fn from(name: &PropertyName<'name>) -> Self {
name.clone()
}
}
impl<'name> NoneValue for PropertyName<'name> {
type NoneType = &'name str;
fn null_value() -> Self::NoneType {
<&str>::default()
}
}
#[derive(Clone, Hash, PartialEq, Eq, Serialize, Type, Value, PartialOrd, Ord, OwnedValue)]
pub struct OwnedPropertyName(#[serde(borrow)] PropertyName<'static>);
assert_impl_all!(OwnedPropertyName: Send, Sync, Unpin);
impl_str_basic!(OwnedPropertyName);
impl OwnedPropertyName {
pub fn into_inner(self) -> PropertyName<'static> {
self.0
}
pub fn inner(&self) -> &PropertyName<'static> {
&self.0
}
}
impl Deref for OwnedPropertyName {
type Target = PropertyName<'static>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Borrow<str> for OwnedPropertyName {
fn borrow(&self) -> &str {
self.0.as_str()
}
}
impl From<OwnedPropertyName> for PropertyName<'_> {
fn from(o: OwnedPropertyName) -> Self {
o.into_inner()
}
}
impl<'unowned, 'owned: 'unowned> From<&'owned OwnedPropertyName> for PropertyName<'unowned> {
fn from(name: &'owned OwnedPropertyName) -> Self {
PropertyName::from_str_unchecked(name.as_str())
}
}
impl From<PropertyName<'_>> for OwnedPropertyName {
fn from(name: PropertyName<'_>) -> Self {
OwnedPropertyName(name.into_owned())
}
}
impl From<OwnedPropertyName> for Str<'_> {
fn from(value: OwnedPropertyName) -> Self {
value.into_inner().0
}
}
impl<'de> Deserialize<'de> for OwnedPropertyName {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
String::deserialize(deserializer)
.and_then(|n| PropertyName::try_from(n).map_err(|e| de::Error::custom(e.to_string())))
.map(Self)
}
}
impl PartialEq<&str> for OwnedPropertyName {
fn eq(&self, other: &&str) -> bool {
self.as_str() == *other
}
}
impl PartialEq<PropertyName<'_>> for OwnedPropertyName {
fn eq(&self, other: &PropertyName<'_>) -> bool {
self.0 == *other
}
}
impl Debug for OwnedPropertyName {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_tuple("OwnedPropertyName")
.field(&self.as_str())
.finish()
}
}
impl Display for OwnedPropertyName {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(&PropertyName::from(self), f)
}
}
impl NoneValue for OwnedPropertyName {
type NoneType = <PropertyName<'static> as NoneValue>::NoneType;
fn null_value() -> Self::NoneType {
PropertyName::null_value()
}
}