netfilter/
error.rs

1// Copyright 2020 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_filter_deprecated as filter;
8
9pub trait FidlReturn {
10    type Item;
11    fn transform_result(self) -> Result<Self::Item, anyhow::Error>;
12}
13
14macro_rules! impl_trait {
15    ($error:ident) => {
16        pub struct $error(pub filter::$error);
17
18        impl std::fmt::Debug for $error {
19            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20                std::fmt::Debug::fmt(&self.0, f)
21            }
22        }
23
24        impl std::fmt::Display for $error {
25            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26                std::fmt::Debug::fmt(self, f)
27            }
28        }
29
30        impl std::error::Error for $error {}
31
32        impl<T> FidlReturn for Result<Result<T, filter::$error>, fidl::Error> {
33            type Item = T;
34            fn transform_result(self) -> Result<Self::Item, anyhow::Error> {
35                Ok(self.context("FIDL error")?.map_err($error).context("Filter error")?)
36            }
37        }
38    };
39}
40
41impl_trait!(EnableDisableInterfaceError);
42impl_trait!(FilterUpdateRulesError);
43impl_trait!(FilterUpdateNatRulesError);
44impl_trait!(FilterUpdateRdrRulesError);