Compare commits

...

4 Commits

Author SHA1 Message Date
1f0823a6a4 solution to exercise 03_07 2024-06-23 18:22:03 +02:00
56075fa40e solution to exercise 03_06 2024-06-23 18:08:59 +02:00
852cd5f0b9 solution to exercise 03_05 2024-06-23 17:58:03 +02:00
1c16ac8c22 solution to exercise 03_04 2024-06-23 17:54:39 +02:00
4 changed files with 67 additions and 29 deletions

View File

@@ -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(),
}; // };
} }
} }

View File

@@ -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
}
} }
} }

View File

@@ -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
} }
} }

View File

@@ -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)]