accesskit_unix/
util.rs

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 accesskit_atspi_common::{Error as InternalError, PlatformNode};
7use zbus::fdo::Error as FdoError;
8
9use crate::atspi::ObjectId;
10
11#[cfg(not(feature = "tokio"))]
12pub(crate) fn block_on<F: std::future::Future>(future: F) -> F::Output {
13    futures_lite::future::block_on(future)
14}
15
16#[cfg(feature = "tokio")]
17pub(crate) fn block_on<F: std::future::Future>(future: F) -> F::Output {
18    let runtime = tokio::runtime::Builder::new_current_thread()
19        .enable_io()
20        .enable_time()
21        .build()
22        .expect("launch of single-threaded tokio runtime");
23    runtime.block_on(future)
24}
25
26pub(crate) fn map_error(source: ObjectId, error: InternalError) -> FdoError {
27    match error {
28        InternalError::Defunct | InternalError::UnsupportedInterface => {
29            FdoError::UnknownObject(source.path().to_string())
30        }
31        InternalError::TooManyChildren => FdoError::Failed("Too many children.".into()),
32        InternalError::IndexOutOfRange => FdoError::Failed("Index is too big.".into()),
33        InternalError::TooManyCharacters => FdoError::Failed("Too many characters.".into()),
34        InternalError::UnsupportedTextGranularity => {
35            FdoError::Failed("Unsupported text granularity.".into())
36        }
37    }
38}
39
40pub(crate) fn map_error_from_node(source: &PlatformNode, error: InternalError) -> FdoError {
41    map_error(ObjectId::from(source), error)
42}