Convert emacs wrapper to tokio's Command.

This commit is contained in:
Tom Alexander 2023-08-17 03:34:49 -04:00
parent 0d2a13739a
commit 65aba3c993
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
5 changed files with 24 additions and 8 deletions

10
Cargo.lock generated
View File

@ -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",

View File

@ -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"] }

View File

@ -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<OwnerTree>) {
async fn parse_org_mode(body: String) -> Result<(StatusCode, Json<OwnerTree>), (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)]

1
src/owner_tree.rs Normal file
View File

@ -0,0 +1 @@

View File

@ -1,6 +1,8 @@
use std::process::Command;
use tokio::process::Command;
pub fn emacs_parse_org_document<C>(file_contents: C) -> Result<String, Box<dyn std::error::Error>>
pub async fn emacs_parse_org_document<C>(
file_contents: C,
) -> Result<String, Box<dyn std::error::Error>>
where
C: AsRef<str>,
{
@ -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<C>(file_contents: C) -> String