1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! A primitive utility library for Protocol Buffers in Rust.
//!
//! # ⚠️ NOT A GOOGLE OFFICIAL PRODUCT
//!
//! **This library is not an official Google product.** Google provides its own Protocol Buffers
//! implementation for Rust (see [protocolbuffers/protobuf](https://github.com/protocolbuffers/protobuf)
//! and official Rust support). This project is an **independent, community-maintained** library.
//!
//! ---
//!
//! This library provides common definitions, constants, enums, and basic logic for implementing
//! Protocol Buffers. It is designed to minimize entry barriers for developers who want to
//! implement Protocol Buffers functionality.
//!
//! ## Overview
//!
//! This library provides **building blocks** for implementing Protocol Buffers, not a complete
//! message parser or serializer. It focuses on:
//!
//! - **Low-level primitives**: Raw field I/O without semantic interpretation
//! - **Flexibility**: Support for both owned and borrowed data
//! - **Minimal dependencies**: Only depends on `thiserror` for error handling
//! - **Clear API**: Trait-based extension methods following Rust conventions
//!
//! ## Quick Start
//!
//! ### Reading Fields
//!
//! This library provides multiple ways to read protobuf fields from different sources:
//!
//! ```rust
//! use protobuf_core::{Field, IteratorExtProtobuf, AsRefExtProtobuf};
//!
//! // From a byte iterator - returns Field<Vec<u8>> (Len values are Vec<u8>)
//! let bytes = vec![0x08, 0x96, 0x01]; // field 1: 150
//! let fields: Vec<Field<Vec<u8>>> = bytes
//! .into_iter()
//! .protobuf_fields()
//! .collect::<Result<Vec<Field<Vec<u8>>>, _>>()
//! .unwrap();
//!
//! // From a slice - returns Field<&[u8]> (Len values are &[u8], zero-copy)
//! let slice: &[u8] = &[0x08, 0x96, 0x01];
//! let fields: Vec<Field<&[u8]>> = AsRefExtProtobuf::read_protobuf_fields(&slice)
//! .collect::<Result<Vec<Field<&[u8]>>, _>>()
//! .unwrap();
//! ```
//!
//! ### Writing Fields
//!
//! ```rust
//! use protobuf_core::{WriteExtProtobuf, Field, FieldValue, FieldNumber};
//!
//! let mut buffer = Vec::new();
//! let field: Field<Vec<u8>> = Field::new(
//! FieldNumber::try_from(1)?,
//! FieldValue::from_uint64(150)
//! );
//! buffer.write_protobuf_field(&field)?;
//! # Ok::<(), protobuf_core::ProtobufError>(())
//! ```
//!
//! ## Core Types
//!
//! ### Fields
//!
//! - [`Field<L>`]: Represents a raw protobuf field with a field number and value.
//! The `L` type parameter represents the type used for length-delimited values (e.g., `Vec<u8>`
//! for owned data, `&'a [u8]` for borrowed data).
//! - [`FieldValue<L>`]: Represents the raw value of a field. It can be:
//! - `Varint(Varint)`: Variable-width integers (Int32, Int64, UInt32, UInt64, SInt32, SInt64,
//! Bool, Enum)
//! - `I32([u8; 4])`: 32-bit fixed-width values (Fixed32, SFixed32, Float)
//! - `I64([u8; 8])`: 64-bit fixed-width values (Fixed64, SFixed64, Double)
//! - `Len(L)`: Length-delimited values (String, Bytes, embedded messages, packed repeated
//! fields)
//!
//! ### Basic Types
//!
//! - [`Tag`]: Represents a protobuf tag (field number + wire type)
//! - [`Varint`]: Represents a deserialized varint value (8-byte internal
//! representation)
//! - [`FieldNumber`]: A validated field number (range: 1 to 2^29 - 1)
//! - [`WireType`]: Represents the protobuf wire type (Varint, Int32, Int64,
//! Len, StartGroup, EndGroup)
//!
//! ### Error Handling
//!
//! - [`ProtobufError`]: Unified error type for all protobuf operations
//! - [`Result<T>`]: Type alias for `Result<T, ProtobufError>`
//!
//! ## Reading Traits
//!
//! The library provides several traits for reading protobuf fields from different sources:
//!
//! - [`IteratorExtProtobuf`]: Read fields from `Iterator<Item = u8>`
//! - Output: [`Field<Vec<u8>>`] - Len values are owned `Vec<u8>`
//! - [`TryIteratorExtProtobuf`]: Read fields from
//! `Iterator<Item = Result<u8, E>>`
//! - Output: [`Field<Vec<u8>>`] - Len values are owned `Vec<u8>`
//! - [`AsRefExtProtobuf`]: Read fields from `AsRef<[u8]>` types
//! (slices, arrays, etc.)
//! - Output: [`Field<&[u8]>`] - Len values are borrowed `&[u8]` slices (zero-copy)
//! - [`ReadExtProtobuf`]: Read fields from `std::io::Read` types
//! - Output: [`Field<Vec<u8>>`] - Len values are owned `Vec<u8>`
//!
//! ## Writing Traits
//!
//! - [`WriteExtProtobuf`]: Write fields to `std::io::Write`
//!
//! ## Tag/Varint Traits
//!
//! For lower-level operations, the library provides traits for reading and writing tags and
//! varints:
//!
//! - [`IteratorExtTag`] / [`TryIteratorExtTag`]
//! / [`ReadExtTag`]: Read tags (sync, with partial/resume for chunked input)
//! - [`IteratorExtVarint`] / [`TryIteratorExtVarint`]
//! / [`ReadExtVarint`]: Read varints (sync, with partial/resume for chunked input)
//! - [`WriteExtVarint`]: Write varints
//!
//! ## Feature Flags
//!
//! - `read` (enabled by default): Enables field reading utilities
//! - `write` (enabled by default): Enables field writing utilities
//!
//! You can use features independently:
//!
//! ```toml
//! [dependencies]
//! protobuf-core = { version = "0.1.0", default-features = false, features = ["read"] }
//! ```
//!
//! ## Constants
//!
//! The library also provides various constants related to the protobuf wire format:
//!
//! - Field number limits: [`MIN_FIELD_NUMBER`], [`MAX_FIELD_NUMBER`]
//! - Size limits: [`MAX_MESSAGE_SIZE`], [`MAX_STRING_SIZE`], [`MAX_VARINT_BYTES`]
//! - Wire format constants: [`FIXED32_BYTES`], [`FIXED64_BYTES`], etc.
pub
pub
pub
pub
pub
pub use WriteExtProtobuf;
pub use ;
pub use ;
pub use FieldNumber;
pub use ;
pub use ;
pub use ;
use Infallible;
use Error;
/// Unified error type for all protobuf operations
/// Custom Result type for protobuf operations
pub type Result<T> = Result;