From 6a820cbbfeb881397b388da917de084e6fa544a2 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Thu, 24 Oct 2024 16:16:23 +0200 Subject: [PATCH] Exercise 07-07 --- exercises/07_threads/07_ack/src/lib.rs | 35 ++++++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/exercises/07_threads/07_ack/src/lib.rs b/exercises/07_threads/07_ack/src/lib.rs index 746c355..9eb02dc 100644 --- a/exercises/07_threads/07_ack/src/lib.rs +++ b/exercises/07_threads/07_ack/src/lib.rs @@ -1,4 +1,7 @@ +use data::{Ticket, TicketDraft}; use std::sync::mpsc::{Receiver, Sender}; +use store::TicketId; + use crate::store::TicketStore; pub mod data; @@ -6,8 +9,14 @@ pub mod store; // Refer to the tests to understand the expected schema. pub enum Command { - Insert { todo!() }, - Get { todo!() } + Insert { + draft: TicketDraft, + response_sender: Sender, + }, + Get { + id: TicketId, + response_sender: Sender>, + }, } pub fn launch() -> Sender { @@ -21,19 +30,29 @@ pub fn server(receiver: Receiver) { let mut store = TicketStore::new(); loop { match receiver.recv() { - Ok(Command::Insert {}) => { - todo!() + Ok(Command::Insert { + draft, + response_sender, + }) => { + let ticket_id = store.add_ticket(draft); + response_sender + .send(ticket_id) + .expect("Cannot send response"); } Ok(Command::Get { - todo!() + id, + response_sender, }) => { - todo!() + let ticket = store.get(id); + response_sender + .send(ticket.cloned()) + .expect("Cannot send response"); } Err(_) => { // There are no more senders, so we can safely break // and shut down the server. - break - }, + break; + } } } } -- 2.45.2