fidl_fuchsia_net_stack_ext/
error.rs

1// Copyright 2019 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use anyhow::Context as _;
6
7use fidl_fuchsia_net_stack as fidl_net_stack;
8
9#[macro_export]
10macro_rules! exec_fidl {
11    ($method:expr, $context:expr) => {
12        $method.await.squash_result().context($context)
13    };
14}
15
16/// Newtype wrapper of `fidl_fuchsia_net_stack::Error` so that external traits
17/// (`thiserror::Error` in particular) can be implemented for it.
18pub struct NetstackError(pub fidl_net_stack::Error);
19
20impl std::fmt::Debug for NetstackError {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        std::fmt::Debug::fmt(&self.0, f)
23    }
24}
25
26impl std::fmt::Display for NetstackError {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        std::fmt::Debug::fmt(self, f)
29    }
30}
31
32impl std::error::Error for NetstackError {}
33
34/// Helper trait to reduce boilerplate issuing FIDL calls.
35pub trait FidlReturn {
36    type Item;
37    fn squash_result(self) -> Result<Self::Item, anyhow::Error>;
38}
39
40impl<T> FidlReturn for Result<Result<T, fidl_net_stack::Error>, fidl::Error> {
41    type Item = T;
42
43    fn squash_result(self) -> Result<Self::Item, anyhow::Error> {
44        Ok(self.context("FIDL error")?.map_err(NetstackError).context("Netstack error")?)
45    }
46}