Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
webcalc-rs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michael Hauspie
webcalc-rs
Compare revisions
ccda501ff4b67bc5394a8362eff71b28dc23d6fe to 6f2210b165513d435a11c36ae15cef53ba714a6d
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
michael.hauspie/webcalc-rs
Select target project
No results found
6f2210b165513d435a11c36ae15cef53ba714a6d
Select Git revision
Branches
master
Tags
0.1
2 results
Swap
Target
michael.hauspie/webcalc-rs
Select target project
michael.hauspie/webcalc-rs
1 result
ccda501ff4b67bc5394a8362eff71b28dc23d6fe
Select Git revision
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source (2)
Refactor by moving operations to own module
· f74d8421
Michael Hauspie
authored
1 year ago
f74d8421
Add Moins
· 6f2210b1
Michael Hauspie
authored
1 year ago
6f2210b1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main.rs
+24
-27
24 additions, 27 deletions
src/main.rs
src/operations.rs
+73
-0
73 additions, 0 deletions
src/operations.rs
with
97 additions
and
27 deletions
src/main.rs
View file @
6f2210b1
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 diff is collapsed.
Click to expand it.
src/operations.rs
0 → 100644
View file @
6f2210b1
//! 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
);
}
}
This diff is collapsed.
Click to expand it.