mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
4.2 KiB
127 lines
4.2 KiB
4 years ago
|
## error types
|
||
|
|
||
|
### Instructions
|
||
|
|
||
4 years ago
|
For this exercise, you will have to implement an **error type**.
|
||
4 years ago
|
|
||
4 years ago
|
The main objective is to create a form validator, where you must implement an
|
||
4 years ago
|
error type. This must validate the password and the first name. The
|
||
|
first name must not be empty and the password must have at least 8 char and a combination of alphanumeric and none alphanumeric ASCII characters
|
||
|
|
||
|
ex: "asDd123=%" => good
|
||
4 years ago
|
"asgfD" => error
|
||
|
"asdsdf2" => error
|
||
|
"sad\_#$" => error
|
||
4 years ago
|
|
||
|
Create a structure called `Form` that will have the following fields:
|
||
|
|
||
|
- `first_name`, that will be a string
|
||
|
- `last_name`, that will be a string
|
||
|
- `birth`, of type `NaiveDate` that will convert a string "2015-09-05" to a date of that format
|
||
|
- `sex`, SexType that must be a `enum` with the fields `Male` and `Female`
|
||
|
- `birth_location`, that will be a string
|
||
|
- `password`, that will be a string
|
||
|
|
||
|
You must also implement for this structure a function to initialize the structure, `new` and a function called
|
||
|
`validate` that will validate the form
|
||
|
|
||
|
For the error type you must create a type struct called `FErr`, that will be the type for the error
|
||
|
It must have the fields:
|
||
|
|
||
|
- `form_values`, this will be a tuple of strings that will save the value that the user inserted into the form
|
||
|
|
||
4 years ago
|
ex: ("password", "asdaSD\_")
|
||
|
("first_name", "someone")
|
||
4 years ago
|
|
||
|
- `date`, that will have the date that the error occurred in the format "2020-12-14 09:33:41"
|
||
|
- `err`, that will have the error description:
|
||
|
- "No user name"
|
||
|
- "At least 8 characters"
|
||
|
- "Combination of different ASCII character types (numbers, letters and none alphanumeric characters)"
|
||
|
|
||
4 years ago
|
### Notions
|
||
|
|
||
|
- [Error types](https://doc.rust-lang.org/rust-by-example/error/multiple_error_types/define_error_type.html)
|
||
|
- [Struct NaiveDate](https://docs.rs/chrono/0.4.19/chrono/naive/struct.NaiveDate.html)
|
||
|
|
||
4 years ago
|
### Expected Function
|
||
|
|
||
|
```rust
|
||
|
pub use chrono::{Utc, NaiveDate};
|
||
|
|
||
|
// this will be the structure that wil handle the errors
|
||
|
#[derive(Debug, Eq, PartialEq)]
|
||
|
pub struct FErr {
|
||
|
// expected public fields
|
||
|
}
|
||
|
impl FErr {
|
||
|
pub fn new(name: String, error: String, err: String) -> FErr {}
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, Eq, PartialEq)]
|
||
|
pub enum SexType {
|
||
|
// expected public fields
|
||
|
}
|
||
|
#[derive(Debug, Eq, PartialEq)]
|
||
|
pub struct Form {
|
||
|
// expected public fields
|
||
|
}
|
||
|
|
||
|
impl Form {
|
||
|
pub fn new(first_name: String,
|
||
|
last_name: String,
|
||
|
birth: NaiveDate,
|
||
|
sex: SexType,
|
||
|
birth_location: String,
|
||
|
password: String) -> Form {}
|
||
|
pub fn validate(&self) -> Result<Vec<&str>, FErr> {}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### Usage
|
||
|
|
||
4 years ago
|
Here is a program to test your function:
|
||
4 years ago
|
|
||
|
```rust
|
||
4 years ago
|
use error_types::*;
|
||
|
|
||
4 years ago
|
fn main() {
|
||
|
let mut form_output = Form::new(
|
||
|
String::from("Alice"),
|
||
|
String::from("Bear"),
|
||
|
create_date("2015-09-05"),
|
||
|
SexType::Male,
|
||
|
String::from("Africa"),
|
||
|
String::from("qwqwsa1dty_"));
|
||
|
|
||
|
println!("{:?}", form_output);
|
||
|
println!("{:?}", form_output.validate().unwrap());
|
||
|
|
||
|
form_output.first_name = String::from("");
|
||
|
println!("{:?}", form_output.validate().unwrap_err());
|
||
|
|
||
|
form_output.first_name = String::from("as");
|
||
|
form_output.password = String::from("dty_1");
|
||
|
println!("{:?}", form_output.validate().unwrap_err());
|
||
|
|
||
|
form_output.password = String::from("asdasASd(_");
|
||
|
println!("{:?}", form_output.validate().unwrap_err());
|
||
|
|
||
|
form_output.password = String::from("asdasASd123SA");
|
||
|
println!("{:?}", form_output.validate().unwrap_err());
|
||
|
}
|
||
|
```
|
||
|
|
||
|
And its output:
|
||
|
|
||
|
```console
|
||
|
student@ubuntu:~/[[ROOT]]/test$ cargo run
|
||
|
Form { first_name: "Lee", last_name: "Silva", birth: 2015-09-05, sex: Male, birth_location: "Africa", password: "qwqwsa1dty_" }
|
||
|
["Valid first name", "Valid password"]
|
||
|
FErr { form_values: ("first_name", ""), date: "2020-12-28 13:29:11", err: "No user name" }
|
||
|
FErr { form_values: ("password", "dty_1"), date: "2020-12-28 13:29:11", err: "At least 8 characters" }
|
||
|
FErr { form_values: ("password", "asdasASd(_"), date: "2020-12-28 13:29:11", err: "Combination of different ASCII character types (numbers, letters and none alphanumeric characters)" }
|
||
|
FErr { form_values: ("password", "asdasASd123SA"), date: "2020-12-28 13:29:11", err: "Combination of different ASCII character types (numbers, letters and none alphanumeric characters)" }
|
||
|
student@ubuntu:~/[[ROOT]]/test$
|
||
|
```
|