Skip to content

Commit

Permalink
feat: implement Ord and PartialOrd for Module (#611)
Browse files Browse the repository at this point in the history
  • Loading branch information
neoeinstein authored Mar 28, 2022
1 parent fc9fbd9 commit f3e788a
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions prost-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ impl fmt::Debug for Config {
}

/// A Rust module path for a Protobuf package.
#[derive(Clone, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Module {
components: Vec<String>,
}
Expand Down Expand Up @@ -1092,7 +1092,7 @@ impl Module {
}
}

/// An iterator over the parts of the path
/// An iterator over the parts of the path.
pub fn parts(&self) -> impl Iterator<Item = &str> {
self.components.iter().map(|s| s.as_str())
}
Expand All @@ -1117,6 +1117,11 @@ impl Module {
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<usize>) -> String {
self.components[range].join(".")
}
Expand All @@ -1126,6 +1131,20 @@ impl Module {
}
}

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
Expand Down

0 comments on commit f3e788a

Please sign in to comment.