Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • master
  • 0.1
2 results

Target

Select target project
  • michael.hauspie/webcalc-rs
1 result
Select Git revision
Show changes
Commits on Source (2)
use rocket::{get, launch, post, routes, serde::json::Json}; use rocket::{get, launch, post, routes, serde::json::Json};
use serde::{Deserialize, Serialize};
const INDEX_TEXT: &str = r#"\ const INDEX_TEXT: &str = r#"\
Cette application permet d'executer des calculs simple. Cette application permet d'executer des calculs simple.
...@@ -30,16 +29,8 @@ Pour envoyer une requête avec curl: ...@@ -30,16 +29,8 @@ Pour envoyer une requête avec curl:
curl -X POST http://monurl -H 'Content-Type: application/json' -d '{"Plus", [3, 4]}' curl -X POST http://monurl -H 'Content-Type: application/json' -d '{"Plus", [3, 4]}'
"#; "#;
#[derive(Serialize, Deserialize, Copy, Clone, Debug)] mod operations;
enum Operation { use operations::{Operation, OperationResult};
Plus(i32,i32),
}
#[derive(Deserialize, Serialize, PartialEq, Debug)]
struct OperationResult {
operation: String,
resultat: i32,
}
#[get("/")] #[get("/")]
fn index() -> &'static str { fn index() -> &'static str {
...@@ -48,12 +39,7 @@ fn index() -> &'static str { ...@@ -48,12 +39,7 @@ fn index() -> &'static str {
#[post("/", data = "<operation>")] #[post("/", data = "<operation>")]
fn do_calc(operation: Json<Operation>) -> Json<OperationResult> { fn do_calc(operation: Json<Operation>) -> Json<OperationResult> {
match operation.into_inner() { OperationResult::from(operation.into_inner()).into()
Operation::Plus(op1, op2) => OperationResult {
operation: format!("{op1} + {op2}"),
resultat: op1 + op2,
}.into()
}
} }
#[get("/test")] #[get("/test")]
...@@ -74,6 +60,13 @@ mod tests { ...@@ -74,6 +60,13 @@ mod tests {
use rocket::uri; use rocket::uri;
use super::*; use super::*;
fn build_response<'r>(operation: Operation) -> (Status, OperationResult) {
let client = Client::tracked(launch()).expect("valid rocket instance");
let req = client.post(uri!(super::index)).json(&operation);
let response = req.dispatch();
(response.status(), response.into_json::<OperationResult>().unwrap())
}
#[test] #[test]
fn index() { fn index() {
let client = Client::tracked(launch()).expect("valid rocket instance"); let client = Client::tracked(launch()).expect("valid rocket instance");
...@@ -85,16 +78,20 @@ mod tests { ...@@ -85,16 +78,20 @@ mod tests {
#[test] #[test]
fn plus() { fn plus() {
let client = Client::tracked(launch()).expect("valid rocket instance"); let (status, response) = build_response(Operation::Plus(3, 8));
let operation = Operation::Plus(3, 8); assert_eq!(status, Status::Ok);
let req = client.post(uri!(super::index)).json(&operation);
let response = req.dispatch(); let expected = OperationResult::new("3 + 8", 11);
assert_eq!(response.status(), Status::Ok); assert_eq!(response, expected);
}
#[test]
fn moins() {
let (status, response) = build_response(Operation::Moins(10, 20));
assert_eq!(status, Status::Ok);
let expected = OperationResult { let expected = OperationResult::new("10 - 20", -10);
operation: "3 + 8".into(), assert_eq!(response, expected);
resultat: 11,
};
assert_eq!(response.into_json::<OperationResult>().unwrap(), expected);
} }
} }
//! This modules defines the possible operation and provide methods
//! to do the actual computation
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
pub enum Operation {
Plus(i32,i32),
Moins(i32,i32),
}
#[derive(Deserialize, Serialize, PartialEq, Debug)]
pub struct OperationResult {
operation: String,
resultat: i32,
}
impl OperationResult {
/// Creates a new operation result from a [`&str`] and a value.
pub fn new(s: &str, value: i32) -> Self {
Self {
operation: String::from(s),
resultat: value,
}
}
}
impl From<&Operation> for OperationResult {
fn from(value: &Operation) -> Self {
match value {
Operation::Plus(op1, op2) => OperationResult {
operation: format!("{op1} + {op2}"),
resultat: op1 + op2,
},
Operation::Moins(op1, op2) => OperationResult {
operation: format!("{op1} - {op2}"),
resultat: op1 - op2,
},
}
}
}
impl From<Operation> for OperationResult {
fn from(value: Operation) -> Self {
OperationResult::from(&value)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn plus() {
let operation = Operation::Plus(4, 5);
let result = OperationResult::from(operation);
let expected = OperationResult::new("4 + 5", 9);
assert_eq!(result, expected);
}
#[test]
fn moins() {
let operation = Operation::Moins(4, 5);
let result = OperationResult::from(operation);
let expected = OperationResult::new("4 - 5", -1);
assert_eq!(result, expected);
}
}