TicketManagement/BTreeMap/Task/src/lib.rs (83 lines of code) (raw):

// TODO: Replace `todo!()`s with the correct implementation. // Implement `IntoIterator` for `&TicketStore`. The iterator should yield immutable // references to the tickets, ordered by their `TicketId`. // Implement additional traits on `TicketId` if needed. use std::collections::BTreeMap; use std::ops::{Index, IndexMut}; use ticket_fields::{TicketDescription, TicketTitle}; #[derive(Clone)] pub struct TicketStore { tickets: BTreeMap<TicketId, Ticket>, counter: u64, } #[derive(Clone, Copy, Debug, PartialEq, Ord, PartialOrd, Eq)] pub struct TicketId(u64); #[derive(Clone, Debug, PartialEq)] pub struct Ticket { pub id: TicketId, pub title: TicketTitle, pub description: TicketDescription, pub status: Status, } #[derive(Clone, Debug, PartialEq, Eq)] pub struct TicketDraft { pub title: TicketTitle, pub description: TicketDescription, } #[derive(Clone, Debug, Copy, PartialEq, Eq)] pub enum Status { ToDo, InProgress, Done, } impl TicketStore { pub fn new() -> Self { Self { tickets: BTreeMap::new(), counter: 0, } } pub fn add_ticket(&mut self, ticket: TicketDraft) -> TicketId { let id = TicketId(self.counter); self.counter += 1; let ticket = Ticket { id, title: ticket.title, description: ticket.description, status: Status::ToDo, }; self.tickets.insert(id, ticket); id } pub fn get(&self, id: TicketId) -> Option<&Ticket> { self.tickets.get(&id) } pub fn get_mut(&mut self, id: TicketId) -> Option<&mut Ticket> { self.tickets.get_mut(&id) } } impl Index<TicketId> for TicketStore { type Output = Ticket; fn index(&self, index: TicketId) -> &Self::Output { self.get(index).unwrap() } } impl Index<&TicketId> for TicketStore { type Output = Ticket; fn index(&self, index: &TicketId) -> &Self::Output { &self[*index] } } impl IndexMut<TicketId> for TicketStore { fn index_mut(&mut self, index: TicketId) -> &mut Self::Output { self.get_mut(index).unwrap() } } impl IndexMut<&TicketId> for TicketStore { fn index_mut(&mut self, index: &TicketId) -> &mut Self::Output { &mut self[*index] } } 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() } }