Compare commits
4 Commits
d3b4c0d653
...
1f0823a6a4
| Author | SHA1 | Date | |
|---|---|---|---|
| 1f0823a6a4 | |||
| 56075fa40e | |||
| 852cd5f0b9 | |||
| 1c16ac8c22 |
@@ -1,12 +1,12 @@
|
|||||||
mod ticket {
|
mod ticket {
|
||||||
struct Ticket {
|
pub struct Ticket {
|
||||||
title: String,
|
title: String,
|
||||||
description: String,
|
description: String,
|
||||||
status: String,
|
status: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Ticket {
|
impl Ticket {
|
||||||
fn new(title: String, description: String, status: String) -> Ticket {
|
pub fn new(title: String, description: String, status: String) -> Ticket {
|
||||||
if title.is_empty() {
|
if title.is_empty() {
|
||||||
panic!("Title cannot be empty");
|
panic!("Title cannot be empty");
|
||||||
}
|
}
|
||||||
@@ -55,7 +55,7 @@ mod tests {
|
|||||||
//
|
//
|
||||||
// TODO: Once you have verified that the below does not compile,
|
// TODO: Once you have verified that the below does not compile,
|
||||||
// comment the line out to move on to the next exercise!
|
// comment the line out to move on to the next exercise!
|
||||||
assert_eq!(ticket.description, "A description");
|
// assert_eq!(ticket.description, "A description");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encapsulation_cannot_be_violated() {
|
fn encapsulation_cannot_be_violated() {
|
||||||
@@ -68,10 +68,10 @@ mod tests {
|
|||||||
//
|
//
|
||||||
// TODO: Once you have verified that the below does not compile,
|
// TODO: Once you have verified that the below does not compile,
|
||||||
// comment the lines out to move on to the next exercise!
|
// comment the lines out to move on to the next exercise!
|
||||||
let ticket = Ticket {
|
// let ticket = Ticket {
|
||||||
title: "A title".into(),
|
// title: "A title".into(),
|
||||||
description: "A description".into(),
|
// description: "A description".into(),
|
||||||
status: "To-Do".into(),
|
// status: "To-Do".into(),
|
||||||
};
|
// };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,17 @@ pub mod ticket {
|
|||||||
// - `title` that returns the `title` field.
|
// - `title` that returns the `title` field.
|
||||||
// - `description` that returns the `description` field.
|
// - `description` that returns the `description` field.
|
||||||
// - `status` that returns the `status` field.
|
// - `status` that returns the `status` field.
|
||||||
|
pub fn title(self) -> String {
|
||||||
|
self.title
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn description(self) -> String {
|
||||||
|
self.description
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn status(self) -> String {
|
||||||
|
self.status
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,16 +34,16 @@ impl Ticket {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn title(self) -> String {
|
pub fn title(&self) -> &String {
|
||||||
self.title
|
&self.title
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn description(self) -> String {
|
pub fn description(&self) -> &String {
|
||||||
self.description
|
&self.description
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn status(self) -> String {
|
pub fn status(&self) -> &String {
|
||||||
self.status
|
&self.status
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,21 +11,9 @@ pub struct Ticket {
|
|||||||
|
|
||||||
impl Ticket {
|
impl Ticket {
|
||||||
pub fn new(title: String, description: String, status: String) -> Ticket {
|
pub fn new(title: String, description: String, status: String) -> Ticket {
|
||||||
if title.is_empty() {
|
validate_title(&title);
|
||||||
panic!("Title cannot be empty");
|
validate_description(&description);
|
||||||
}
|
validate_status(&status);
|
||||||
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 {
|
Ticket {
|
||||||
title,
|
title,
|
||||||
@@ -45,6 +33,45 @@ impl Ticket {
|
|||||||
pub fn status(&self) -> &String {
|
pub fn status(&self) -> &String {
|
||||||
&self.status
|
&self.status
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_title(&mut self, title: String) {
|
||||||
|
validate_title(&title);
|
||||||
|
self.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_description(&mut self, description: String) {
|
||||||
|
validate_description(&description);
|
||||||
|
self.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_status(&mut self, status: String) {
|
||||||
|
validate_status(&status);
|
||||||
|
self.status = status;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_title(title: &String) {
|
||||||
|
if title.is_empty() {
|
||||||
|
panic!("Title cannot be empty");
|
||||||
|
}
|
||||||
|
if title.len() > 50 {
|
||||||
|
panic!("Title cannot be longer than 50 bytes");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_description(description: &String) {
|
||||||
|
if description.is_empty() {
|
||||||
|
panic!("Description cannot be empty");
|
||||||
|
}
|
||||||
|
if description.len() > 500 {
|
||||||
|
panic!("Description cannot be longer than 500 bytes");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_status(status: &String) {
|
||||||
|
if status != "To-Do" && status != "In Progress" && status != "Done" {
|
||||||
|
panic!("Only `To-Do`, `In Progress`, and `Done` statuses are allowed");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
Reference in New Issue
Block a user