From 9161d08b1ca0e6743d9da628d5a8b4cf5da8fe99 Mon Sep 17 00:00:00 2001 From: Phani Pavan K Date: Mon, 24 Feb 2025 15:53:03 +0530 Subject: [PATCH] completed 03/[00-07] --- exercises/03_ticket_v1/00_intro/src/lib.rs | 2 +- exercises/03_ticket_v1/01_struct/src/lib.rs | 11 +++ .../03_ticket_v1/02_validation/src/lib.rs | 18 ++++- exercises/03_ticket_v1/03_modules/src/lib.rs | 2 +- .../03_ticket_v1/04_visibility/src/lib.rs | 16 ++--- .../03_ticket_v1/05_encapsulation/src/lib.rs | 10 +++ .../03_ticket_v1/06_ownership/src/lib.rs | 12 ++-- exercises/03_ticket_v1/07_setters/src/lib.rs | 70 ++++++++++++++++--- 8 files changed, 116 insertions(+), 25 deletions(-) diff --git a/exercises/03_ticket_v1/00_intro/src/lib.rs b/exercises/03_ticket_v1/00_intro/src/lib.rs index f7afa47..c52e3ac 100644 --- a/exercises/03_ticket_v1/00_intro/src/lib.rs +++ b/exercises/03_ticket_v1/00_intro/src/lib.rs @@ -1,6 +1,6 @@ fn intro() -> &'static str { // TODO: fix me 👇 - "I'm ready to __!" + "I'm ready to start modelling a software ticket!" } #[cfg(test)] diff --git a/exercises/03_ticket_v1/01_struct/src/lib.rs b/exercises/03_ticket_v1/01_struct/src/lib.rs index 1119e33..6861577 100644 --- a/exercises/03_ticket_v1/01_struct/src/lib.rs +++ b/exercises/03_ticket_v1/01_struct/src/lib.rs @@ -5,6 +5,17 @@ // It should also have a method named `is_available` that returns a `true` if the quantity is // greater than 0, otherwise `false`. +struct Order{ + price: u32, + quantity: u32, +} + +impl Order{ + fn is_available(self)-> bool{ + self.quantity >0 + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/03_ticket_v1/02_validation/src/lib.rs b/exercises/03_ticket_v1/02_validation/src/lib.rs index 7eaa5e5..2ae9c3f 100644 --- a/exercises/03_ticket_v1/02_validation/src/lib.rs +++ b/exercises/03_ticket_v1/02_validation/src/lib.rs @@ -18,7 +18,23 @@ impl Ticket { // as well as some `String` methods. Use the documentation of Rust's standard library // to find the most appropriate options -> https://doc.rust-lang.org/std/string/struct.String.html fn new(title: String, description: String, status: String) -> Self { - todo!(); + if !(status.eq("To-Do") || status.eq("In Progress") || status.eq("Done")) { + panic!("Only `To-Do`, `In Progress`, and `Done` statuses are allowed"); + } + + if title.is_empty() { + panic!("Title cannot be empty") + } + if description.is_empty() { + panic!("Description cannot be empty") + } + + if description.len() > 500 { + panic!("Description cannot be longer than 500 bytes"); + } + if title.len() > 50 { + panic!("Title cannot be longer than 50 bytes"); + } Self { title, description, diff --git a/exercises/03_ticket_v1/03_modules/src/lib.rs b/exercises/03_ticket_v1/03_modules/src/lib.rs index df07620..24e431e 100644 --- a/exercises/03_ticket_v1/03_modules/src/lib.rs +++ b/exercises/03_ticket_v1/03_modules/src/lib.rs @@ -1,7 +1,7 @@ mod helpers { // TODO: Make this code compile, either by adding a `use` statement or by using // the appropriate path to refer to the `Ticket` struct. - + use super::Ticket; fn create_todo_ticket(title: String, description: String) -> Ticket { Ticket::new(title, description, "To-Do".into()) } diff --git a/exercises/03_ticket_v1/04_visibility/src/lib.rs b/exercises/03_ticket_v1/04_visibility/src/lib.rs index b494fc9..65a1593 100644 --- a/exercises/03_ticket_v1/04_visibility/src/lib.rs +++ b/exercises/03_ticket_v1/04_visibility/src/lib.rs @@ -1,12 +1,12 @@ mod ticket { - struct Ticket { + pub struct Ticket { title: String, description: String, status: String, } 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() { panic!("Title cannot be empty"); } @@ -55,7 +55,7 @@ mod tests { // // TODO: Once you have verified that the below does not compile, // 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() { @@ -68,10 +68,10 @@ mod tests { // // TODO: Once you have verified that the below does not compile, // comment the lines out to move on to the next exercise! - let ticket = Ticket { - title: "A title".into(), - description: "A description".into(), - status: "To-Do".into(), - }; + // let ticket = Ticket { + // title: "A title".into(), + // description: "A description".into(), + // status: "To-Do".into(), + // }; } } diff --git a/exercises/03_ticket_v1/05_encapsulation/src/lib.rs b/exercises/03_ticket_v1/05_encapsulation/src/lib.rs index 91e06eb..3ec2e7c 100644 --- a/exercises/03_ticket_v1/05_encapsulation/src/lib.rs +++ b/exercises/03_ticket_v1/05_encapsulation/src/lib.rs @@ -29,6 +29,16 @@ pub mod ticket { status, } } + pub fn title(self) -> String { + self.title + } + pub fn description(self) -> String { + self.description + } + + pub fn status(self) -> String { + self.status + } // TODO: Add three public methods to the `Ticket` struct: // - `title` that returns the `title` field. diff --git a/exercises/03_ticket_v1/06_ownership/src/lib.rs b/exercises/03_ticket_v1/06_ownership/src/lib.rs index a9639b2..f110ea8 100644 --- a/exercises/03_ticket_v1/06_ownership/src/lib.rs +++ b/exercises/03_ticket_v1/06_ownership/src/lib.rs @@ -34,16 +34,16 @@ impl Ticket { } } - pub fn title(self) -> String { - self.title + pub fn title(&self) -> String { + self.title.clone() } - pub fn description(self) -> String { - self.description + pub fn description(&self) -> String { + self.description.clone() } - pub fn status(self) -> String { - self.status + pub fn status(&self) -> String { + self.status.clone() } } diff --git a/exercises/03_ticket_v1/07_setters/src/lib.rs b/exercises/03_ticket_v1/07_setters/src/lib.rs index e13ec87..e034277 100644 --- a/exercises/03_ticket_v1/07_setters/src/lib.rs +++ b/exercises/03_ticket_v1/07_setters/src/lib.rs @@ -10,23 +10,62 @@ pub struct Ticket { } impl Ticket { - pub fn new(title: String, description: String, status: String) -> Ticket { - if title.is_empty() { + fn cTitleEmpty(newString: &String) -> bool { + if newString.is_empty() { panic!("Title cannot be empty"); } - if title.len() > 50 { + false + } + + fn cTitleTooLong(newString: &String) -> bool { + if newString.len() > 50 { panic!("Title cannot be longer than 50 bytes"); } - if description.is_empty() { - panic!("Description cannot be empty"); - } - if description.len() > 500 { + false + } + + fn cDescTooLong(newDesc: &String) -> bool { + if newDesc.len() > 500 { panic!("Description cannot be longer than 500 bytes"); } - if status != "To-Do" && status != "In Progress" && status != "Done" { + false + } + + fn cDescEmpty(newDesc: &String) -> bool { + if newDesc.is_empty() { + panic!("Description cannot be empty"); + } + false + } + + fn cInvalidStatus(newStatus: &String) -> bool { + if newStatus != "To-Do" && newStatus != "In Progress" && newStatus != "Done" { panic!("Only `To-Do`, `In Progress`, and `Done` statuses are allowed"); } + false + } + 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::cTitleTooLong(&title); + Ticket::cTitleTooLong(&title); + Ticket::cDescEmpty(&description); + Ticket::cDescTooLong(&description); + Ticket::cInvalidStatus(&status); Ticket { title, description, @@ -34,6 +73,21 @@ impl Ticket { } } + pub fn set_title(&mut self, newTitle: String) { + Ticket::cTitleEmpty(&newTitle); + Ticket::cTitleTooLong(&newTitle); + self.title = newTitle; + } + pub fn set_description(&mut self, newDesc: String) { + Ticket::cDescEmpty(&newDesc); + Ticket::cDescTooLong(&newDesc); + self.description = newDesc; + } + + pub fn set_status(&mut self, newStatus: String) { + Ticket::cInvalidStatus(&newStatus); + self.status = newStatus; + } pub fn title(&self) -> &String { &self.title }