Skip to content

Commit

Permalink
prost_build: BytesType and MapType into a collections module.
Browse files Browse the repository at this point in the history
  • Loading branch information
gibbz00 committed Apr 14, 2024
1 parent bbdb402 commit c0953c4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 58 deletions.
36 changes: 0 additions & 36 deletions prost-build/src/code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>",
BytesType::Bytes => "::prost::bytes::Bytes",
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
57 changes: 57 additions & 0 deletions prost-build/src/collections.rs
Original file line number Diff line number Diff line change
@@ -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::<u8>`] 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<u8>",
BytesType::Bytes => "::prost::bytes::Bytes",
}
}
}
25 changes: 3 additions & 22 deletions prost-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<u8>`] 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
Expand Down

0 comments on commit c0953c4

Please sign in to comment.