fidl_fuchsia_net_stack_ext/
error.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
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use anyhow::Context as _;

use fidl_fuchsia_net_stack as fidl_net_stack;

#[macro_export]
macro_rules! exec_fidl {
    ($method:expr, $context:expr) => {
        $method.await.squash_result().context($context)
    };
}

/// Newtype wrapper of `fidl_fuchsia_net_stack::Error` so that external traits
/// (`thiserror::Error` in particular) can be implemented for it.
pub struct NetstackError(pub fidl_net_stack::Error);

impl std::fmt::Debug for NetstackError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Debug::fmt(&self.0, f)
    }
}

impl std::fmt::Display for NetstackError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Debug::fmt(self, f)
    }
}

impl std::error::Error for NetstackError {}

/// Helper trait to reduce boilerplate issuing FIDL calls.
pub trait FidlReturn {
    type Item;
    fn squash_result(self) -> Result<Self::Item, anyhow::Error>;
}

impl<T> FidlReturn for Result<Result<T, fidl_net_stack::Error>, fidl::Error> {
    type Item = T;

    fn squash_result(self) -> Result<Self::Item, anyhow::Error> {
        Ok(self.context("FIDL error")?.map_err(NetstackError).context("Netstack error")?)
    }
}