summaryrefslogtreecommitdiff
path: root/rust/src/web.rs
diff options
context:
space:
mode:
Diffstat (limited to 'rust/src/web.rs')
-rw-r--r--rust/src/web.rs35
1 files changed, 29 insertions, 6 deletions
diff --git a/rust/src/web.rs b/rust/src/web.rs
index 8f56d3e..b9bab27 100644
--- a/rust/src/web.rs
+++ b/rust/src/web.rs
@@ -15,7 +15,7 @@ use tokio::task;
use uuid::Uuid;
use lambda::lambda::{eval_all, eval_whnf, generate_expr, generate_exprs, Environment};
-use lambda::parser::parse;
+use lambda::parser::{parse, parse_total};
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
struct Registration {
@@ -215,13 +215,17 @@ async fn register(
#[post("/eval")]
async fn eval(input: String) -> impl Responder {
- let mut env = Environment::new();
- match parse(&input).first() {
- Some(expr) => {
- let output = eval_whnf(expr, &mut env);
+ let exprs = parse_total(&input);
+ match exprs {
+ Ok(exprs) => {
+ let output = eval_all(&exprs)
+ .iter()
+ .map(|v| format!("{}", v))
+ .collect::<Vec<_>>()
+ .join("\n");
HttpResponse::Ok().body(format!("{}", output))
}
- None => HttpResponse::BadRequest().finish(),
+ Err(e) => HttpResponse::BadRequest().body(format!("{}", e)),
}
}
@@ -438,6 +442,25 @@ mod app_tests {
}
#[actix_web::test]
+ async fn post_expression_returns_multiple_evaluations() {
+ let app = test::init_service(App::new().wrap(Logger::default()).service(eval)).await;
+
+ let req = test::TestRequest::post()
+ .uri("/eval")
+ .set_payload("((lam (x y) x) 1 2)\n42")
+ .insert_header(ContentType::plaintext())
+ .to_request();
+
+ let resp = test::call_service(&app, req).await;
+
+ assert!(resp.status().is_success());
+
+ let body = resp.into_body();
+ let bytes = body::to_bytes(body).await.unwrap();
+ assert_eq!("1\n42".to_string().into_bytes(), bytes);
+ }
+
+ #[actix_web::test]
async fn get_leaderboard_returns_html_page_listing_clients_state() {
let app_state = Arc::new(Mutex::new(State::new()));
app_state.lock().await.register(&Registration {