hosted_git_info/parser/
github.rs1use 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 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 let Some(type_) = type_ {
38 if type_ != "tree" {
39 return Err(ParseError::UnknownUrl);
40 }
41 } else {
42 committish = url.fragment();
43 }
44
45 let project = project.map(|project| project.strip_suffix(".git").unwrap_or(project));
49
50 if user.is_none() || project.is_none() {
54 return Err(ParseError::UnknownUrl);
55 }
56
57 Ok(ParsedSegments {
59 user,
60 project,
61 committish,
62 })
63 }
64}