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 20, 2024
1 parent 0381b93 commit 6efcdb4
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 59 deletions.
38 changes: 1 addition & 37 deletions prost-build/src/code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::ast::{Comments, Method, Service};
use crate::extern_paths::ExternPaths;
use crate::ident::{strip_enum_prefix, to_snake, to_upper_camel};
use crate::message_graph::MessageGraph;
use crate::{BytesType, Config, MapType};
use crate::Config;

mod c_escaping;
use c_escaping::unescape_c_escape_string;
Expand Down Expand Up @@ -1123,39 +1123,3 @@ 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",
}
}
}
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 6efcdb4

Please sign in to comment.