Skip to main content

hap_model/
tree.rs

1//! The typed accessory attribute tree: [`Accessory`] → [`Service`] →
2//! [`Characteristic`].
3
4use crate::format::{CharFormat, CharValue};
5use crate::generated::{CharacteristicType, ServiceType};
6use crate::perms::Perms;
7use serde::Deserialize;
8
9/// One accessory in the bridge/accessory database.
10#[derive(Debug, Clone, PartialEq)]
11pub struct Accessory {
12    /// Accessory instance id (unique within the response).
13    pub aid: u64,
14    /// The accessory's services.
15    pub services: Vec<Service>,
16}
17
18/// One service on an accessory.
19#[derive(Debug, Clone, PartialEq)]
20pub struct Service {
21    /// Service instance id (unique within the accessory).
22    pub iid: u64,
23    /// The HAP service type (decoded from the short `type` UUID).
24    pub service_type: ServiceType,
25    /// The service's characteristics.
26    pub characteristics: Vec<Characteristic>,
27}
28
29/// One characteristic on a service.
30#[derive(Debug, Clone, PartialEq)]
31pub struct Characteristic {
32    /// Characteristic instance id (unique within the accessory).
33    pub iid: u64,
34    /// The HAP characteristic type (decoded from the short `type` UUID).
35    pub char_type: CharacteristicType,
36    /// The value format.
37    pub format: CharFormat,
38    /// Permissions.
39    pub perms: Perms,
40    /// The current value, if the accessory included one and it decoded under
41    /// `format`. `None` if absent.
42    pub value: Option<CharValue>,
43    /// Optional unit string (e.g. `"percentage"`, `"celsius"`).
44    pub unit: Option<String>,
45    /// Optional minimum value constraint.
46    pub min_value: Option<f64>,
47    /// Optional maximum value constraint.
48    pub max_value: Option<f64>,
49    /// Optional step constraint.
50    pub min_step: Option<f64>,
51    /// Optional maximum length (for string/data formats).
52    pub max_len: Option<u64>,
53}
54
55// ---- wire structs (private; mirror the JSON exactly) ----
56
57#[derive(Debug, Deserialize)]
58pub(crate) struct WireAccessories {
59    pub accessories: Vec<WireAccessory>,
60}
61
62#[derive(Debug, Deserialize)]
63pub(crate) struct WireAccessory {
64    pub aid: u64,
65    pub services: Vec<WireService>,
66}
67
68#[derive(Debug, Deserialize)]
69pub(crate) struct WireService {
70    pub iid: u64,
71    #[serde(rename = "type")]
72    pub type_: crate::uuid::Uuid,
73    pub characteristics: Vec<WireCharacteristic>,
74}
75
76#[derive(Debug, Deserialize)]
77pub(crate) struct WireCharacteristic {
78    pub iid: u64,
79    #[serde(rename = "type")]
80    pub type_: crate::uuid::Uuid,
81    pub format: CharFormat,
82    pub perms: Perms,
83    #[serde(default)]
84    pub value: Option<serde_json::Value>,
85    #[serde(default)]
86    pub unit: Option<String>,
87    #[serde(default, rename = "minValue")]
88    pub min_value: Option<f64>,
89    #[serde(default, rename = "maxValue")]
90    pub max_value: Option<f64>,
91    #[serde(default, rename = "minStep")]
92    pub min_step: Option<f64>,
93    #[serde(default, rename = "maxLen")]
94    pub max_len: Option<u64>,
95}