refactor: move slash prefix from SlashedPath

turns out we shouldn't have it on the http version, so perhaps its more
clear this way.
This commit is contained in:
Joonas Koivunen 2020-08-22 22:40:35 +03:00
parent fd4580f837
commit 08782f9b58

View File

@ -97,7 +97,13 @@ impl IpfsPath {
impl fmt::Display for IpfsPath {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}{}", self.root, self.path)
write!(fmt, "{}", self.root)?;
if !self.path.is_empty() {
// slash is not included in the <SlashedPath as fmt::Display>::fmt impl as we need to,
// serialize it later in json *without* one
write!(fmt, "/{}", self.path)?;
}
Ok(())
}
}
@ -189,6 +195,10 @@ impl SlashedPath {
self.path.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
// FIXME: remove this
pub fn path(&self) -> &[String] {
&self.path
@ -197,7 +207,16 @@ impl SlashedPath {
impl fmt::Display for SlashedPath {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
self.path.iter().try_for_each(|s| write!(fmt, "/{}", s))
let mut first = true;
self.path.iter().try_for_each(move |s| {
if first {
first = false;
} else {
write!(fmt, "/")?;
}
write!(fmt, "{}", s)
})
}
}