Add `ToString` support for transport URLs

Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
Alexis Sellier 2022-10-24 21:57:53 +02:00
parent 66493422c2
commit 3407c6c017
No known key found for this signature in database
2 changed files with 41 additions and 0 deletions

View File

@ -1,4 +1,5 @@
//! Git local transport URLs.
use std::fmt;
use std::str::FromStr;
use thiserror::Error;
@ -47,6 +48,16 @@ impl Url {
pub const SCHEME: &str = "rad";
}
impl fmt::Display for Url {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(ns) = self.namespace {
write!(f, "{}://{}/{}", Self::SCHEME, self.repo, ns)
} else {
write!(f, "{}://{}", Self::SCHEME, self.repo)
}
}
}
impl FromStr for Url {
type Err = UrlError;
@ -103,4 +114,23 @@ mod test {
.parse::<Url>()
.is_err());
}
#[test]
fn test_url_to_string() {
let repo = Id::from_str("z2w8RArM3gaBXZxXhQUswE3hhLcss").unwrap();
let namespace =
Namespace::from_str("z6Mkifeb5NPS6j7JP72kEQEeuqMTpCAVcHsJi1C86jGTzHRi").unwrap();
let url = Url {
repo,
namespace: None,
};
assert_eq!(url.to_string(), format!("rad://{repo}"));
let url = Url {
repo,
namespace: Some(namespace),
};
assert_eq!(url.to_string(), format!("rad://{repo}/{namespace}"));
}
}

View File

@ -1,4 +1,5 @@
//! Git remote transport URLs.
use std::fmt;
use std::str::FromStr;
use thiserror::Error;
@ -49,6 +50,16 @@ impl Url {
pub const SCHEME: &str = "heartwood";
}
impl fmt::Display for Url {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(ns) = self.namespace {
write!(f, "{}://{}/{}/{}", Self::SCHEME, self.node, self.repo, ns)
} else {
write!(f, "{}://{}/{}", Self::SCHEME, self.node, self.repo)
}
}
}
impl FromStr for Url {
type Err = UrlError;