diff options
Diffstat (limited to 'rust/src')
| -rw-r--r-- | rust/src/ast.rs | 4 | ||||
| -rw-r--r-- | rust/src/lambda.rs | 5 | ||||
| -rw-r--r-- | rust/src/lib.rs | 1 | ||||
| -rw-r--r-- | rust/src/web.rs | 86 |
4 files changed, 74 insertions, 22 deletions
diff --git a/rust/src/ast.rs b/rust/src/ast.rs index 2221856..dfb6862 100644 --- a/rust/src/ast.rs +++ b/rust/src/ast.rs @@ -1,6 +1,8 @@ use std::fmt::{self, Display}; -#[derive(Debug, PartialEq, Clone)] +use serde::{Deserialize, Serialize}; + +#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)] pub enum Value { Num(i32), Bool(bool), diff --git a/rust/src/lambda.rs b/rust/src/lambda.rs index 6f394d8..7c8d9be 100644 --- a/rust/src/lambda.rs +++ b/rust/src/lambda.rs @@ -1,4 +1,5 @@ use rand::Rng; +use serde::{Deserialize, Serialize}; use std::collections::HashMap; use crate::ast::*; @@ -97,6 +98,10 @@ fn gensym() -> String { format!("x_{}", n1) } +pub fn generate_expr(size: u32) -> Value { + Value::Num(3) +} + #[cfg(test)] mod lambda_test { use crate::parser::parse; diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 568637e..a8cf18e 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -2,4 +2,3 @@ pub mod ast; pub mod io; pub mod lambda; pub mod parser; -pub mod web; diff --git a/rust/src/web.rs b/rust/src/web.rs index 45540e4..94b01cb 100644 --- a/rust/src/web.rs +++ b/rust/src/web.rs @@ -2,18 +2,23 @@ use actix_web::{ get, http::header::ContentType, middleware::Logger, post, web, App, HttpResponse, HttpServer, Responder, }; -use async_std::task; -use futures::future::join_all; use futures::try_join; +use futures::{future::join_all, lock::Mutex}; +use log::info; use serde::{Deserialize, Serialize}; use std::{ collections::HashMap, env::args, io::{stdin, stdout, IsTerminal}, - sync::{Arc, Mutex}, + sync::Arc, }; +use tokio; +use tokio::task; use uuid::Uuid; +use lambda::lambda::{eval_whnf, generate_expr, Environment}; +use lambda::parser::parse; + #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] struct Registration { url: String, @@ -30,14 +35,10 @@ enum RegistrationResult { } #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] -enum RegistrationFailure { - UrlAlreadyRegistered { url: String }, -} - -#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] struct Client { id: Uuid, url: String, + grade: u8, } #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] @@ -66,6 +67,7 @@ impl AppState for State { Client { id, url: registration.url.clone(), + grade: 1, }, ); RegistrationResult::RegistrationSuccess { @@ -81,18 +83,18 @@ async fn register( app_state: web::Data<Arc<Mutex<dyn AppState>>>, registration: web::Json<Registration>, ) -> impl Responder { - let result = app_state.lock().unwrap().register(®istration); + let result = app_state.lock().await.register(®istration); match result { RegistrationResult::RegistrationSuccess { .. } => HttpResponse::Ok().json(result), RegistrationResult::UrlAlreadyRegistered { .. } => HttpResponse::BadRequest().json(result), } } -#[actix_web::main] +#[tokio::main] async fn main() -> std::io::Result<()> { let app_state = Arc::new(Mutex::new(State::new())); let send_state: Arc<Mutex<State>> = app_state.clone(); - let http_state: Arc<Mutex<dyn AppState>> = app_state.clone(); + let http_state: Arc<Mutex<dyn AppState>> = app_state; env_logger::init(); // let it run in the background task::spawn(async move { send_tests(send_state).await }); @@ -107,19 +109,54 @@ async fn main() -> std::io::Result<()> { .await } -async fn send_tests(clients: Arc<Mutex<State>>) -> ! { +async fn send_tests(state_ref: Arc<Mutex<State>>) { loop { - task::sleep(std::time::Duration::from_secs(1)).await; - let clients = clients.lock().unwrap(); - for client in clients.clients.values() { - send_test(client); + tokio::time::sleep(std::time::Duration::from_secs(1)).await; + let mut state = state_ref.lock().await; + for (_, client) in state.clients.iter_mut() { + let result = send_test(client.grade, &client.url).await; + match result { + Ok(_) => { + client.grade += 1; + } + Err(_) => { + // FIXME + } + } } } } -fn send_test(client: &Client) -> Result<(), String> { - println!("Sending test to {}", client.url); - Ok(()) +async fn send_test(grade: u8, url: &String) -> Result<(), String> { + let input = generate_expr(grade.into()); + let mut env = Environment::new(); + let output = eval_whnf(&input, &mut env); + info!("Sending {} to {}", input, url); + let response = reqwest::Client::new() + .post(url) + .body(format!("{}", input)) + .send() + .await; + match response { + Ok(response) => { + let body = response.text().await.unwrap(); + let vals = parse(&body); + // FIXME + let result = eval_whnf(vals.first().unwrap(), &mut env); + info!("Received {} from {}", body, url); + if result == output { + info!("Test passed {}", url); + Ok(()) + } else { + info!("Test failed {}", url); + Err("Test failed".to_string()) + } + } + Err(e) => { + info!("Error sending test: {}", e); + Err("Error sending test".to_string()) + } + } } #[cfg(test)] @@ -143,7 +180,7 @@ mod app_tests { impl AppState for DummyAppState { fn register(&mut self, registration: &Registration) -> RegistrationResult { - if self.id == "" { + if self.id.is_empty() { RegistrationResult::UrlAlreadyRegistered { url: registration.url.clone(), } @@ -236,4 +273,13 @@ mod app_tests { result ); } + + // #[test] + // async fn tester_repeatedly_callback_registered_client() { + // let app_state = Arc::new(Mutex::new(State::new())); + // let send_state = app_state.clone(); + // let client = Client { + // id: Uuid::new_v4(), + // url: "http:// + // } } |
