Skip to content

Commit

Permalink
05_08
Browse files Browse the repository at this point in the history
  • Loading branch information
chemonoworld committed Sep 26, 2024
1 parent fffffc5 commit bcfbad6
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions exercises/05_ticket_v2/08_error_enums/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
// 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(String),
DescriptionError(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!()
let ticket = Ticket::new(title.clone(), description.clone(), status.clone());

match ticket {
Ok(ticket) => ticket,
Err(ticket_new_error) => match ticket_new_error {
TicketNewError::TitleError(m) => panic!("{}", m),
TicketNewError::DescriptionError(m) => Ticket {
title,
description: "Description not provided".to_string(),
status
}
},
}
}

#[derive(Debug, PartialEq)]
Expand All @@ -32,16 +47,24 @@ impl Ticket {
status: Status,
) -> Result<Ticket, TicketNewError> {
if title.is_empty() {
return Err("Title cannot be empty".to_string());
return Err(TicketNewError::TitleError(
"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(
"Title cannot be longer than 50 bytes".to_string(),
));
}
if description.is_empty() {
return Err("Description cannot be empty".to_string());
return Err(TicketNewError::DescriptionError(
"Description cannot be empty".to_string(),
));
}
if description.len() > 500 {
return Err("Description cannot be longer than 500 bytes".to_string());
return Err(TicketNewError::DescriptionError(
"Description cannot be longer than 500 bytes".to_string(),
));
}

Ok(Ticket {
Expand Down

0 comments on commit bcfbad6

Please sign in to comment.