1use std::str::Split;
2
3use crate::Error;
4
5#[derive(Debug, Clone, Default, PartialEq, Eq)]
10pub struct GitUrl {
11 inner: git_url_parse::GitUrl,
12 url_str: String,
13}
14
15impl GitUrl {
16 pub fn parse(s: &str) -> Result<Self, Error> {
18 let url = git_url_parse::GitUrl::parse(s).map_err(Error::GitUrlError)?;
19 Ok(Self {
20 inner: url,
21 url_str: s.to_string(),
22 })
23 }
24
25 pub fn raw(&self) -> &str {
27 &self.url_str
28 }
29
30 pub fn scheme(&self) -> Option<&str> {
32 self.inner.scheme()
33 }
34
35 pub fn path(&self) -> &str {
37 self.inner.path()
38 }
39
40 pub fn split_path(&self) -> Split<'_, char> {
42 self.inner.path().trim_start_matches('/').split('/')
43 }
44}
45
46#[cfg(test)]
47mod test {
48 use super::*;
49
50 #[test]
51 fn test_parse() {
52 assert!(GitUrl::parse("https://server.example/owner/git-stratum.git").is_ok());
53 assert!(GitUrl::parse("git@server.example:owner/git-stratum.git").is_ok());
54 assert!(GitUrl::parse("rubbish-url$@./").is_err());
55 assert!(GitUrl::parse("https://server.example").is_err());
56 }
57
58 #[test]
59 fn test_raw() {
60 let raw = "https://server.example/owner/git-stratum.git";
61 let url = GitUrl::parse(raw).unwrap();
62
63 assert_eq!(url.raw(), raw)
64 }
65
66 #[test]
67 fn test_scheme() {
68 let raw = "https://server.example/owner/git-stratum.git";
69 let url = GitUrl::parse(raw).unwrap();
70
71 assert_eq!(url.scheme(), Some("https"))
72 }
73
74 #[test]
75 fn test_path() {
76 let raw = "https://server.example/owner/git-stratum.git";
77 let url = GitUrl::parse(raw).unwrap();
78
79 assert_eq!(url.path(), "/owner/git-stratum.git")
80 }
81
82 #[test]
83 fn test_split_path() {
84 let raw = "https://server.example/owner/git-stratum.git";
85 let url = GitUrl::parse(raw).unwrap();
86
87 let path_components: Vec<&str> = url.split_path().collect();
88 let expected_components = vec!["owner", "git-stratum.git"];
89 assert_eq!(path_components, expected_components)
90 }
91}