use std::str::Split;
use crate::Error;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct GitUrl {
inner: git_url_parse::GitUrl,
url_str: String,
}
impl GitUrl {
pub fn parse(s: &str) -> Result<Self, Error> {
let url = git_url_parse::GitUrl::parse(s).map_err(Error::GitUrlError)?;
Ok(Self {
inner: url,
url_str: s.to_string(),
})
}
pub fn raw(&self) -> &str {
&self.url_str
}
pub fn scheme(&self) -> Option<&str> {
self.inner.scheme()
}
pub fn path(&self) -> &str {
self.inner.path()
}
pub fn split_path(&self) -> Split<'_, char> {
self.inner.path().trim_start_matches('/').split('/')
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_parse() {
assert!(GitUrl::parse("https://server.example/owner/git-stratum.git").is_ok());
assert!(GitUrl::parse("git@server.example:owner/git-stratum.git").is_ok());
assert!(GitUrl::parse("rubbish-url$@./").is_err());
assert!(GitUrl::parse("https://server.example").is_err());
}
#[test]
fn test_raw() {
let raw = "https://server.example/owner/git-stratum.git";
let url = GitUrl::parse(raw).unwrap();
assert_eq!(url.raw(), raw)
}
#[test]
fn test_scheme() {
let raw = "https://server.example/owner/git-stratum.git";
let url = GitUrl::parse(raw).unwrap();
assert_eq!(url.scheme(), Some("https"))
}
#[test]
fn test_path() {
let raw = "https://server.example/owner/git-stratum.git";
let url = GitUrl::parse(raw).unwrap();
assert_eq!(url.path(), "/owner/git-stratum.git")
}
#[test]
fn test_split_path() {
let raw = "https://server.example/owner/git-stratum.git";
let url = GitUrl::parse(raw).unwrap();
let path_components: Vec<&str> = url.split_path().collect();
let expected_components = vec!["owner", "git-stratum.git"];
assert_eq!(path_components, expected_components)
}
}