2
2
3
3
extern crate pyo3;
4
4
extern crate source_map_mappings;
5
- #[ macro_use] extern crate serde_derive;
5
+ #[ macro_use]
6
+ extern crate serde_derive;
6
7
extern crate serde;
7
8
extern crate serde_json;
8
9
9
10
use std:: fs:: File ;
10
11
11
12
use pyo3:: prelude:: * ;
12
- use source_map_mappings:: { Bias , Mapping , Mappings , parse_mappings } ;
13
+ use source_map_mappings:: { parse_mappings , Bias , Mapping , Mappings } ;
13
14
14
15
#[ derive( Serialize , Deserialize , Debug ) ]
15
16
#[ serde( rename_all = "camelCase" ) ]
@@ -34,13 +35,11 @@ impl SourcemapParser {
34
35
#[ new]
35
36
fn __new__ ( obj : & PyRawObject , path : & str ) -> PyResult < ( ) > {
36
37
let file = File :: open ( path) . map_err ( PyErr :: from) ?;
37
- let raw_sourcemap: RawSourceMap = serde_json:: from_reader ( file) . map_err ( |e| {
38
- PyErr :: new :: < exc:: TypeError , _ > ( format ! ( "{:?}" , e) )
39
- } ) ?;
38
+ let raw_sourcemap: RawSourceMap = serde_json:: from_reader ( file)
39
+ . map_err ( |e| PyErr :: new :: < exc:: TypeError , _ > ( format ! ( "{:?}" , e) ) ) ?;
40
40
let mapping_bytes = raw_sourcemap. mappings . as_bytes ( ) ;
41
- let mappings = parse_mappings ( mapping_bytes) . map_err ( |_| {
42
- PyErr :: new :: < exc:: TypeError , _ > ( format ! ( "Parse Sourcemap failed: {}" , path) )
43
- } ) ?;
41
+ let mappings = parse_mappings ( mapping_bytes)
42
+ . map_err ( |_| PyErr :: new :: < exc:: TypeError , _ > ( format ! ( "Parse Sourcemap failed: {}" , path) ) ) ?;
44
43
let sources = raw_sourcemap. sources ;
45
44
let names = raw_sourcemap. names ;
46
45
obj. init ( move |_| SourcemapParser {
@@ -50,17 +49,36 @@ impl SourcemapParser {
50
49
} )
51
50
}
52
51
53
- fn original_location_for ( & self , generated_line : u32 , generated_column : u32 ) -> PyResult < ( u32 , u32 , Option < String > , Option < String > ) > {
54
- if let Some ( Mapping { original, .. } ) = self . parsed_map . original_location_for ( generated_line, generated_column, Bias :: LeastUpperBound ) {
52
+ fn original_location_for (
53
+ & self ,
54
+ generated_line : u32 ,
55
+ generated_column : u32 ,
56
+ ) -> PyResult < ( u32 , u32 , Option < String > , Option < String > ) > {
57
+ if let Some ( Mapping { original, .. } ) =
58
+ self
59
+ . parsed_map
60
+ . original_location_for ( generated_line, generated_column, Bias :: LeastUpperBound )
61
+ {
55
62
match original {
56
63
Some ( location) => {
57
64
let name = location. name . and_then ( |index| {
58
- self . names . get ( index as usize ) . map ( |str_slice| str_slice. to_string ( ) )
65
+ self
66
+ . names
67
+ . get ( index as usize )
68
+ . map ( |str_slice| str_slice. to_string ( ) )
59
69
} ) ;
60
- let source = self . sources . get ( location. source as usize ) . map ( |str_slice| str_slice. to_string ( ) ) ;
61
- return Ok ( ( location. original_line , location. original_column , source, name) )
62
- } ,
63
- None => return Err ( PyErr :: new :: < exc:: TypeError , _ > ( "No original lines" ) )
70
+ let source = self
71
+ . sources
72
+ . get ( location. source as usize )
73
+ . map ( |str_slice| str_slice. to_string ( ) ) ;
74
+ return Ok ( (
75
+ location. original_line + 1 ,
76
+ location. original_column + 1 ,
77
+ source,
78
+ name,
79
+ ) ) ;
80
+ }
81
+ None => return Err ( PyErr :: new :: < exc:: TypeError , _ > ( "No original lines" ) ) ,
64
82
} ;
65
83
}
66
84
Err ( PyErr :: new :: < exc:: TypeError , _ > ( "No sources found" ) )
0 commit comments