Skip to content

Commit a53ad51

Browse files
authored
prost_build: BytesType and MapType into a collections module. (#1030)
1 parent 1fc736e commit a53ad51

File tree

3 files changed

+61
-59
lines changed

3 files changed

+61
-59
lines changed

prost-build/src/code_generator.rs

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::ast::{Comments, Method, Service};
1818
use crate::extern_paths::ExternPaths;
1919
use crate::ident::{strip_enum_prefix, to_snake, to_upper_camel};
2020
use crate::message_graph::MessageGraph;
21-
use crate::{BytesType, Config, MapType};
21+
use crate::Config;
2222

2323
mod c_escaping;
2424
use c_escaping::unescape_c_escape_string;
@@ -1123,39 +1123,3 @@ fn build_enum_value_mappings<'a>(
11231123
}
11241124
mappings
11251125
}
1126-
1127-
impl MapType {
1128-
/// The `prost-derive` annotation type corresponding to the map type.
1129-
fn annotation(&self) -> &'static str {
1130-
match self {
1131-
MapType::HashMap => "map",
1132-
MapType::BTreeMap => "btree_map",
1133-
}
1134-
}
1135-
1136-
/// The fully-qualified Rust type corresponding to the map type.
1137-
fn rust_type(&self) -> &'static str {
1138-
match self {
1139-
MapType::HashMap => "::std::collections::HashMap",
1140-
MapType::BTreeMap => "::prost::alloc::collections::BTreeMap",
1141-
}
1142-
}
1143-
}
1144-
1145-
impl BytesType {
1146-
/// The `prost-derive` annotation type corresponding to the bytes type.
1147-
fn annotation(&self) -> &'static str {
1148-
match self {
1149-
BytesType::Vec => "vec",
1150-
BytesType::Bytes => "bytes",
1151-
}
1152-
}
1153-
1154-
/// The fully-qualified Rust type corresponding to the bytes type.
1155-
fn rust_type(&self) -> &'static str {
1156-
match self {
1157-
BytesType::Vec => "::prost::alloc::vec::Vec<u8>",
1158-
BytesType::Bytes => "::prost::bytes::Bytes",
1159-
}
1160-
}
1161-
}

prost-build/src/collections.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/// The map collection type to output for Protobuf `map` fields.
2+
#[non_exhaustive]
3+
#[derive(Default, Clone, Copy, Debug, PartialEq)]
4+
pub(crate) enum MapType {
5+
/// The [`std::collections::HashMap`] type.
6+
#[default]
7+
HashMap,
8+
/// The [`std::collections::BTreeMap`] type.
9+
BTreeMap,
10+
}
11+
12+
/// The bytes collection type to output for Protobuf `bytes` fields.
13+
#[non_exhaustive]
14+
#[derive(Default, Clone, Copy, Debug, PartialEq)]
15+
pub(crate) enum BytesType {
16+
/// The [`alloc::collections::Vec::<u8>`] type.
17+
#[default]
18+
Vec,
19+
/// The [`bytes::Bytes`] type.
20+
Bytes,
21+
}
22+
23+
impl MapType {
24+
/// The `prost-derive` annotation type corresponding to the map type.
25+
pub fn annotation(&self) -> &'static str {
26+
match self {
27+
MapType::HashMap => "map",
28+
MapType::BTreeMap => "btree_map",
29+
}
30+
}
31+
32+
/// The fully-qualified Rust type corresponding to the map type.
33+
pub fn rust_type(&self) -> &'static str {
34+
match self {
35+
MapType::HashMap => "::std::collections::HashMap",
36+
MapType::BTreeMap => "::prost::alloc::collections::BTreeMap",
37+
}
38+
}
39+
}
40+
41+
impl BytesType {
42+
/// The `prost-derive` annotation type corresponding to the bytes type.
43+
pub fn annotation(&self) -> &'static str {
44+
match self {
45+
BytesType::Vec => "vec",
46+
BytesType::Bytes => "bytes",
47+
}
48+
}
49+
50+
/// The fully-qualified Rust type corresponding to the bytes type.
51+
pub fn rust_type(&self) -> &'static str {
52+
match self {
53+
BytesType::Vec => "::prost::alloc::vec::Vec<u8>",
54+
BytesType::Bytes => "::prost::bytes::Bytes",
55+
}
56+
}
57+
}

prost-build/src/lib.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ use prost_types::FileDescriptorSet;
139139
mod ast;
140140
pub use crate::ast::{Comments, Method, Service};
141141

142+
mod collections;
143+
pub(crate) use collections::{BytesType, MapType};
144+
142145
mod code_generator;
143146
mod extern_paths;
144147
mod ident;
@@ -196,28 +199,6 @@ pub trait ServiceGenerator {
196199
fn finalize_package(&mut self, _package: &str, _buf: &mut String) {}
197200
}
198201

199-
/// The map collection type to output for Protobuf `map` fields.
200-
#[non_exhaustive]
201-
#[derive(Default, Clone, Copy, Debug, PartialEq)]
202-
enum MapType {
203-
/// The [`std::collections::HashMap`] type.
204-
#[default]
205-
HashMap,
206-
/// The [`std::collections::BTreeMap`] type.
207-
BTreeMap,
208-
}
209-
210-
/// The bytes collection type to output for Protobuf `bytes` fields.
211-
#[non_exhaustive]
212-
#[derive(Default, Clone, Copy, Debug, PartialEq)]
213-
enum BytesType {
214-
/// The [`alloc::collections::Vec::<u8>`] type.
215-
#[default]
216-
Vec,
217-
/// The [`bytes::Bytes`] type.
218-
Bytes,
219-
}
220-
221202
/// Compile `.proto` files into Rust files during a Cargo build.
222203
///
223204
/// The generated `.rs` files are written to the Cargo `OUT_DIR` directory, suitable for use with

0 commit comments

Comments
 (0)