summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGalen Guyer <galen@galenguyer.com>2022-03-20 17:21:43 -0400
committerGalen Guyer <galen@galenguyer.com>2022-03-20 17:21:43 -0400
commitbec49237d3df8182db247115a56e5b61cec4eac0 (patch)
tree51fe10c83480f087e8baf84ec114716204d228ce
parent95067db71dc7b7b05ed70d910379f4ce424f1b65 (diff)
execute python with rust
-rw-r--r--Cargo.lock139
-rw-r--r--Cargo.toml3
-rw-r--r--python.docxbin6219 -> 6219 bytes
-rw-r--r--src/main.rs22
4 files changed, 158 insertions, 6 deletions
diff --git a/Cargo.lock b/Cargo.lock
index bbd1ce0..7184f8e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -199,6 +199,24 @@ dependencies = [
]
[[package]]
+name = "indoc"
+version = "1.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e7906a9fababaeacb774f72410e497a1d18de916322e33797bb2cd29baa23c9e"
+dependencies = [
+ "unindent",
+]
+
+[[package]]
+name = "instant"
+version = "0.1.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
+dependencies = [
+ "cfg-if",
+]
+
+[[package]]
name = "itoa"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -226,6 +244,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efaa7b300f3b5fe8eb6bf21ce3895e1751d9665086af2d64b42f19701015ff4f"
[[package]]
+name = "lock_api"
+version = "0.4.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "88943dd7ef4a2e5a4bfa2753aaab3013e34ce2533d1996fb18ef591e315e2b3b"
+dependencies = [
+ "scopeguard",
+]
+
+[[package]]
name = "log"
version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -314,6 +341,37 @@ dependencies = [
]
[[package]]
+name = "once_cell"
+version = "1.10.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
+
+[[package]]
+name = "parking_lot"
+version = "0.11.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99"
+dependencies = [
+ "instant",
+ "lock_api",
+ "parking_lot_core",
+]
+
+[[package]]
+name = "parking_lot_core"
+version = "0.8.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d76e8e1493bcac0d2766c42737f34458f1c8c50c0d23bcb24ea953affb273216"
+dependencies = [
+ "cfg-if",
+ "instant",
+ "libc",
+ "redox_syscall",
+ "smallvec",
+ "winapi",
+]
+
+[[package]]
name = "png"
version = "0.16.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -339,6 +397,66 @@ name = "pydocx"
version = "0.1.0"
dependencies = [
"docx-rs",
+ "pyo3",
+]
+
+[[package]]
+name = "pyo3"
+version = "0.16.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a378727d5fdcaafd15b5afe9842cff1c25fdc43f62a162ffda2263c57ad98703"
+dependencies = [
+ "cfg-if",
+ "indoc",
+ "libc",
+ "parking_lot",
+ "pyo3-build-config",
+ "pyo3-ffi",
+ "pyo3-macros",
+ "unindent",
+]
+
+[[package]]
+name = "pyo3-build-config"
+version = "0.16.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4fbb27a3e96edd34c13d97d0feefccc90a79270c577c66e19d95af8323823dfc"
+dependencies = [
+ "once_cell",
+]
+
+[[package]]
+name = "pyo3-ffi"
+version = "0.16.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7b719fff844bcf3f911132112ec06527eb195f6a98e0c42cf97e1118929fd4ea"
+dependencies = [
+ "libc",
+ "pyo3-build-config",
+]
+
+[[package]]
+name = "pyo3-macros"
+version = "0.16.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f795e52d3320abb349ca28b501a7112154a87f353fae1c811deecd58e99cfa9b"
+dependencies = [
+ "proc-macro2",
+ "pyo3-macros-backend",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "pyo3-macros-backend"
+version = "0.16.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "39e03aa57a3bb7b96982958088df38302a139df4eef54671bc595f26556cb75b"
+dependencies = [
+ "proc-macro2",
+ "pyo3-build-config",
+ "quote",
+ "syn",
]
[[package]]
@@ -376,6 +494,15 @@ dependencies = [
]
[[package]]
+name = "redox_syscall"
+version = "0.2.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8380fe0152551244f0747b1bf41737e0f8a74f97a14ccefd1148187271634f3c"
+dependencies = [
+ "bitflags",
+]
+
+[[package]]
name = "ryu"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -425,6 +552,12 @@ dependencies = [
]
[[package]]
+name = "smallvec"
+version = "1.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83"
+
+[[package]]
name = "syn"
version = "1.0.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -505,6 +638,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
+name = "unindent"
+version = "0.1.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "514672a55d7380da379785a4d70ca8386c8883ff7eaae877be4d2081cebe73d8"
+
+[[package]]
name = "wasm-bindgen"
version = "0.2.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index c937268..5bdcc06 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,3 +7,6 @@ edition = "2021"
[dependencies]
docx-rs = "0.3.4"
+pyo3 = "0.16.2"
+
+[features]
diff --git a/python.docx b/python.docx
index 11c1bd0..b0fb0d6 100644
--- a/python.docx
+++ b/python.docx
Binary files differ
diff --git a/src/main.rs b/src/main.rs
index 7e475b7..efbd445 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -8,7 +8,7 @@ pub fn main() {
let mut buf = vec![];
file.read_to_end(&mut buf).unwrap();
- // let mut file = File::create("./python.json").unwrap();
+ let mut code = String::from("");
let res = read_docx(&buf).unwrap().document;
for document_child in res.children {
match document_child {
@@ -18,12 +18,17 @@ pub fn main() {
ParagraphChild::Run(run) => {
for run_child in run.children {
match run_child {
- RunChild::Tab(_) => {print!(" ")}
- RunChild::Text(text) => {println!("{}", text.text)}
+ RunChild::Tab(_) => {
+ code.push_str(" ");
+ }
+ RunChild::Text(text) => {
+ code.push_str(&text.text);
+ code.push('\n');
+ }
_ => {}
}
}
- },
+ }
_ => {}
}
}
@@ -31,6 +36,11 @@ pub fn main() {
_ => {}
}
}
- // file.write_all(res.as_bytes()).unwrap();
- // file.flush().unwrap();
+
+ code = code.replace("\u{201c}", "\"");
+ code = code.replace("\u{201d}", "\"");
+
+ pyo3::prepare_freethreaded_python();
+ let py = pyo3::Python::acquire_gil();
+ py.python().run(&code, None, None).unwrap();
}