vfs/test_utils/
node.rs

1// Copyright 2019 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//! Helper methods for the `NodeProxy` objects.
6
7use fidl::endpoints::{create_proxy, ProtocolMarker, ServerEnd};
8use fidl_fuchsia_io as fio;
9
10pub fn open_get_proxy<M>(proxy: &fio::DirectoryProxy, flags: fio::OpenFlags, path: &str) -> M::Proxy
11where
12    M: ProtocolMarker,
13{
14    let (new_proxy, new_server_end) = create_proxy::<M>();
15
16    #[cfg(fuchsia_api_level_at_least = "NEXT")]
17    proxy
18        .deprecated_open(
19            flags,
20            fio::ModeType::empty(),
21            path,
22            ServerEnd::<fio::NodeMarker>::new(new_server_end.into_channel()),
23        )
24        .unwrap();
25    #[cfg(not(fuchsia_api_level_at_least = "NEXT"))]
26    proxy
27        .open(
28            flags,
29            fio::ModeType::empty(),
30            path,
31            ServerEnd::<fio::NodeMarker>::new(new_server_end.into_channel()),
32        )
33        .unwrap();
34
35    new_proxy
36}
37
38pub fn open3_get_proxy<M>(
39    proxy: &fio::DirectoryProxy,
40    flags: fio::Flags,
41    options: &fio::Options,
42    path: &str,
43) -> M::Proxy
44where
45    M: ProtocolMarker,
46{
47    let (new_proxy, new_server_end) = create_proxy::<M>();
48
49    #[cfg(fuchsia_api_level_at_least = "NEXT")]
50    proxy.open(path, flags, options, new_server_end.into_channel()).unwrap();
51    #[cfg(not(fuchsia_api_level_at_least = "NEXT"))]
52    proxy.open3(path, flags, options, new_server_end.into_channel()).unwrap();
53
54    new_proxy
55}
56
57/// This trait repeats parts of the `NodeProxy` trait, and is implemented for `NodeProxy`,
58/// `FileProxy`, and `DirectoryProxy`, which all share the same API.  FIDL currently does not
59/// expose the API inheritance, so with this trait we have a workaround.  As soon as FIDL will
60/// export the necessary information we should remove this trait, as it is just a workaround. The
61/// downsides is that this mapping needs to be manually updated, and that it is not something all
62/// the users of FIDL of `NodeProxy` would be familiar with - unnecessary complexity.
63///
64/// Add methods to this trait when they are necessary to reduce the maintenance effort.
65pub trait NodeProxyApi {
66    fn clone(
67        &self,
68        flags: fio::OpenFlags,
69        server_end: ServerEnd<fio::NodeMarker>,
70    ) -> Result<(), fidl::Error>;
71}
72
73/// Calls .clone() on the proxy object, and returns a client side of the connection passed into the
74/// clone() method.
75pub fn clone_get_proxy<M, Proxy>(proxy: &Proxy, flags: fio::OpenFlags) -> M::Proxy
76where
77    M: ProtocolMarker,
78    Proxy: NodeProxyApi,
79{
80    let (new_proxy, new_server_end) = create_proxy::<M>();
81
82    proxy.clone(flags, new_server_end.into_channel().into()).unwrap();
83
84    new_proxy
85}
86
87impl NodeProxyApi for fio::NodeProxy {
88    fn clone(
89        &self,
90        flags: fio::OpenFlags,
91        server_end: ServerEnd<fio::NodeMarker>,
92    ) -> Result<(), fidl::Error> {
93        #[cfg(fuchsia_api_level_at_least = "26")]
94        return Self::deprecated_clone(self, flags, server_end);
95        #[cfg(not(fuchsia_api_level_at_least = "26"))]
96        return Self::clone(self, flags, server_end);
97    }
98}
99
100impl NodeProxyApi for fio::FileProxy {
101    fn clone(
102        &self,
103        flags: fio::OpenFlags,
104        server_end: ServerEnd<fio::NodeMarker>,
105    ) -> Result<(), fidl::Error> {
106        #[cfg(fuchsia_api_level_at_least = "26")]
107        return Self::deprecated_clone(self, flags, server_end);
108        #[cfg(not(fuchsia_api_level_at_least = "26"))]
109        return Self::clone(self, flags, server_end);
110    }
111}
112
113impl NodeProxyApi for fio::DirectoryProxy {
114    fn clone(
115        &self,
116        flags: fio::OpenFlags,
117        server_end: ServerEnd<fio::NodeMarker>,
118    ) -> Result<(), fidl::Error> {
119        #[cfg(fuchsia_api_level_at_least = "26")]
120        return Self::deprecated_clone(self, flags, server_end);
121        #[cfg(not(fuchsia_api_level_at_least = "26"))]
122        return Self::clone(self, flags, server_end);
123    }
124}