initial commit
This commit is contained in:
7
exercises/05_ticket_v2/07_unwrap/Cargo.toml
Normal file
7
exercises/05_ticket_v2/07_unwrap/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "unwrap"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dev-dependencies]
|
||||
common = { path = "../../../helpers/common" }
|
||||
73
exercises/05_ticket_v2/07_unwrap/src/lib.rs
Normal file
73
exercises/05_ticket_v2/07_unwrap/src/lib.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
// TODO: `easy_ticket` should panic when the title is invalid.
|
||||
// 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!()
|
||||
}
|
||||
|
||||
#[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, String> {
|
||||
if title.is_empty() {
|
||||
return Err("Title cannot be empty".to_string());
|
||||
}
|
||||
if title.len() > 50 {
|
||||
return Err("Title cannot be longer than 50 bytes".to_string());
|
||||
}
|
||||
if description.is_empty() {
|
||||
return Err("Description cannot be empty".to_string());
|
||||
}
|
||||
if description.len() > 500 {
|
||||
return Err("Description cannot be longer than 500 bytes".to_string());
|
||||
}
|
||||
|
||||
Ok(Ticket {
|
||||
title,
|
||||
description,
|
||||
status,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use common::{overly_long_description, overly_long_title, valid_description, valid_title};
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Title cannot be empty")]
|
||||
fn title_cannot_be_empty() {
|
||||
easy_ticket("".into(), valid_description(), Status::ToDo);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn template_description_is_used_if_empty() {
|
||||
let ticket = easy_ticket(valid_title(), "".into(), Status::ToDo);
|
||||
assert_eq!(ticket.description, "Description not provided");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Title cannot be longer than 50 bytes")]
|
||||
fn title_cannot_be_longer_than_fifty_chars() {
|
||||
easy_ticket(overly_long_title(), valid_description(), Status::ToDo);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn template_description_is_used_if_too_long() {
|
||||
let ticket = easy_ticket(valid_title(), overly_long_description(), Status::ToDo);
|
||||
assert_eq!(ticket.description, "Description not provided");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user