Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prost_build: BytesType and MapType into a collections module. #1030

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading