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
// Copyright 2020 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_filter_deprecated as filter;

pub trait FidlReturn {
    type Item;
    fn transform_result(self) -> Result<Self::Item, anyhow::Error>;
}

macro_rules! impl_trait {
    ($error:ident) => {
        pub struct $error(pub filter::$error);

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

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

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

        impl<T> FidlReturn for Result<Result<T, filter::$error>, fidl::Error> {
            type Item = T;
            fn transform_result(self) -> Result<Self::Item, anyhow::Error> {
                Ok(self.context("FIDL error")?.map_err($error).context("Filter error")?)
            }
        }
    };
}

impl_trait!(EnableDisableInterfaceError);
impl_trait!(FilterUpdateRulesError);
impl_trait!(FilterUpdateNatRulesError);
impl_trait!(FilterUpdateRdrRulesError);