Skip to main content

policy_properties/
lib.rs

1// Copyright 2025 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
5//! Utilities to make interacting with `fuchsia.net.policy.properties` more ergonomic.
6
7#![deny(unused)]
8#![deny(missing_docs)]
9
10use fidl_fuchsia_net_policy_properties as fnp_properties;
11
12/// Extensions for [`fnp_properties::NetworkToken`].
13pub trait NetworkTokenExt: Sized {
14    /// Attempt to make a copy of the current [`fnp_properties::NetworkToken`],
15    /// to allow it to be passed to a
16    /// `fuchsia.net.policy.properties/Networks.WatchProperties` call.
17    fn duplicate(&self) -> Result<Self, zx::Status>;
18
19    /// Get the [`zx::Koid`] for the token value.
20    fn koid(&self) -> Result<zx::Koid, zx::Status>;
21}
22
23impl NetworkTokenExt for fnp_properties::NetworkToken {
24    fn duplicate(&self) -> Result<fnp_properties::NetworkToken, zx::Status> {
25        Ok(fnp_properties::NetworkToken {
26            value: self.value.duplicate_handle(zx::Rights::SAME_RIGHTS)?,
27        })
28    }
29
30    fn koid(&self) -> Result<zx::Koid, zx::Status> {
31        self.value.koid()
32    }
33}
34
35/// Utilities to extend a [`fnp_properties::NetworksWatchDefaultResponse`].
36pub trait NetworksWatchDefaultResponseExt {
37    /// Return the resulting ['fnp_properties::NetworkToken`] or None if one isn't present.
38    fn take_network(&mut self) -> Option<fnp_properties::NetworkToken>;
39
40    /// Convert the response into its resulting [`fnp_properties::NetworkToken`]
41    /// or None if one isn't present.
42    fn into_network(self) -> Option<fnp_properties::NetworkToken>;
43}
44
45impl NetworksWatchDefaultResponseExt for fnp_properties::NetworksWatchDefaultResponse {
46    fn take_network(&mut self) -> Option<fnp_properties::NetworkToken> {
47        let mut replace =
48            fnp_properties::NetworksWatchDefaultResponse::NoDefaultNetwork(fnp_properties::Empty);
49        std::mem::swap(&mut replace, self);
50        replace.into_network()
51    }
52
53    fn into_network(self) -> Option<fidl_fuchsia_net_policy_properties::NetworkToken> {
54        match self {
55            fnp_properties::NetworksWatchDefaultResponse::Network(network_token) => {
56                Some(network_token)
57            }
58            fnp_properties::NetworksWatchDefaultResponse::NoDefaultNetwork(_)
59            | fnp_properties::NetworksWatchDefaultResponse::__SourceBreaking { .. } => None,
60        }
61    }
62}