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