Skip to content

Commit f3e788a

Browse files
authored
feat: implement Ord and PartialOrd for Module (#611)
1 parent fc9fbd9 commit f3e788a

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

prost-build/src/lib.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ impl fmt::Debug for Config {
10611061
}
10621062

10631063
/// A Rust module path for a Protobuf package.
1064-
#[derive(Clone, PartialEq, Eq, Hash)]
1064+
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
10651065
pub struct Module {
10661066
components: Vec<String>,
10671067
}
@@ -1092,7 +1092,7 @@ impl Module {
10921092
}
10931093
}
10941094

1095-
/// An iterator over the parts of the path
1095+
/// An iterator over the parts of the path.
10961096
pub fn parts(&self) -> impl Iterator<Item = &str> {
10971097
self.components.iter().map(|s| s.as_str())
10981098
}
@@ -1117,6 +1117,11 @@ impl Module {
11171117
self.components.len()
11181118
}
11191119

1120+
/// Whether the module's path contains any components.
1121+
pub fn is_empty(&self) -> bool {
1122+
self.components.is_empty()
1123+
}
1124+
11201125
fn to_partial_file_name(&self, range: RangeToInclusive<usize>) -> String {
11211126
self.components[range].join(".")
11221127
}
@@ -1126,6 +1131,20 @@ impl Module {
11261131
}
11271132
}
11281133

1134+
impl fmt::Display for Module {
1135+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1136+
let mut parts = self.parts();
1137+
if let Some(first) = parts.next() {
1138+
f.write_str(first)?;
1139+
}
1140+
for part in parts {
1141+
f.write_str("::")?;
1142+
f.write_str(part)?;
1143+
}
1144+
Ok(())
1145+
}
1146+
}
1147+
11291148
/// Compile `.proto` files into Rust files during a Cargo build.
11301149
///
11311150
/// The generated `.rs` files are written to the Cargo `OUT_DIR` directory, suitable for use with

0 commit comments

Comments
 (0)