1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::str;
use std::io::{Read, BufRead, BufReader};
use errors::Result;
use decoder::{decode_data_url, DecodedMap};
#[derive(PartialEq, Debug)]
pub enum SourceMapRef {
Ref(String),
LegacyRef(String),
Missing
}
impl SourceMapRef {
pub fn get_url(&self) -> Option<&str> {
match *self {
SourceMapRef::Ref(ref u) => Some(u.as_str()),
SourceMapRef::LegacyRef(ref u) => Some(u.as_str()),
SourceMapRef::Missing => None,
}
}
pub fn get_embedded_sourcemap(&self) -> Result<Option<DecodedMap>> {
if let Some(url) = self.get_url() {
if url.starts_with("data:") {
return Ok(Some(try!(decode_data_url(url))));
}
}
Ok(None)
}
}
pub fn locate_sourcemap_reference<R: Read>(rdr: R) -> Result<SourceMapRef> {
for line in BufReader::new(rdr).lines() {
let line = try!(line);
if line.starts_with("//# sourceMappingURL=") ||
line.starts_with("//@ sourceMappingURL=") {
let url = try!(str::from_utf8(&line.as_bytes()[21..])).trim().to_owned();
if line.starts_with("//@") {
return Ok(SourceMapRef::LegacyRef(url));
} else {
return Ok(SourceMapRef::Ref(url));
}
}
}
Ok(SourceMapRef::Missing)
}