hosted_git_info/parser/
github.rs

1use super::{ParsedSegments, Parser};
2use crate::{ParseError, Provider};
3use std::str;
4use url::Url;
5
6#[derive(Debug, Eq, PartialEq)]
7pub struct GitHubParser {}
8
9impl Parser for GitHubParser {
10    fn provider(&self) -> Provider {
11        Provider::GitHub
12    }
13
14    fn supports_scheme(&self, scheme: &str) -> bool {
15        matches!(
16            scheme,
17            "git" | "http" | "git+ssh" | "git+https" | "ssh" | "https"
18        )
19    }
20
21    fn extract<'a>(&self, url: &'a Url) -> Result<ParsedSegments<'a>, ParseError> {
22        // let [, user, project, type, committish] = url.pathname.split('/', 5)
23        let mut path_segments = url.path().splitn(5, '/');
24        let _ = path_segments.next();
25        let user = path_segments.next();
26        let project = path_segments.next();
27        let type_ = path_segments.next();
28        let mut committish = path_segments.next();
29
30        // if (type && type !== 'tree') {
31        //   return
32        // }
33        //
34        // if (!type) {
35        //   committish = url.hash.slice(1)
36        // }
37        if let Some(type_) = type_ {
38            if type_ != "tree" {
39                return Err(ParseError::UnknownUrl);
40            }
41        } else {
42            committish = url.fragment();
43        }
44
45        // if (project && project.endsWith('.git')) {
46        //   project = project.slice(0, -4)
47        // }
48        let project = project.map(|project| project.strip_suffix(".git").unwrap_or(project));
49
50        // if (!user || !project) {
51        //   return
52        // }
53        if user.is_none() || project.is_none() {
54            return Err(ParseError::UnknownUrl);
55        }
56
57        // return { user, project, committish }
58        Ok(ParsedSegments {
59            user,
60            project,
61            committish,
62        })
63    }
64}