initial commit
This commit is contained in:
7
exercises/05_ticket_v2/05_nullability/Cargo.toml
Normal file
7
exercises/05_ticket_v2/05_nullability/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "nullability"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dev-dependencies]
|
||||
common = { path = "../../../helpers/common" }
|
||||
71
exercises/05_ticket_v2/05_nullability/src/lib.rs
Normal file
71
exercises/05_ticket_v2/05_nullability/src/lib.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
// TODO: Implement `Ticket::assigned_to` using `Option` as the return type.
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct Ticket {
|
||||
title: String,
|
||||
description: String,
|
||||
status: Status,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
enum Status {
|
||||
ToDo,
|
||||
InProgress { assigned_to: String },
|
||||
Done,
|
||||
}
|
||||
|
||||
impl Ticket {
|
||||
pub fn new(title: String, description: String, status: Status) -> Ticket {
|
||||
if title.is_empty() {
|
||||
panic!("Title cannot be empty");
|
||||
}
|
||||
if title.len() > 50 {
|
||||
panic!("Title cannot be longer than 50 bytes");
|
||||
}
|
||||
if description.is_empty() {
|
||||
panic!("Description cannot be empty");
|
||||
}
|
||||
if description.len() > 500 {
|
||||
panic!("Description cannot be longer than 500 bytes");
|
||||
}
|
||||
|
||||
Ticket {
|
||||
title,
|
||||
description,
|
||||
status,
|
||||
}
|
||||
}
|
||||
pub fn assigned_to(&self) -> Option<&String> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use common::{valid_description, valid_title};
|
||||
|
||||
#[test]
|
||||
fn test_todo() {
|
||||
let ticket = Ticket::new(valid_title(), valid_description(), Status::ToDo);
|
||||
assert!(ticket.assigned_to().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_done() {
|
||||
let ticket = Ticket::new(valid_title(), valid_description(), Status::Done);
|
||||
assert!(ticket.assigned_to().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_in_progress() {
|
||||
let ticket = Ticket::new(
|
||||
valid_title(),
|
||||
valid_description(),
|
||||
Status::InProgress {
|
||||
assigned_to: "Alice".to_string(),
|
||||
},
|
||||
);
|
||||
assert_eq!(ticket.assigned_to(), Some(&"Alice".to_string()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user