initial commit
This commit is contained in:
61
exercises/03_ticket_v1/05_encapsulation/src/lib.rs
Normal file
61
exercises/03_ticket_v1/05_encapsulation/src/lib.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
pub mod ticket {
|
||||
pub struct Ticket {
|
||||
title: String,
|
||||
description: String,
|
||||
status: String,
|
||||
}
|
||||
|
||||
impl Ticket {
|
||||
pub fn new(title: String, description: String, status: String) -> 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");
|
||||
}
|
||||
if status != "To-Do" && status != "In Progress" && status != "Done" {
|
||||
panic!("Only `To-Do`, `In Progress`, and `Done` statuses are allowed");
|
||||
}
|
||||
|
||||
Ticket {
|
||||
title,
|
||||
description,
|
||||
status,
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Add three public methods to the `Ticket` struct:
|
||||
// - `title` that returns the `title` field.
|
||||
// - `description` that returns the `description` field.
|
||||
// - `status` that returns the `status` field.
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::ticket::Ticket;
|
||||
|
||||
#[test]
|
||||
fn description() {
|
||||
let ticket = Ticket::new("A title".into(), "A description".into(), "To-Do".into());
|
||||
assert_eq!(ticket.description(), "A description");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn title() {
|
||||
let ticket = Ticket::new("A title".into(), "A description".into(), "To-Do".into());
|
||||
assert_eq!(ticket.title(), "A title");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn status() {
|
||||
let ticket = Ticket::new("A title".into(), "A description".into(), "To-Do".into());
|
||||
assert_eq!(ticket.status(), "To-Do");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user