From 3407c6c017ab959e1892904443e905f9de6ac61d Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Mon, 24 Oct 2022 21:57:53 +0200 Subject: [PATCH] Add `ToString` support for transport URLs Signed-off-by: Alexis Sellier --- .../src/storage/git/transport/local/url.rs | 30 +++++++++++++++++++ .../src/storage/git/transport/remote/url.rs | 11 +++++++ 2 files changed, 41 insertions(+) diff --git a/radicle/src/storage/git/transport/local/url.rs b/radicle/src/storage/git/transport/local/url.rs index 4e86e511..9efb1652 100644 --- a/radicle/src/storage/git/transport/local/url.rs +++ b/radicle/src/storage/git/transport/local/url.rs @@ -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::() .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}")); + } } diff --git a/radicle/src/storage/git/transport/remote/url.rs b/radicle/src/storage/git/transport/remote/url.rs index d6982c1a..1f0e718a 100644 --- a/radicle/src/storage/git/transport/remote/url.rs +++ b/radicle/src/storage/git/transport/remote/url.rs @@ -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;