| 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 | |
| 6 | use crate::atspi::OwnedObjectAddress; |
| 7 | use accesskit::NodeId; |
| 8 | use accesskit_atspi_common::PlatformNode; |
| 9 | use serde::{Serialize, Serializer}; |
| 10 | use zbus::{ |
| 11 | names::UniqueName, |
| 12 | zvariant::{ObjectPath, OwnedObjectPath, Signature, Structure, StructureBuilder, Type}, |
| 13 | }; |
| 14 | |
| 15 | const ACCESSIBLE_PATH_PREFIX: &str = "/org/a11y/atspi/accessible/" ; |
| 16 | const ROOT_PATH: &str = "/org/a11y/atspi/accessible/root" ; |
| 17 | |
| 18 | #[derive (Debug, PartialEq)] |
| 19 | pub(crate) enum ObjectId { |
| 20 | Root, |
| 21 | Node { adapter: usize, node: NodeId }, |
| 22 | } |
| 23 | |
| 24 | impl ObjectId { |
| 25 | pub(crate) fn to_address(&self, bus_name: &UniqueName) -> 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 | |
| 41 | impl 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 | |
| 53 | impl Type for ObjectId { |
| 54 | fn signature() -> Signature<'static> { |
| 55 | <&str>::signature() |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | impl 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 | |
| 70 | impl 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 | |