From bcfbad65ed0ea09d9d1c266833dcbfbe03a53285 Mon Sep 17 00:00:00 2001 From: Calvin Lee <jinwoo0451@naver.com> Date: Thu, 26 Sep 2024 23:45:37 +0900 Subject: [PATCH] 05_08 --- .../05_ticket_v2/08_error_enums/src/lib.rs | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/exercises/05_ticket_v2/08_error_enums/src/lib.rs b/exercises/05_ticket_v2/08_error_enums/src/lib.rs index c74fcc9ad..5f3498db5 100644 --- a/exercises/05_ticket_v2/08_error_enums/src/lib.rs +++ b/exercises/05_ticket_v2/08_error_enums/src/lib.rs @@ -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)] @@ -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 {