22
33extern crate pyo3;
44extern crate source_map_mappings;
5- #[ macro_use] extern crate serde_derive;
5+ #[ macro_use]
6+ extern crate serde_derive;
67extern crate serde;
78extern crate serde_json;
89
910use std:: fs:: File ;
1011
1112use pyo3:: prelude:: * ;
12- use source_map_mappings:: { Bias , Mapping , Mappings , parse_mappings } ;
13+ use source_map_mappings:: { parse_mappings , Bias , Mapping , Mappings } ;
1314
1415#[ derive( Serialize , Deserialize , Debug ) ]
1516#[ serde( rename_all = "camelCase" ) ]
@@ -34,13 +35,11 @@ impl SourcemapParser {
3435 #[ new]
3536 fn __new__ ( obj : & PyRawObject , path : & str ) -> PyResult < ( ) > {
3637 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) ) ) ?;
4040 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) ) ) ?;
4443 let sources = raw_sourcemap. sources ;
4544 let names = raw_sourcemap. names ;
4645 obj. init ( move |_| SourcemapParser {
@@ -50,17 +49,36 @@ impl SourcemapParser {
5049 } )
5150 }
5251
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+ {
5562 match original {
5663 Some ( location) => {
5764 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 ( ) )
5969 } ) ;
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" ) ) ,
6482 } ;
6583 }
6684 Err ( PyErr :: new :: < exc:: TypeError , _ > ( "No sources found" ) )
0 commit comments