initial commit
This commit is contained in:
7
exercises/04_traits/06_str_slice/Cargo.toml
Normal file
7
exercises/04_traits/06_str_slice/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "str_slice"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dev-dependencies]
|
||||
common = { path = "../../../helpers/common" }
|
||||
61
exercises/04_traits/06_str_slice/src/lib.rs
Normal file
61
exercises/04_traits/06_str_slice/src/lib.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
// TODO: Re-implement `Ticket`'s accessor methods. This time return a `&str` rather than a `&String`.
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn title(&self) -> &String {
|
||||
&self.title
|
||||
}
|
||||
|
||||
pub fn description(&self) -> &String {
|
||||
&self.description
|
||||
}
|
||||
|
||||
pub fn status(&self) -> &String {
|
||||
&self.status
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use common::{valid_description, valid_title};
|
||||
use std::any::{Any, TypeId};
|
||||
|
||||
#[test]
|
||||
fn test_type() {
|
||||
let ticket = Ticket::new(valid_title(), valid_description(), "To-Do".to_string());
|
||||
// Some dark magic to verify that you used the expected return types
|
||||
assert_eq!(TypeId::of::<str>(), ticket.title().type_id());
|
||||
assert_eq!(TypeId::of::<str>(), ticket.description().type_id());
|
||||
assert_eq!(TypeId::of::<str>(), ticket.status().type_id());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user