|
1 |
| -use lazy_static::lazy_static; |
2 | 1 | use nom::branch::alt;
|
3 | 2 | use nom::bytes::complete::{tag, take_until1};
|
4 | 3 | use nom::combinator::{eof, map};
|
5 | 4 | use nom::error::ErrorKind;
|
6 | 5 | use nom::sequence::terminated;
|
7 | 6 | use nom::{Err, IResult};
|
8 |
| -use regex::Regex; |
9 | 7 |
|
10 | 8 | /// A special source file path for source files which are hosted online.
|
11 | 9 | ///
|
@@ -72,38 +70,9 @@ impl MappedPath {
|
72 | 70 |
|
73 | 71 | /// Detect some URLs of plain text files and convert them to a `MappedPath`.
|
74 | 72 | pub fn from_url(url: &str) -> Option<Self> {
|
75 |
| - lazy_static! { |
76 |
| - static ref GITHUB_REGEX: Regex = Regex::new(r"^https://raw\.githubusercontent\.com/(?P<repo>[^/]+/[^/]+)/(?P<rev>[^/]+)/(?P<path>.*)$").unwrap(); |
77 |
| - static ref HG_REGEX: Regex = Regex::new(r"^https://(?P<repo>hg\..+)/raw-file/(?P<rev>[0-9a-f]+)/(?P<path>.*)$").unwrap(); |
78 |
| - static ref S3_REGEX: Regex = Regex::new(r"^https://(?P<bucket>[^/]+).s3.amazonaws.com/(?P<digest>[^/]+)/(?P<path>.*)$").unwrap(); |
79 |
| - } |
80 |
| - if let Some(captures) = GITHUB_REGEX.captures(url) { |
81 |
| - // https://raw.githubusercontent.com/baldurk/renderdoc/v1.15/renderdoc/data/glsl/gl_texsample.h |
82 |
| - // -> "git:github.com/baldurk/renderdoc:renderdoc/data/glsl/gl_texsample.h:v1.15" |
83 |
| - let repo = format!("github.com/{}", captures.name("repo").unwrap().as_str()); |
84 |
| - let path = captures.name("path").unwrap().as_str().to_owned(); |
85 |
| - let rev = captures.name("rev").unwrap().as_str().to_owned(); |
86 |
| - Some(MappedPath::Git { repo, path, rev }) |
87 |
| - } else if let Some(captures) = HG_REGEX.captures(url) { |
88 |
| - // "https://hg.mozilla.org/mozilla-central/raw-file/1706d4d54ec68fae1280305b70a02cb24c16ff68/mozglue/baseprofiler/core/ProfilerBacktrace.cpp" |
89 |
| - // -> "hg:hg.mozilla.org/mozilla-central:mozglue/baseprofiler/core/ProfilerBacktrace.cpp:1706d4d54ec68fae1280305b70a02cb24c16ff68" |
90 |
| - let repo = captures.name("repo").unwrap().as_str().to_owned(); |
91 |
| - let path = captures.name("path").unwrap().as_str().to_owned(); |
92 |
| - let rev = captures.name("rev").unwrap().as_str().to_owned(); |
93 |
| - Some(MappedPath::Hg { repo, path, rev }) |
94 |
| - } else if let Some(captures) = S3_REGEX.captures(url) { |
95 |
| - // "https://gecko-generated-sources.s3.amazonaws.com/7a1db5dfd0061d0e0bcca227effb419a20439aef4f6c4e9cd391a9f136c6283e89043d62e63e7edbd63ad81c339c401092bcfeff80f74f9cae8217e072f0c6f3/x86_64-pc-windows-msvc/release/build/swgl-59e3a0e09f56f4ea/out/brush_solid_DEBUG_OVERDRAW.h" |
96 |
| - // -> "s3:gecko-generated-sources:7a1db5dfd0061d0e0bcca227effb419a20439aef4f6c4e9cd391a9f136c6283e89043d62e63e7edbd63ad81c339c401092bcfeff80f74f9cae8217e072f0c6f3/x86_64-pc-windows-msvc/release/build/swgl-59e3a0e09f56f4ea/out/brush_solid_DEBUG_OVERDRAW.h:" |
97 |
| - let bucket = captures.name("bucket").unwrap().as_str().to_owned(); |
98 |
| - let digest = captures.name("digest").unwrap().as_str().to_owned(); |
99 |
| - let path = captures.name("path").unwrap().as_str().to_owned(); |
100 |
| - Some(MappedPath::S3 { |
101 |
| - bucket, |
102 |
| - digest, |
103 |
| - path, |
104 |
| - }) |
105 |
| - } else { |
106 |
| - None |
| 73 | + match parse_url(url) { |
| 74 | + Ok((_, mapped_path)) => Some(mapped_path), |
| 75 | + Err(_) => None, |
107 | 76 | }
|
108 | 77 | }
|
109 | 78 |
|
@@ -191,7 +160,6 @@ fn cargo_path(input: &str) -> IResult<&str, (String, String, String, String)> {
|
191 | 160 | ))
|
192 | 161 | }
|
193 | 162 |
|
194 |
| -// Parse any of the line data that can occur in the body of a symbol file. |
195 | 163 | fn parse_special_path(input: &str) -> IResult<&str, MappedPath> {
|
196 | 164 | alt((
|
197 | 165 | map(git_path, |(repo, path, rev)| MappedPath::Git {
|
@@ -220,6 +188,70 @@ fn parse_special_path(input: &str) -> IResult<&str, MappedPath> {
|
220 | 188 | ))(input)
|
221 | 189 | }
|
222 | 190 |
|
| 191 | +fn github_url(input: &str) -> IResult<&str, (String, String, String)> { |
| 192 | + // Example: "https://raw.githubusercontent.com/baldurk/renderdoc/v1.15/renderdoc/data/glsl/gl_texsample.h" |
| 193 | + let (input, _) = tag("https://raw.githubusercontent.com/")(input)?; |
| 194 | + let (input, org) = terminated(take_until1("/"), tag("/"))(input)?; |
| 195 | + let (input, repo_name) = terminated(take_until1("/"), tag("/"))(input)?; |
| 196 | + let (input, rev) = terminated(take_until1("/"), tag("/"))(input)?; |
| 197 | + let path = input; |
| 198 | + Ok(( |
| 199 | + "", |
| 200 | + ( |
| 201 | + format!("github.com/{org}/{repo_name}"), |
| 202 | + path.to_owned(), |
| 203 | + rev.to_owned(), |
| 204 | + ), |
| 205 | + )) |
| 206 | +} |
| 207 | + |
| 208 | +fn hg_url(input: &str) -> IResult<&str, (String, String, String)> { |
| 209 | + // Example: "https://hg.mozilla.org/mozilla-central/raw-file/1706d4d54ec68fae1280305b70a02cb24c16ff68/mozglue/baseprofiler/core/ProfilerBacktrace.cpp" |
| 210 | + let (input, _) = tag("https://hg.")(input)?; |
| 211 | + let (input, host_rest) = terminated(take_until1("/"), tag("/"))(input)?; |
| 212 | + let (input, repo) = terminated(take_until1("/raw-file/"), tag("/raw-file/"))(input)?; |
| 213 | + let (input, rev) = terminated(take_until1("/"), tag("/"))(input)?; |
| 214 | + let path = input; |
| 215 | + Ok(( |
| 216 | + "", |
| 217 | + ( |
| 218 | + format!("hg.{host_rest}/{repo}"), |
| 219 | + path.to_owned(), |
| 220 | + rev.to_owned(), |
| 221 | + ), |
| 222 | + )) |
| 223 | +} |
| 224 | + |
| 225 | +fn s3_url(input: &str) -> IResult<&str, (String, String, String)> { |
| 226 | + // Example: "https://gecko-generated-sources.s3.amazonaws.com/7a1db5dfd0061d0e0bcca227effb419a20439aef4f6c4e9cd391a9f136c6283e89043d62e63e7edbd63ad81c339c401092bcfeff80f74f9cae8217e072f0c6f3/x86_64-pc-windows-msvc/release/build/swgl-59e3a0e09f56f4ea/out/brush_solid_DEBUG_OVERDRAW.h" |
| 227 | + let (input, _) = tag("https://")(input)?; |
| 228 | + let (input, bucket) = |
| 229 | + terminated(take_until1(".s3.amazonaws.com/"), tag(".s3.amazonaws.com/"))(input)?; |
| 230 | + let (input, digest) = terminated(take_until1("/"), tag("/"))(input)?; |
| 231 | + let path = input; |
| 232 | + Ok(("", (bucket.to_owned(), digest.to_owned(), path.to_owned()))) |
| 233 | +} |
| 234 | + |
| 235 | +fn parse_url(input: &str) -> IResult<&str, MappedPath> { |
| 236 | + alt(( |
| 237 | + map(github_url, |(repo, path, rev)| MappedPath::Git { |
| 238 | + repo, |
| 239 | + path, |
| 240 | + rev, |
| 241 | + }), |
| 242 | + map(hg_url, |(repo, path, rev)| MappedPath::Hg { |
| 243 | + repo, |
| 244 | + path, |
| 245 | + rev, |
| 246 | + }), |
| 247 | + map(s3_url, |(bucket, digest, path)| MappedPath::S3 { |
| 248 | + bucket, |
| 249 | + digest, |
| 250 | + path, |
| 251 | + }), |
| 252 | + ))(input) |
| 253 | +} |
| 254 | + |
223 | 255 | #[cfg(test)]
|
224 | 256 | mod test {
|
225 | 257 | use super::*;
|
|
0 commit comments