accesskit_unix/atspi/
object_id.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2022 The AccessKit Authors. All rights reserved.
// Licensed under the Apache License, Version 2.0 (found in
// the LICENSE-APACHE file) or the MIT license (found in
// the LICENSE-MIT file), at your option.

use crate::atspi::OwnedObjectAddress;
use accesskit::NodeId;
use accesskit_atspi_common::PlatformNode;
use serde::{Serialize, Serializer};
use zbus::{
    names::OwnedUniqueName,
    zvariant::{ObjectPath, OwnedObjectPath, Signature, Structure, StructureBuilder, Type},
};

const ACCESSIBLE_PATH_PREFIX: &str = "/org/a11y/atspi/accessible/";
const ROOT_PATH: &str = "/org/a11y/atspi/accessible/root";

#[derive(Debug, PartialEq)]
pub(crate) enum ObjectId {
    Root,
    Node { adapter: usize, node: NodeId },
}

impl ObjectId {
    pub(crate) fn to_address(&self, bus_name: OwnedUniqueName) -> OwnedObjectAddress {
        OwnedObjectAddress::new(bus_name, self.path())
    }

    pub(crate) fn path(&self) -> OwnedObjectPath {
        match self {
            Self::Root => ObjectPath::from_str_unchecked(ROOT_PATH),
            Self::Node { adapter, node } => ObjectPath::from_string_unchecked(format!(
                "{}{}/{}",
                ACCESSIBLE_PATH_PREFIX, adapter, node.0
            )),
        }
        .into()
    }
}

impl Serialize for ObjectId {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            Self::Root => serializer.serialize_str("root"),
            Self::Node { node, .. } => serializer.serialize_str(&node.0.to_string()),
        }
    }
}

impl Type for ObjectId {
    fn signature() -> Signature<'static> {
        <&str>::signature()
    }
}

impl From<ObjectId> for Structure<'_> {
    fn from(id: ObjectId) -> Self {
        StructureBuilder::new()
            .add_field(match id {
                ObjectId::Root => "root".into(),
                ObjectId::Node { node, .. } => node.0.to_string(),
            })
            .build()
    }
}

impl From<&PlatformNode> for ObjectId {
    fn from(node: &PlatformNode) -> Self {
        Self::Node {
            adapter: node.adapter_id(),
            node: node.id(),
        }
    }
}