From 65aba3c993edd5118becfb25f837f95d854d8e82 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 17 Aug 2023 03:34:49 -0400 Subject: [PATCH] Convert emacs wrapper to tokio's Command. --- Cargo.lock | 10 ++++++++++ Cargo.toml | 2 +- src/main.rs | 7 +++++-- src/owner_tree.rs | 1 + src/parse.rs | 12 +++++++----- 5 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 src/owner_tree.rs diff --git a/Cargo.lock b/Cargo.lock index 34dd413..76b29ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -524,6 +524,15 @@ dependencies = [ "serde", ] +[[package]] +name = "signal-hook-registry" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" +dependencies = [ + "libc", +] + [[package]] name = "socket2" version = "0.4.9" @@ -573,6 +582,7 @@ dependencies = [ "mio", "num_cpus", "pin-project-lite", + "signal-hook-registry", "socket2 0.5.3", "tokio-macros", "windows-sys", diff --git a/Cargo.toml b/Cargo.toml index 4fc8503..c02a1f7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,5 @@ edition = "2021" axum = { git = "https://github.com/tokio-rs/axum.git", rev = "52a90390195e884bcc12ff5bd9fd805cac806447" } nom = "7.1.1" serde = { version = "1.0.183", features = ["derive"] } -tokio = { version = "1.30.0", default-features = false, features = ["macros", "rt", "rt-multi-thread"] } +tokio = { version = "1.30.0", default-features = false, features = ["macros", "process", "rt", "rt-multi-thread"] } tower-http = { version = "0.4.3", features = ["fs"] } diff --git a/src/main.rs b/src/main.rs index b20b551..296910a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,10 @@ #![feature(exit_status_error)] use axum::{http::StatusCode, routing::post, Json, Router}; +use parse::emacs_parse_org_document; use serde::Serialize; use tower_http::services::{ServeDir, ServeFile}; +mod owner_tree; mod parse; #[tokio::main] @@ -16,9 +18,10 @@ async fn main() { axum::serve(listener, app).await.unwrap(); } -async fn parse_org_mode(body: String) -> (StatusCode, Json) { +async fn parse_org_mode(body: String) -> Result<(StatusCode, Json), (StatusCode, String)> { + let ast = emacs_parse_org_document(&body).await.map_err(|e| (StatusCode::BAD_REQUEST, e.to_string()))?; let ret = OwnerTree { input_source: body }; - (StatusCode::OK, Json(ret)) + Ok((StatusCode::OK, Json(ret))) } #[derive(Serialize)] diff --git a/src/owner_tree.rs b/src/owner_tree.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/owner_tree.rs @@ -0,0 +1 @@ + diff --git a/src/parse.rs b/src/parse.rs index bce786e..bdf3d25 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -1,6 +1,8 @@ -use std::process::Command; +use tokio::process::Command; -pub fn emacs_parse_org_document(file_contents: C) -> Result> +pub async fn emacs_parse_org_document( + file_contents: C, +) -> Result> where C: AsRef, { @@ -22,10 +24,10 @@ where .arg("--batch") .arg("--eval") .arg(elisp_script); - let out = proc.output()?; + + let out = proc.output().await?; out.status.exit_ok()?; - let org_sexp = out.stderr; - Ok(String::from_utf8(org_sexp)?) + Ok(String::from_utf8(out.stderr)?) } fn escape_elisp_string(file_contents: C) -> String