From d23f2c56cafd8e249a4baffc5f3982ec1d5be6be Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Fri, 29 Mar 2024 17:48:27 +0100 Subject: [PATCH] prost-build: Split `Module` into a separate module. --- prost-build/src/lib.rs | 94 ++------------------------------------- prost-build/src/module.rs | 89 ++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 90 deletions(-) create mode 100644 prost-build/src/module.rs diff --git a/prost-build/src/lib.rs b/prost-build/src/lib.rs index 0814afbae..5375ddabd 100644 --- a/prost-build/src/lib.rs +++ b/prost-build/src/lib.rs @@ -127,9 +127,7 @@ //! //! [`protobuf-src`]: https://docs.rs/protobuf-src -use std::fmt; use std::io::Result; -use std::ops::RangeToInclusive; use std::path::Path; use prost_types::FileDescriptorSet; @@ -139,10 +137,7 @@ pub use crate::ast::{Comments, Method, Service}; mod code_generator; mod extern_paths; - mod ident; -use crate::ident::to_snake; - mod message_graph; mod path; @@ -155,6 +150,10 @@ pub use config::{ error_message_protoc_not_found, protoc_from_env, protoc_include_from_env, Config, }; +mod module; +// HELP(gibbz00): I think `Module` should be made pub(crate). +pub use module::Module; + /// A service generator takes a service descriptor and generates Rust code. /// /// `ServiceGenerator` can be used to generate application-specific interfaces @@ -222,91 +221,6 @@ enum BytesType { Bytes, } -/// A Rust module path for a Protobuf package. -#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] -pub struct Module { - components: Vec, -} - -impl Module { - /// Construct a module path from an iterator of parts. - pub fn from_parts(parts: I) -> Self - where - I: IntoIterator, - I::Item: Into, - { - Self { - components: parts.into_iter().map(|s| s.into()).collect(), - } - } - - /// Construct a module path from a Protobuf package name. - /// - /// Constituent parts are automatically converted to snake case in order to follow - /// Rust module naming conventions. - pub fn from_protobuf_package_name(name: &str) -> Self { - Self { - components: name - .split('.') - .filter(|s| !s.is_empty()) - .map(to_snake) - .collect(), - } - } - - /// An iterator over the parts of the path. - pub fn parts(&self) -> impl Iterator { - self.components.iter().map(|s| s.as_str()) - } - - /// Format the module path into a filename for generated Rust code. - /// - /// If the module path is empty, `default` is used to provide the root of the filename. - pub fn to_file_name_or(&self, default: &str) -> String { - let mut root = if self.components.is_empty() { - default.to_owned() - } else { - self.components.join(".") - }; - - root.push_str(".rs"); - - root - } - - /// The number of parts in the module's path. - pub fn len(&self) -> usize { - self.components.len() - } - - /// Whether the module's path contains any components. - pub fn is_empty(&self) -> bool { - self.components.is_empty() - } - - fn to_partial_file_name(&self, range: RangeToInclusive) -> String { - self.components[range].join(".") - } - - fn part(&self, idx: usize) -> &str { - self.components[idx].as_str() - } -} - -impl fmt::Display for Module { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let mut parts = self.parts(); - if let Some(first) = parts.next() { - f.write_str(first)?; - } - for part in parts { - f.write_str("::")?; - f.write_str(part)?; - } - Ok(()) - } -} - /// 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 diff --git a/prost-build/src/module.rs b/prost-build/src/module.rs new file mode 100644 index 000000000..21cab1163 --- /dev/null +++ b/prost-build/src/module.rs @@ -0,0 +1,89 @@ +use std::fmt; +use std::ops::RangeToInclusive; + +use crate::ident::to_snake; + +/// A Rust module path for a Protobuf package. +#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct Module { + components: Vec, +} + +impl Module { + /// Construct a module path from an iterator of parts. + pub fn from_parts(parts: I) -> Self + where + I: IntoIterator, + I::Item: Into, + { + Self { + components: parts.into_iter().map(|s| s.into()).collect(), + } + } + + /// Construct a module path from a Protobuf package name. + /// + /// Constituent parts are automatically converted to snake case in order to follow + /// Rust module naming conventions. + pub fn from_protobuf_package_name(name: &str) -> Self { + Self { + components: name + .split('.') + .filter(|s| !s.is_empty()) + .map(to_snake) + .collect(), + } + } + + /// An iterator over the parts of the path. + pub fn parts(&self) -> impl Iterator { + self.components.iter().map(|s| s.as_str()) + } + + /// Format the module path into a filename for generated Rust code. + /// + /// If the module path is empty, `default` is used to provide the root of the filename. + pub fn to_file_name_or(&self, default: &str) -> String { + let mut root = if self.components.is_empty() { + default.to_owned() + } else { + self.components.join(".") + }; + + root.push_str(".rs"); + + root + } + + /// The number of parts in the module's path. + pub fn len(&self) -> usize { + self.components.len() + } + + /// Whether the module's path contains any components. + pub fn is_empty(&self) -> bool { + self.components.is_empty() + } + + pub(crate) fn to_partial_file_name(&self, range: RangeToInclusive) -> String { + self.components[range].join(".") + } + + pub(crate) fn part(&self, idx: usize) -> &str { + self.components[idx].as_str() + } +} + +impl fmt::Display for Module { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let mut parts = self.parts(); + if let Some(first) = parts.next() { + f.write_str(first)?; + } + for part in parts { + f.write_str("::")?; + f.write_str(part)?; + } + Ok(()) + } +}