protobuf_core/
lib.rs

1// Copyright 2021 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//      http://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
15//! Protocol Buffers core library
16//!
17//! This library provides common definitions, constants, enums, and basic logic
18//! for implementing Protocol Buffers in Rust.
19
20#[cfg(any(feature = "read", feature = "write"))]
21pub mod field;
22pub mod field_number;
23pub mod tag;
24pub mod varint;
25pub mod wire_format;
26
27#[cfg(feature = "write")]
28pub use self::field::WriteExtProtobuf;
29#[cfg(any(feature = "read", feature = "write"))]
30pub use self::field::{Field, FieldValue};
31#[cfg(feature = "read")]
32pub use self::field::{ProtobufFieldIterator, ReadExtProtobuf};
33pub use self::field_number::FieldNumber;
34pub use self::tag::{ReadExtTag, Tag, read_tag};
35pub use self::varint::{IteratorExtVarint, ReadExtVarint, Varint, WriteExtVarint};
36pub use self::wire_format::{MAX_FIELD_NUMBER, MAX_MESSAGE_SIZE, MIN_FIELD_NUMBER, WireType};
37
38use ::thiserror::Error;
39
40/// Unified error type for all protobuf operations
41#[derive(Error, Debug)]
42pub enum ProtobufError {
43    #[error("Field number {value} is out of valid range [1, 536_870_911]")]
44    FieldNumberOutOfRange { value: String },
45
46    #[error("Invalid wire type: {value} (must be 0-5)")]
47    InvalidWireType { value: u8 },
48
49    #[error("Varint value {value} is out of range for target type: {target_type}")]
50    VarintDowncastOutOfRange {
51        value: u64,
52        target_type: &'static str,
53    },
54
55    #[error("Failed to downcast field value to expected type: {expected_type}")]
56    FieldTypeDowncastError { expected_type: String },
57
58    #[error("Malformed tag: field_number={field_number}, wire_type={wire_type}")]
59    MalformedTag { field_number: u32, wire_type: u8 },
60
61    #[error("Unexpected EOF while parsing field")]
62    UnexpectedEof,
63
64    #[error("I/O error: {0}")]
65    IoError(#[from] ::std::io::Error),
66}
67
68/// Custom Result type for protobuf operations
69pub type Result<T> = ::std::result::Result<T, ProtobufError>;