completed most of 05 ticket v2
This commit is contained in:
@@ -1,14 +1,27 @@
|
||||
// TODO: Use two variants, one for a title error and one for a description error.
|
||||
// Each variant should contain a string with the explanation of what went wrong exactly.
|
||||
// You'll have to update the implementation of `Ticket::new` as well.
|
||||
enum TicketNewError {}
|
||||
enum TicketNewError {
|
||||
TitleError { desc: String },
|
||||
DescError { desc: String },
|
||||
}
|
||||
|
||||
// TODO: `easy_ticket` should panic when the title is invalid, using the error message
|
||||
// stored inside the relevant variant of the `TicketNewError` enum.
|
||||
// When the description is invalid, instead, it should use a default description:
|
||||
// "Description not provided".
|
||||
fn easy_ticket(title: String, description: String, status: Status) -> Ticket {
|
||||
todo!()
|
||||
match Ticket::new(title.clone(), description, status.clone()) {
|
||||
Ok(proper) => proper,
|
||||
Err(TicketNewError::DescError { desc }) => Ticket {
|
||||
title: title.clone(),
|
||||
description: "Description not provided".to_string(),
|
||||
status: status.clone(),
|
||||
},
|
||||
Err(TicketNewError::TitleError { desc }) => {
|
||||
panic!("{desc}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
@@ -32,16 +45,24 @@ impl Ticket {
|
||||
status: Status,
|
||||
) -> Result<Ticket, TicketNewError> {
|
||||
if title.is_empty() {
|
||||
return Err("Title cannot be empty".to_string());
|
||||
return Err(TicketNewError::TitleError {
|
||||
desc: "Title cannot be empty".to_string(),
|
||||
});
|
||||
}
|
||||
if title.len() > 50 {
|
||||
return Err("Title cannot be longer than 50 bytes".to_string());
|
||||
return Err(TicketNewError::TitleError {
|
||||
desc: "Title cannot be longer than 50 bytes".to_string(),
|
||||
});
|
||||
}
|
||||
if description.is_empty() {
|
||||
return Err("Description cannot be empty".to_string());
|
||||
return Err(TicketNewError::DescError {
|
||||
desc: "Description cannot be empty".to_string(),
|
||||
});
|
||||
}
|
||||
if description.len() > 500 {
|
||||
return Err("Description cannot be longer than 500 bytes".to_string());
|
||||
return Err(TicketNewError::DescError {
|
||||
desc: "Description cannot be longer than 500 bytes".to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(Ticket {
|
||||
|
||||
Reference in New Issue
Block a user