Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
Add `ToString` support for transport URLs
Alexis Sellier committed 3 years ago
commit 3407c6c017ab959e1892904443e905f9de6ac61d
parent 66493422c2d1f99d5ff9210f3ee8e72eb725a815
2 files changed +41 -0
modified 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::<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}"));
+
    }
}
modified 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;