Use unindent crate instead of _indoc_runtime
This commit is contained in:
parent
f642dbccb2
commit
503ec1a061
|
@ -25,8 +25,9 @@ num-traits = "0.2.6"
|
||||||
pyo3cls = { path = "pyo3cls", version = "=0.7.0" }
|
pyo3cls = { path = "pyo3cls", version = "=0.7.0" }
|
||||||
mashup = "0.1.9"
|
mashup = "0.1.9"
|
||||||
num-complex = { version = "0.2.1", optional = true }
|
num-complex = { version = "0.2.1", optional = true }
|
||||||
indoc = "0.3.3"
|
|
||||||
inventory = "0.1.3"
|
inventory = "0.1.3"
|
||||||
|
indoc = "0.3.3"
|
||||||
|
unindent = "0.1.3"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
assert_approx_eq = "1.1.0"
|
assert_approx_eq = "1.1.0"
|
||||||
|
|
55
src/lib.rs
55
src/lib.rs
|
@ -142,6 +142,9 @@ pub use inventory;
|
||||||
// Re-exported for the `__wrap` functions
|
// Re-exported for the `__wrap` functions
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub use libc;
|
pub use libc;
|
||||||
|
// Re-exported for py_run
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub use unindent;
|
||||||
|
|
||||||
/// Raw ffi declarations for the c interface of python
|
/// Raw ffi declarations for the c interface of python
|
||||||
pub mod ffi;
|
pub mod ffi;
|
||||||
|
@ -271,7 +274,7 @@ macro_rules! py_run {
|
||||||
pyo3::py_run_impl!($py, $($val)+, pyo3::indoc::indoc!($code))
|
pyo3::py_run_impl!($py, $($val)+, pyo3::indoc::indoc!($code))
|
||||||
}};
|
}};
|
||||||
($py:expr, $($val:ident)+, $code:expr) => {{
|
($py:expr, $($val:ident)+, $code:expr) => {{
|
||||||
pyo3::py_run_impl!($py, $($val)+, &pyo3::_indoc_runtime($code))
|
pyo3::py_run_impl!($py, $($val)+, &pyo3::unindent::unindent($code))
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -296,30 +299,6 @@ macro_rules! py_run_impl {
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Removes indentation from multiline strings in pyrun commands
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub fn _indoc_runtime(commands: &str) -> String {
|
|
||||||
let ignore_first_line = commands.starts_with("\n") || commands.starts_with("\r\n");
|
|
||||||
let num_spaces = commands
|
|
||||||
.lines()
|
|
||||||
.skip(1)
|
|
||||||
.map(|s| s.chars().take_while(char::is_ascii_whitespace).count())
|
|
||||||
.min()
|
|
||||||
.unwrap_or(0);
|
|
||||||
let mut res = String::with_capacity(commands.len());
|
|
||||||
for (i, line) in commands.lines().enumerate() {
|
|
||||||
if i > 1 || (i == 1 && !ignore_first_line) {
|
|
||||||
res.push_str("\n")
|
|
||||||
}
|
|
||||||
if i == 0 {
|
|
||||||
res.push_str(line);
|
|
||||||
} else if line.len() > num_spaces {
|
|
||||||
res.push_str(&line[num_spaces..])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Test readme and user guide
|
/// Test readme and user guide
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub mod doc_test {
|
pub mod doc_test {
|
||||||
|
@ -353,29 +332,3 @@ pub mod doc_test {
|
||||||
doctest!("../guide/src/pypy.md", guide_pypy_md);
|
doctest!("../guide/src/pypy.md", guide_pypy_md);
|
||||||
doctest!("../guide/src/rust_cpython.md", guide_rust_cpython_md);
|
doctest!("../guide/src/rust_cpython.md", guide_rust_cpython_md);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_indoc_runtime() {
|
|
||||||
macro_rules! test_indoc {
|
|
||||||
($code: literal) => {
|
|
||||||
assert_eq!(indoc::indoc!($code), &_indoc_runtime($code));
|
|
||||||
};
|
|
||||||
}
|
|
||||||
test_indoc!(
|
|
||||||
r#"
|
|
||||||
assert time.hour == 8
|
|
||||||
assert time.repl_japanese() == "8時43分16秒"
|
|
||||||
assert time.as_tuple() == time_as_tuple
|
|
||||||
"#
|
|
||||||
);
|
|
||||||
test_indoc!(
|
|
||||||
r#"
|
|
||||||
def euclid(m, n):
|
|
||||||
assert m >= n
|
|
||||||
if n == 0:
|
|
||||||
return m
|
|
||||||
else:
|
|
||||||
return euclid(n, m % n)
|
|
||||||
"#
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue