Add `ToString` support for transport URLs
Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
parent
66493422c2
commit
3407c6c017
|
|
@ -1,4 +1,5 @@
|
||||||
//! Git local transport URLs.
|
//! Git local transport URLs.
|
||||||
|
use std::fmt;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
@ -47,6 +48,16 @@ impl Url {
|
||||||
pub const SCHEME: &str = "rad";
|
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 {
|
impl FromStr for Url {
|
||||||
type Err = UrlError;
|
type Err = UrlError;
|
||||||
|
|
||||||
|
|
@ -103,4 +114,23 @@ mod test {
|
||||||
.parse::<Url>()
|
.parse::<Url>()
|
||||||
.is_err());
|
.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}"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
//! Git remote transport URLs.
|
//! Git remote transport URLs.
|
||||||
|
use std::fmt;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
@ -49,6 +50,16 @@ impl Url {
|
||||||
pub const SCHEME: &str = "heartwood";
|
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 {
|
impl FromStr for Url {
|
||||||
type Err = UrlError;
|
type Err = UrlError;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue