xref: /aosp_15_r20/external/pigweed/pw_protobuf/public/pw_protobuf/map_utils.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2021 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // 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, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 //
15 // The header provides a set of helper utils for protobuf related operations.
16 // The APIs may not be finalized yet.
17 
18 #pragma once
19 
20 #include <cstddef>
21 #include <string_view>
22 
23 #include "pw_assert/check.h"
24 #include "pw_protobuf/stream_decoder.h"
25 #include "pw_status/status.h"
26 #include "pw_status/try.h"
27 #include "pw_stream/stream.h"
28 
29 namespace pw::protobuf {
30 
31 // The function writes an entry for the proto map<string, bytes> field type.
32 //
33 // Args:
34 //   field_number - The field number for the map.
35 //   key - The string payload for the key value of the entry.
36 //   key_size - Number of bytes in the key.
37 //   value - The value payload for the entry.
38 //   value_size - Number of bytes in the value.
39 //   stream_pipe_buffer - A non-zero size buffer for the function to read and
40 //     store data from the reader and write to the given writer.
41 //   writer - The output writer to write to.
42 //
43 // Returns:
44 // OK - Entry is successfully written.
45 // RESOURCE_EXHAUSTED - Entry would exceed write limit.
46 // INVALID_ARGUMENT - Field number is invalid.
47 //
48 // Since all length-delimited fields can be treated as `bytes`,
49 // it can be used to write any string to length-delimited field map, i.e.
50 // map<string, message>, map<string, bytes> etc.
51 Status WriteProtoStringToBytesMapEntry(uint32_t field_number,
52                                        stream::Reader& key,
53                                        size_t key_size,
54                                        stream::Reader& value,
55                                        size_t value_size,
56                                        ByteSpan stream_pipe_buffer,
57                                        stream::Writer& writer);
58 
59 }  // namespace pw::protobuf
60