Skip to content

Commit 18447da

Browse files
committed
init project
0 parents  commit 18447da

File tree

7 files changed

+72
-0
lines changed

7 files changed

+72
-0
lines changed

.cargo/config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[target.x86_64-apple-darwin]
2+
rustflags = [
3+
"-C", "link-arg=-undefined",
4+
"-C", "link-arg=dynamic_lookup",
5+
]

.editorconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[*]
2+
charset = utf-8
3+
end_of_line = lf
4+
insert_final_newline = true
5+
trim_trailing_whitespace = true
6+
indent_size = 2
7+
indent_style = space

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/target
2+
**/*.rs.bk
3+
Cargo.lock

Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "py-sourcemap"
3+
version = "0.1.0"
4+
authors = ["LongYinan <[email protected]>"]
5+
6+
[lib]
7+
name = "py_sourcemap"
8+
crate-type = ["cdylib"]
9+
10+
[dependencies]
11+
pyo3 = { version="0.4", features=["extension-module"] }
12+
source-map-mappings = { version="0.5" }

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# py-sourcemap
2+
A tiny [source-map-mappings](https://github.com/fitzgen/source-map-mappings) bindings for python using [PyO3](https://github.com/PyO3/pyo3)

src/lib.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#![feature(specialization)]
2+
3+
extern crate pyo3;
4+
extern crate source_map_mappings;
5+
6+
use std::fs::File;
7+
use std::io::{BufReader, Read};
8+
9+
use pyo3::prelude::*;
10+
use source_map_mappings::{Bias, Mappings, parse_mappings};
11+
12+
#[pyclass]
13+
struct SourcemapParser {
14+
parsed_map: Mappings<()>
15+
}
16+
17+
#[pymethods]
18+
impl SourcemapParser {
19+
#[new]
20+
fn __new__(obj: &PyRawObject, path: &str) -> PyResult<()> {
21+
let file = File::open(path).map_err(PyErr::from)?;
22+
let mut buffers = vec![];
23+
let mut reader = BufReader::new(file);
24+
reader.read_to_end(&mut buffers).map_err(PyErr::from)?;
25+
let mappings = parse_mappings(&buffers).map_err(|_| PyErr::new::<exc::TypeError, _>(format!("Parse Sourcemap failed: {}", path)))?;
26+
obj.init(|_| SourcemapParser {
27+
parsed_map: mappings,
28+
})
29+
}
30+
31+
fn original_location_for(&self, generated_line: u32, generated_column: u32) -> PyResult<(u32, u32)> {
32+
if let Some(mapping) = self.parsed_map.original_location_for(generated_line, generated_column, Bias::LeastUpperBound) {
33+
return Ok((mapping.generated_line, mapping.generated_column))
34+
}
35+
Err(PyErr::new::<exc::TypeError, _>("No sources found"))
36+
}
37+
}
38+
39+
#[pymodinit]
40+
fn py_sourcemap(_py: Python, m: &PyModule) -> PyResult<()> {
41+
m.add_class::<SourcemapParser>()?;
42+
Ok(())
43+
}

tests/lookup.py

Whitespace-only changes.

0 commit comments

Comments
 (0)