completed most of 05 ticket v2

This commit is contained in:
2025-03-17 06:39:58 +05:30
parent 8d1342c831
commit 8413518a84
14 changed files with 132 additions and 27 deletions

View File

@@ -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 {