initial commit
This commit is contained in:
9
exercises/05_ticket_v2/12_thiserror/Cargo.toml
Normal file
9
exercises/05_ticket_v2/12_thiserror/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "thiserror_"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
|
||||
[dev-dependencies]
|
||||
common = { path = "../../../helpers/common" }
|
||||
85
exercises/05_ticket_v2/12_thiserror/src/lib.rs
Normal file
85
exercises/05_ticket_v2/12_thiserror/src/lib.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
// TODO: Implement the `Error` trait for `TicketNewError` using `thiserror`.
|
||||
// We've changed the enum variants to be more specific, thus removing the need for storing
|
||||
// a `String` field into each variant.
|
||||
// You'll also have to add `thiserror` as a dependency in the `Cargo.toml` file.
|
||||
|
||||
enum TicketNewError {
|
||||
TitleCannotBeEmpty,
|
||||
TitleTooLong,
|
||||
DescriptionCannotBeEmpty,
|
||||
DescriptionTooLong,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
struct Ticket {
|
||||
title: String,
|
||||
description: String,
|
||||
status: Status,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
enum Status {
|
||||
ToDo,
|
||||
InProgress { assigned_to: String },
|
||||
Done,
|
||||
}
|
||||
|
||||
impl Ticket {
|
||||
pub fn new(
|
||||
title: String,
|
||||
description: String,
|
||||
status: Status,
|
||||
) -> Result<Ticket, TicketNewError> {
|
||||
if title.is_empty() {
|
||||
return Err(TicketNewError::TitleCannotBeEmpty);
|
||||
}
|
||||
if title.len() > 50 {
|
||||
return Err(TicketNewError::TitleTooLong);
|
||||
}
|
||||
if description.is_empty() {
|
||||
return Err(TicketNewError::DescriptionCannotBeEmpty);
|
||||
}
|
||||
if description.len() > 500 {
|
||||
return Err(TicketNewError::DescriptionTooLong);
|
||||
}
|
||||
|
||||
Ok(Ticket {
|
||||
title,
|
||||
description,
|
||||
status,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use common::{overly_long_description, overly_long_title, valid_description, valid_title};
|
||||
|
||||
#[test]
|
||||
fn title_cannot_be_empty() {
|
||||
let err = Ticket::new("".into(), valid_description(), Status::ToDo).unwrap_err();
|
||||
assert_eq!(err.to_string(), "Title cannot be empty");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn description_cannot_be_empty() {
|
||||
let err = Ticket::new(valid_title(), "".into(), Status::ToDo).unwrap_err();
|
||||
assert_eq!(err.to_string(), "Description cannot be empty");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn title_cannot_be_longer_than_fifty_chars() {
|
||||
let err = Ticket::new(overly_long_title(), valid_description(), Status::ToDo).unwrap_err();
|
||||
assert_eq!(err.to_string(), "Title cannot be longer than 50 bytes");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn description_cannot_be_too_long() {
|
||||
let err = Ticket::new(valid_title(), overly_long_description(), Status::ToDo).unwrap_err();
|
||||
assert_eq!(
|
||||
err.to_string(),
|
||||
"Description cannot be longer than 500 bytes"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user