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

@@ -3,17 +3,41 @@
// The docs for the `std::fmt` module are a good place to start and look for examples:
// https://doc.rust-lang.org/std/fmt/index.html#write
use std::error::Error;
use std::fmt::{Display, Formatter};
#[derive(Debug)]
enum TicketNewError {
TitleError(String),
DescriptionError(String),
}
impl Display for TicketNewError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
match self {
TicketNewError::TitleError(msg) => write!(f, "{msg}"),
TicketNewError::DescriptionError(msg) => write!(f, "{msg}"),
}
// write!(f, "")
}
}
impl Error for TicketNewError {}
// 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(ticket) => ticket,
Err(err) => match err {
TicketNewError::TitleError(_) => panic!("{err}"),
TicketNewError::DescriptionError(_) => {
Ticket::new(title, "Description not provided".to_string(), status).unwrap()
}
},
}
}
#[derive(Debug, PartialEq, Clone)]