diff --git a/prost-build/src/code_generator.rs b/prost-build/src/code_generator.rs index 406c5cc9e..8cce080aa 100644 --- a/prost-build/src/code_generator.rs +++ b/prost-build/src/code_generator.rs @@ -1227,42 +1227,6 @@ fn build_enum_value_mappings<'a>( mappings } -impl MapType { - /// The `prost-derive` annotation type corresponding to the map type. - fn annotation(&self) -> &'static str { - match self { - MapType::HashMap => "map", - MapType::BTreeMap => "btree_map", - } - } - - /// The fully-qualified Rust type corresponding to the map type. - fn rust_type(&self) -> &'static str { - match self { - MapType::HashMap => "::std::collections::HashMap", - MapType::BTreeMap => "::prost::alloc::collections::BTreeMap", - } - } -} - -impl BytesType { - /// The `prost-derive` annotation type corresponding to the bytes type. - fn annotation(&self) -> &'static str { - match self { - BytesType::Vec => "vec", - BytesType::Bytes => "bytes", - } - } - - /// The fully-qualified Rust type corresponding to the bytes type. - fn rust_type(&self) -> &'static str { - match self { - BytesType::Vec => "::prost::alloc::vec::Vec", - BytesType::Bytes => "::prost::bytes::Bytes", - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/prost-build/src/collections.rs b/prost-build/src/collections.rs new file mode 100644 index 000000000..63be4d627 --- /dev/null +++ b/prost-build/src/collections.rs @@ -0,0 +1,57 @@ +/// The map collection type to output for Protobuf `map` fields. +#[non_exhaustive] +#[derive(Default, Clone, Copy, Debug, PartialEq)] +pub(crate) enum MapType { + /// The [`std::collections::HashMap`] type. + #[default] + HashMap, + /// The [`std::collections::BTreeMap`] type. + BTreeMap, +} + +/// The bytes collection type to output for Protobuf `bytes` fields. +#[non_exhaustive] +#[derive(Default, Clone, Copy, Debug, PartialEq)] +pub(crate) enum BytesType { + /// The [`alloc::collections::Vec::`] type. + #[default] + Vec, + /// The [`bytes::Bytes`] type. + Bytes, +} + +impl MapType { + /// The `prost-derive` annotation type corresponding to the map type. + pub fn annotation(&self) -> &'static str { + match self { + MapType::HashMap => "map", + MapType::BTreeMap => "btree_map", + } + } + + /// The fully-qualified Rust type corresponding to the map type. + pub fn rust_type(&self) -> &'static str { + match self { + MapType::HashMap => "::std::collections::HashMap", + MapType::BTreeMap => "::prost::alloc::collections::BTreeMap", + } + } +} + +impl BytesType { + /// The `prost-derive` annotation type corresponding to the bytes type. + pub fn annotation(&self) -> &'static str { + match self { + BytesType::Vec => "vec", + BytesType::Bytes => "bytes", + } + } + + /// The fully-qualified Rust type corresponding to the bytes type. + pub fn rust_type(&self) -> &'static str { + match self { + BytesType::Vec => "::prost::alloc::vec::Vec", + BytesType::Bytes => "::prost::bytes::Bytes", + } + } +} diff --git a/prost-build/src/lib.rs b/prost-build/src/lib.rs index 7b4f43cba..c979d19cd 100644 --- a/prost-build/src/lib.rs +++ b/prost-build/src/lib.rs @@ -139,6 +139,9 @@ use prost_types::FileDescriptorSet; mod ast; pub use crate::ast::{Comments, Method, Service}; +mod collections; +pub(crate) use collections::{BytesType, MapType}; + mod code_generator; mod extern_paths; mod ident; @@ -196,28 +199,6 @@ pub trait ServiceGenerator { fn finalize_package(&mut self, _package: &str, _buf: &mut String) {} } -/// The map collection type to output for Protobuf `map` fields. -#[non_exhaustive] -#[derive(Default, Clone, Copy, Debug, PartialEq)] -enum MapType { - /// The [`std::collections::HashMap`] type. - #[default] - HashMap, - /// The [`std::collections::BTreeMap`] type. - BTreeMap, -} - -/// The bytes collection type to output for Protobuf `bytes` fields. -#[non_exhaustive] -#[derive(Default, Clone, Copy, Debug, PartialEq)] -enum BytesType { - /// The [`alloc::collections::Vec::`] type. - #[default] - Vec, - /// The [`bytes::Bytes`] type. - Bytes, -} - /// Compile `.proto` files into Rust files during a Cargo build. /// /// The generated `.rs` files are written to the Cargo `OUT_DIR` directory, suitable for use with