1
+ use lazy_static:: lazy_static;
1
2
use nom:: branch:: alt;
2
3
use nom:: bytes:: complete:: { tag, take_until1} ;
3
4
use nom:: combinator:: { eof, map} ;
4
5
use nom:: error:: ErrorKind ;
5
6
use nom:: sequence:: terminated;
6
7
use nom:: { Err , IResult } ;
8
+ use regex:: Regex ;
7
9
8
10
/// A special source file path for source files which are hosted online.
9
11
///
@@ -68,6 +70,43 @@ impl MappedPath {
68
70
}
69
71
}
70
72
73
+ /// Detect some URLs of plain text files and convert them to a `MappedPath`.
74
+ 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
107
+ }
108
+ }
109
+
71
110
/// Serialize this mapped path to a string, using the "special path" syntax.
72
111
pub fn to_special_path_str ( & self ) -> String {
73
112
match self {
@@ -197,6 +236,16 @@ mod test {
197
236
rev: "997f00815e6bc28806b75448c8829f0259d2cb28" . to_string( ) ,
198
237
} )
199
238
) ;
239
+ assert_eq ! (
240
+ MappedPath :: from_url(
241
+ "https://hg.mozilla.org/mozilla-central/raw-file/1706d4d54ec68fae1280305b70a02cb24c16ff68/mozglue/baseprofiler/core/ProfilerBacktrace.cpp"
242
+ ) ,
243
+ Some ( MappedPath :: Hg {
244
+ repo: "hg.mozilla.org/mozilla-central" . to_string( ) ,
245
+ path: "mozglue/baseprofiler/core/ProfilerBacktrace.cpp" . to_string( ) ,
246
+ rev: "1706d4d54ec68fae1280305b70a02cb24c16ff68" . to_string( ) ,
247
+ } )
248
+ ) ;
200
249
}
201
250
202
251
#[ test]
@@ -231,6 +280,16 @@ mod test {
231
280
rev: "dab1161c861cc239e48a17e1a5d729aa12785a53" . to_string( ) ,
232
281
} )
233
282
) ;
283
+ assert_eq ! (
284
+ MappedPath :: from_url(
285
+ "https://raw.githubusercontent.com/baldurk/renderdoc/v1.15/renderdoc/data/glsl/gl_texsample.h"
286
+ ) ,
287
+ Some ( MappedPath :: Git {
288
+ repo: "github.com/baldurk/renderdoc" . to_string( ) ,
289
+ path: "renderdoc/data/glsl/gl_texsample.h" . to_string( ) ,
290
+ rev: "v1.15" . to_string( ) ,
291
+ } )
292
+ ) ;
234
293
}
235
294
236
295
#[ test]
@@ -256,6 +315,16 @@ mod test {
256
315
digest: "4fd754dd7ca7565035aaa3357b8cd99959a2dddceba0fc2f7018ef99fd78ea63d03f9bf928afdc29873089ee15431956791130b97f66ab8fcb88ec75f4ba6b04" . to_string( ) ,
257
316
} )
258
317
) ;
318
+ assert_eq ! (
319
+ MappedPath :: from_url(
320
+ "https://gecko-generated-sources.s3.amazonaws.com/7a1db5dfd0061d0e0bcca227effb419a20439aef4f6c4e9cd391a9f136c6283e89043d62e63e7edbd63ad81c339c401092bcfeff80f74f9cae8217e072f0c6f3/x86_64-pc-windows-msvc/release/build/swgl-59e3a0e09f56f4ea/out/brush_solid_DEBUG_OVERDRAW.h"
321
+ ) ,
322
+ Some ( MappedPath :: S3 {
323
+ bucket: "gecko-generated-sources" . to_string( ) ,
324
+ path: "x86_64-pc-windows-msvc/release/build/swgl-59e3a0e09f56f4ea/out/brush_solid_DEBUG_OVERDRAW.h" . to_string( ) ,
325
+ digest: "7a1db5dfd0061d0e0bcca227effb419a20439aef4f6c4e9cd391a9f136c6283e89043d62e63e7edbd63ad81c339c401092bcfeff80f74f9cae8217e072f0c6f3" . to_string( ) ,
326
+ } )
327
+ ) ;
259
328
}
260
329
261
330
#[ test]
0 commit comments