1// Copyright 2022 The AccessKit Authors. All rights reserved.
2// Licensed under the Apache License, Version 2.0 (found in
3// the LICENSE-APACHE file) or the MIT license (found in
4// the LICENSE-MIT file), at your option.
5
6use crate::atspi::OwnedObjectAddress;
7use accesskit::NodeId;
8use accesskit_atspi_common::PlatformNode;
9use serde::{Serialize, Serializer};
10use zbus::{
11 names::OwnedUniqueName,
12 zvariant::{ObjectPath, OwnedObjectPath, Signature, Structure, StructureBuilder, Type},
13};
14
15const ACCESSIBLE_PATH_PREFIX: &str = "/org/a11y/atspi/accessible/";
16const ROOT_PATH: &str = "/org/a11y/atspi/accessible/root";
17
18#[derive(Debug, PartialEq)]
19pub(crate) enum ObjectId {
20 Root,
21 Node { adapter: usize, node: NodeId },
22}
23
24impl ObjectId {
25 pub(crate) fn to_address(&self, bus_name: OwnedUniqueName) -> OwnedObjectAddress {
26 OwnedObjectAddress::new(bus_name, self.path())
27 }
28
29 pub(crate) fn path(&self) -> OwnedObjectPath {
30 matchObjectPath<'_> self {
31 Self::Root => ObjectPath::from_str_unchecked(ROOT_PATH),
32 Self::Node { adapter: &usize, node: &NodeId } => ObjectPath::from_string_unchecked(path:format!(
33 "{}{}/{}",
34 ACCESSIBLE_PATH_PREFIX, adapter, node.0
35 )),
36 }
37 .into()
38 }
39}
40
41impl Serialize for ObjectId {
42 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
43 where
44 S: Serializer,
45 {
46 match self {
47 Self::Root => serializer.serialize_str("root"),
48 Self::Node { node: &NodeId, .. } => serializer.serialize_str(&node.0.to_string()),
49 }
50 }
51}
52
53impl Type for ObjectId {
54 fn signature() -> Signature<'static> {
55 <&str>::signature()
56 }
57}
58
59impl From<ObjectId> for Structure<'_> {
60 fn from(id: ObjectId) -> Self {
61 StructureBuilderStructureBuilder<'_>::new()
62 .add_field(match id {
63 ObjectId::Root => "root".into(),
64 ObjectId::Node { node: NodeId, .. } => node.0.to_string(),
65 })
66 .build()
67 }
68}
69
70impl From<&PlatformNode> for ObjectId {
71 fn from(node: &PlatformNode) -> Self {
72 Self::Node {
73 adapter: node.adapter_id(),
74 node: node.id(),
75 }
76 }
77}
78