google_cloud_storage/storage/stub.rs
1// Copyright 2025 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::Result;
16use crate::model::{Object, ReadObjectRequest};
17use crate::model_ext::WriteObjectRequest;
18use crate::read_object::ReadObjectResponse;
19use crate::storage::request_options::RequestOptions;
20use crate::streaming_source::{Seek, StreamingSource};
21use crate::{
22 http::HeaderMap,
23 model_ext::{OpenObjectRequest, ReadRange},
24 object_descriptor::ObjectDescriptor as Descriptor,
25};
26#[cfg(google_cloud_unstable_storage_bidi)]
27use bytes::Bytes;
28use gaxi::unimplemented::UNIMPLEMENTED;
29
30/// Defines the trait used to implement [crate::client::Storage].
31///
32/// Application developers may need to implement this trait to mock
33/// `client::Storage`. In other use-cases, application developers only
34/// use `client::Storage` and need not be concerned with this trait or
35/// its implementations.
36///
37/// Services gain new RPCs routinely. Consequently, this trait gains new methods
38/// too. To avoid breaking applications the trait provides a default
39/// implementation of each method. Most of these implementations just return an
40/// error.
41pub trait Storage: std::fmt::Debug + Send + Sync {
42 /// Implements [crate::client::Storage::read_object].
43 fn read_object(
44 &self,
45 _req: ReadObjectRequest,
46 _options: RequestOptions,
47 ) -> impl std::future::Future<Output = Result<ReadObjectResponse>> + Send {
48 unimplemented_stub::<ReadObjectResponse>()
49 }
50
51 /// Implements [crate::client::Storage::write_object].
52 fn write_object_buffered<P>(
53 &self,
54 _payload: P,
55 _req: WriteObjectRequest,
56 _options: RequestOptions,
57 ) -> impl std::future::Future<Output = Result<Object>> + Send
58 where
59 P: StreamingSource + Send + Sync + 'static,
60 {
61 unimplemented_stub::<Object>()
62 }
63
64 /// Implements [crate::client::Storage::write_object].
65 fn write_object_unbuffered<P>(
66 &self,
67 _payload: P,
68 _req: WriteObjectRequest,
69 _options: RequestOptions,
70 ) -> impl std::future::Future<Output = Result<Object>> + Send
71 where
72 P: StreamingSource + Seek + Send + Sync + 'static,
73 {
74 unimplemented_stub::<Object>()
75 }
76
77 /// Implements [crate::client::Storage::open_object].
78 fn open_object(
79 &self,
80 _request: OpenObjectRequest,
81 _options: RequestOptions,
82 ) -> impl std::future::Future<Output = Result<(Descriptor, Vec<ReadObjectResponse>)>> + Send
83 {
84 unimplemented_stub::<(Descriptor, Vec<ReadObjectResponse>)>()
85 }
86
87 #[cfg(google_cloud_unstable_storage_bidi)]
88 /// Implements [crate::client::Storage::open_appendable_object].
89 fn open_appendable_object(
90 &self,
91 _request: crate::model_ext::OpenAppendableObjectRequest,
92 _options: RequestOptions,
93 ) -> impl std::future::Future<
94 Output = Result<crate::appendable_object_writer::AppendableObjectWriter>,
95 > + Send {
96 unimplemented_stub::<crate::appendable_object_writer::AppendableObjectWriter>()
97 }
98
99 #[cfg(google_cloud_unstable_storage_bidi)]
100 /// Implements [crate::client::Storage::open_appendable_object]. Also sends an
101 /// initial chunk of data with the opening request.
102 fn open_appendable_object_and_append(
103 &self,
104 _request: crate::model_ext::OpenAppendableObjectRequest,
105 _chunk: Bytes,
106 _options: RequestOptions,
107 ) -> impl std::future::Future<
108 Output = Result<crate::appendable_object_writer::AppendableObjectWriter>,
109 > + Send {
110 unimplemented_stub::<crate::appendable_object_writer::AppendableObjectWriter>()
111 }
112
113 #[cfg(google_cloud_unstable_storage_bidi)]
114 /// Implements [crate::client::Storage::reopen_appendable_object].
115 fn reopen_appendable_object(
116 &self,
117 _request: crate::model_ext::ReopenAppendableObjectRequest,
118 _options: RequestOptions,
119 ) -> impl std::future::Future<
120 Output = Result<crate::appendable_object_writer::AppendableObjectWriter>,
121 > + Send {
122 unimplemented_stub::<crate::appendable_object_writer::AppendableObjectWriter>()
123 }
124}
125
126/// Defines the trait used to implement [crate::object_descriptor::ObjectDescriptor].
127///
128/// Application developers may need to implement this trait to mock
129/// `ObjectDescriptor`. In other use-cases, application developers
130/// should use `ObjectDescriptor` directly, and need not be concerned
131/// with this trait or its implementations.
132pub trait ObjectDescriptor: std::fmt::Debug + Send + Sync {
133 /// The implementation for [ObjectDescriptor::object][Descriptor::object].
134 fn object(&self) -> Object;
135
136 /// The implementation for [ObjectDescriptor::read_range][Descriptor::read_range].
137 fn read_range(&self, range: ReadRange) -> impl Future<Output = ReadObjectResponse> + Send;
138
139 /// The implementation for [ObjectDescriptor::headers][Descriptor::headers].
140 fn headers(&self) -> HeaderMap;
141}
142
143/// Defines the trait used to implement [crate::appendable_object_writer::AppendableObjectWriter].
144///
145/// Application developers may need to implement this trait to mock
146/// `AppendableObjectWriter`. In other use-cases, application developers
147/// should use `AppendableObjectWriter` directly, and need not be concerned
148/// with this trait or its implementations.
149#[cfg(google_cloud_unstable_storage_bidi)]
150#[cfg_attr(docsrs, doc(cfg(feature = "unstable-stream")))]
151pub trait AppendableObjectWriter: std::fmt::Debug + Send + Sync {
152 /// The implementation for [AppendableObjectWriter::append][crate::appendable_object_writer::AppendableObjectWriter::append].
153 fn append(
154 &mut self,
155 chunk: Bytes,
156 ) -> impl std::future::Future<Output = crate::Result<()>> + Send;
157
158 /// The implementation for [AppendableObjectWriter::flush][crate::appendable_object_writer::AppendableObjectWriter::flush].
159 fn flush(&mut self) -> impl std::future::Future<Output = crate::Result<i64>> + Send;
160
161 /// The implementation for [AppendableObjectWriter::finalize][crate::appendable_object_writer::AppendableObjectWriter::finalize].
162 fn finalize(
163 self,
164 ) -> impl std::future::Future<Output = crate::Result<crate::model::Object>> + Send;
165
166 /// The implementation for [AppendableObjectWriter::close][crate::appendable_object_writer::AppendableObjectWriter::close].
167 fn close(self) -> impl std::future::Future<Output = crate::Result<i64>> + Send;
168
169 /// The implementation for [AppendableObjectWriter::generation][crate::appendable_object_writer::AppendableObjectWriter::generation].
170 fn generation(&self) -> i64;
171
172 /// The implementation for [AppendableObjectWriter::persisted_size][crate::appendable_object_writer::AppendableObjectWriter::persisted_size].
173 fn persisted_size(&self) -> i64;
174}
175
176async fn unimplemented_stub<T>() -> google_cloud_gax::Result<T> {
177 unimplemented!("{UNIMPLEMENTED}");
178}