zbus_names/
property_name.rs

1use crate::{Error, Result, utils::define_name_type_impls};
2use serde::Serialize;
3use zvariant::{OwnedValue, Str, Type, Value};
4
5/// String that identifies a [property][pn] name on the bus.
6///
7/// # Examples
8///
9/// ```
10/// use zbus_names::PropertyName;
11///
12/// // Valid property names.
13/// let name = PropertyName::try_from("Property_for_you").unwrap();
14/// assert_eq!(name, "Property_for_you");
15/// let name = PropertyName::try_from("CamelCase101").unwrap();
16/// assert_eq!(name, "CamelCase101");
17/// let name = PropertyName::try_from("a_very_loooooooooooooooooo_ooooooo_0000o0ngName").unwrap();
18/// assert_eq!(name, "a_very_loooooooooooooooooo_ooooooo_0000o0ngName");
19/// let name = PropertyName::try_from("Property_for_you-1").unwrap();
20/// assert_eq!(name, "Property_for_you-1");
21/// ```
22///
23/// [pn]: https://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties
24#[derive(
25    Clone, Debug, Hash, PartialEq, Eq, Serialize, Type, Value, PartialOrd, Ord, OwnedValue,
26)]
27pub struct PropertyName<'name>(Str<'name>);
28
29/// Owned sibling of [`PropertyName`].
30#[derive(Clone, Hash, PartialEq, Eq, Serialize, Type, Value, PartialOrd, Ord, OwnedValue)]
31pub struct OwnedPropertyName(#[serde(borrow)] PropertyName<'static>);
32
33define_name_type_impls! {
34    name: PropertyName,
35    owned: OwnedPropertyName,
36    validate: ensure_correct_property_name,
37}
38
39fn ensure_correct_property_name(name: &str) -> Result<()> {
40    if name.is_empty() {
41        return Err(Error::InvalidName(
42            "Invalid property name. It has to be at least 1 character long.",
43        ));
44    } else if name.len() > 255 {
45        return Err(Error::InvalidName(
46            "Invalid property name. It can not be longer than 255 characters.",
47        ));
48    }
49
50    Ok(())
51}