Skip to content

Commit 99b1c09

Browse files
NicolappsConvex, Inc.
authored andcommitted
Implement Display for ComponentDefinitionPath (#42073)
Motivation: I want to use ComponentDefinitionPath in an error message in #42013. GitOrigin-RevId: 0862de573033363bca9d48d082f1517905f54d01
1 parent 84367e6 commit 99b1c09

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

crates/common/src/components/component_definition_path.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
fmt,
23
ops::Deref,
34
path::{
45
Component as PathComponent,
@@ -102,6 +103,12 @@ impl Deref for ComponentDefinitionPath {
102103
}
103104
}
104105

106+
impl fmt::Display for ComponentDefinitionPath {
107+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108+
write!(f, "{}", &**self)
109+
}
110+
}
111+
105112
impl From<ComponentDefinitionPath> for String {
106113
fn from(value: ComponentDefinitionPath) -> Self {
107114
value
@@ -139,3 +146,26 @@ impl proptest::arbitrary::Arbitrary for ComponentDefinitionPath {
139146
.boxed()
140147
}
141148
}
149+
150+
#[cfg(test)]
151+
mod tests {
152+
use super::*;
153+
154+
#[test]
155+
fn test_display_root_component() {
156+
let path = ComponentDefinitionPath::root();
157+
assert_eq!(path.to_string(), "");
158+
}
159+
160+
#[test]
161+
fn test_display_one_level() {
162+
let path: ComponentDefinitionPath = "component1".parse().unwrap();
163+
assert_eq!(path.to_string(), "component1");
164+
}
165+
166+
#[test]
167+
fn test_display_two_level() {
168+
let path: ComponentDefinitionPath = "component1/component2".parse().unwrap();
169+
assert_eq!(path.to_string(), "component1/component2");
170+
}
171+
}

0 commit comments

Comments
 (0)