Skip to content

Commit 9f34132

Browse files
authored
refactor(path): simplify Display implementations for InterfacePath (#50)
2 parents 3707f30 + 845ccc8 commit 9f34132

File tree

1 file changed

+48
-26
lines changed

1 file changed

+48
-26
lines changed

src/path.rs

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ impl From<ForeignInterfacePath> for InterfacePath {
4848

4949
impl Display for ForeignInterfacePath {
5050
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51-
if let Some(ref version) = self.version {
52-
write!(
53-
f,
54-
"{}/{}@{}",
55-
self.package_name, self.interface_name, version
56-
)
57-
} else {
58-
write!(f, "{}/{}", self.package_name, self.interface_name)
59-
}
51+
write!(
52+
f,
53+
"{}/{}{}",
54+
self.package_name,
55+
self.interface_name,
56+
self.version
57+
.as_ref()
58+
.map_or_else(|| "".to_string(), |v| format!("@{v}"))
59+
)
6060
}
6161
}
6262

@@ -152,22 +152,15 @@ impl FromStr for InterfacePath {
152152

153153
impl Display for InterfacePath {
154154
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
155-
if let Some(ref version) = self.version {
156-
write!(
157-
f,
158-
"{}/{}@{}",
159-
self.package_name.as_ref().unwrap_or(&"".to_string()),
160-
self.interface_name,
161-
version
162-
)
163-
} else {
164-
write!(
165-
f,
166-
"{}/{}",
167-
self.package_name.as_ref().unwrap_or(&"".to_string()),
168-
self.interface_name
169-
)
170-
}
155+
write!(
156+
f,
157+
"{}/{}{}",
158+
self.package_name.as_ref().unwrap_or(&"".to_string()),
159+
self.interface_name,
160+
self.version
161+
.as_ref()
162+
.map_or_else(|| "".to_string(), |v| format!("@{v}")),
163+
)
171164
}
172165
}
173166

@@ -184,10 +177,39 @@ pub enum InterfacePathParseError {
184177
#[cfg(test)]
185178
mod tests {
186179
use super::*;
180+
const PACKAGE: &str = "package_name/interface_name@1.0.0";
181+
182+
#[test]
183+
fn test_path_display() {
184+
let path = InterfacePath::from_str(PACKAGE).unwrap();
185+
assert_eq!(PACKAGE, format!("{path}"));
186+
187+
let foreign_path = path.clone().into_foreign().unwrap();
188+
assert_eq!(PACKAGE, format!("{foreign_path}"));
189+
190+
assert_eq!(format!("{path}"), format!("{foreign_path}"));
191+
}
192+
193+
#[test]
194+
fn test_interface_path_roundtrip() {
195+
// Convert to ForeignInterfacePath and back
196+
let path = InterfacePath::from_str(PACKAGE).unwrap();
197+
assert_eq!(path, path.clone().into_foreign().unwrap().into());
198+
199+
// Parse the string representation back into InterfacePath
200+
assert_eq!(
201+
path,
202+
InterfacePath::new(
203+
path.package_name().map(String::from),
204+
path.interface_name().to_string(),
205+
path.version().cloned(),
206+
)
207+
);
208+
}
187209

188210
#[test]
189211
fn test_interface_path_parsing() {
190-
let path = InterfacePath::from_str("package_name/interface_name@1.0.0").unwrap();
212+
let path = InterfacePath::from_str(PACKAGE).unwrap();
191213
assert_eq!(path.package_name(), Some("package_name"));
192214
assert_eq!(path.interface_name(), "interface_name");
193215
assert_eq!(path.version(), Some(&Version::parse("1.0.0").unwrap()));

0 commit comments

Comments
 (0)