From 9bc063a05b6a0278dde703ea1ad0ae7b1ab10fc3 Mon Sep 17 00:00:00 2001 From: Phani Pavan K Date: Mon, 7 Apr 2025 12:12:47 +0530 Subject: [PATCH] completed 06 ticket management --- .../12_two_states/src/lib.rs | 19 +++++++++++++++++-- .../06_ticket_management/13_index/src/lib.rs | 16 ++++++++++++++++ .../14_index_mut/src/lib.rs | 14 +++++++++++++- .../15_hashmap/src/lib.rs | 10 +++++----- .../16_btreemap/src/lib.rs | 19 ++++++++++++++----- 5 files changed, 65 insertions(+), 13 deletions(-) diff --git a/exercises/06_ticket_management/12_two_states/src/lib.rs b/exercises/06_ticket_management/12_two_states/src/lib.rs index 10b5cb1..1a3b0eb 100644 --- a/exercises/06_ticket_management/12_two_states/src/lib.rs +++ b/exercises/06_ticket_management/12_two_states/src/lib.rs @@ -11,6 +11,7 @@ use ticket_fields::{TicketDescription, TicketTitle}; #[derive(Clone)] pub struct TicketStore { tickets: Vec, + total: usize, } #[derive(Clone, Copy, Debug, PartialEq)] @@ -41,11 +42,25 @@ impl TicketStore { pub fn new() -> Self { Self { tickets: Vec::new(), + total: 0, } } - pub fn add_ticket(&mut self, ticket: Ticket) { - self.tickets.push(ticket); + pub fn add_ticket(&mut self, ticket: TicketDraft) -> TicketId { + let newID = TicketId(self.total as u64 + 1); + let tick: Ticket = Ticket { + title: ticket.title, + description: ticket.description, + status: Status::ToDo, + id: newID, + }; + self.tickets.push(tick); + self.total += 1; + return newID; + } + + pub fn get(&self, id: TicketId) -> Option<&Ticket> { + return Some(&self.tickets[(id.0 - 1) as usize]); } } diff --git a/exercises/06_ticket_management/13_index/src/lib.rs b/exercises/06_ticket_management/13_index/src/lib.rs index 0b08cc1..0a7c840 100644 --- a/exercises/06_ticket_management/13_index/src/lib.rs +++ b/exercises/06_ticket_management/13_index/src/lib.rs @@ -1,5 +1,7 @@ // TODO: Implement `Index<&TicketId>` and `Index` for `TicketStore`. +use std::ops::Index; + use ticket_fields::{TicketDescription, TicketTitle}; #[derive(Clone)] @@ -58,6 +60,20 @@ impl TicketStore { } } +impl Index for TicketStore { + type Output = Ticket; + fn index(&self, index: TicketId) -> &Self::Output { + return &self.tickets[index.0 as usize]; + } +} + +impl Index<&TicketId> for TicketStore { + type Output = Ticket; + fn index(&self, index: &TicketId) -> &Self::Output { + return &self.tickets[index.0 as usize]; + } +} + #[cfg(test)] mod tests { use crate::{Status, TicketDraft, TicketStore}; diff --git a/exercises/06_ticket_management/14_index_mut/src/lib.rs b/exercises/06_ticket_management/14_index_mut/src/lib.rs index fa39451..2fd71d6 100644 --- a/exercises/06_ticket_management/14_index_mut/src/lib.rs +++ b/exercises/06_ticket_management/14_index_mut/src/lib.rs @@ -1,6 +1,6 @@ // TODO: Implement `IndexMut<&TicketId>` and `IndexMut` for `TicketStore`. -use std::ops::Index; +use std::ops::{Index, IndexMut}; use ticket_fields::{TicketDescription, TicketTitle}; #[derive(Clone)] @@ -75,6 +75,18 @@ impl Index<&TicketId> for TicketStore { } } +impl IndexMut for TicketStore { + fn index_mut(&mut self, index: TicketId) -> &mut Self::Output { + self.tickets.iter_mut().find(|a| a.id == index).unwrap() + } +} + +impl IndexMut<&TicketId> for TicketStore { + fn index_mut(&mut self, index: &TicketId) -> &mut Self::Output { + self.tickets.iter_mut().find(|a| a.id == *index).unwrap() + } +} + #[cfg(test)] mod tests { use crate::{Status, TicketDraft, TicketStore}; diff --git a/exercises/06_ticket_management/15_hashmap/src/lib.rs b/exercises/06_ticket_management/15_hashmap/src/lib.rs index ea109a6..c50fb57 100644 --- a/exercises/06_ticket_management/15_hashmap/src/lib.rs +++ b/exercises/06_ticket_management/15_hashmap/src/lib.rs @@ -11,7 +11,7 @@ pub struct TicketStore { counter: u64, } -#[derive(Clone, Copy, Debug, PartialEq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct TicketId(u64); #[derive(Clone, Debug, PartialEq)] @@ -38,7 +38,7 @@ pub enum Status { impl TicketStore { pub fn new() -> Self { Self { - tickets: todo!(), + tickets: HashMap::new(), counter: 0, } } @@ -52,16 +52,16 @@ impl TicketStore { description: ticket.description, status: Status::ToDo, }; - todo!(); + self.tickets.insert(id, ticket); id } pub fn get(&self, id: TicketId) -> Option<&Ticket> { - todo!() + self.tickets.get(&id) } pub fn get_mut(&mut self, id: TicketId) -> Option<&mut Ticket> { - todo!() + self.tickets.get_mut(&id) } } diff --git a/exercises/06_ticket_management/16_btreemap/src/lib.rs b/exercises/06_ticket_management/16_btreemap/src/lib.rs index d9af95d..b21e078 100644 --- a/exercises/06_ticket_management/16_btreemap/src/lib.rs +++ b/exercises/06_ticket_management/16_btreemap/src/lib.rs @@ -13,7 +13,16 @@ pub struct TicketStore { counter: u64, } -#[derive(Clone, Copy, Debug, PartialEq)] +impl<'a> IntoIterator for &'a TicketStore { + type Item = &'a Ticket; + type IntoIter = std::collections::btree_map::Values<'a, TicketId, Ticket>; + + fn into_iter(self) -> Self::IntoIter { + self.tickets.values() + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Ord, PartialOrd)] pub struct TicketId(u64); #[derive(Clone, Debug, PartialEq)] @@ -40,7 +49,7 @@ pub enum Status { impl TicketStore { pub fn new() -> Self { Self { - tickets: todo!(), + tickets: BTreeMap::new(), counter: 0, } } @@ -54,16 +63,16 @@ impl TicketStore { description: ticket.description, status: Status::ToDo, }; - todo!(); + self.tickets.insert(id, ticket); id } pub fn get(&self, id: TicketId) -> Option<&Ticket> { - todo!() + self.tickets.get(&id) } pub fn get_mut(&mut self, id: TicketId) -> Option<&mut Ticket> { - todo!() + self.tickets.get_mut(&id) } }