initial commit
This commit is contained in:
7
exercises/07_threads/08_client/Cargo.toml
Normal file
7
exercises/07_threads/08_client/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "client"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
ticket_fields = { path = "../../../helpers/ticket_fields" }
|
||||
23
exercises/07_threads/08_client/src/data.rs
Normal file
23
exercises/07_threads/08_client/src/data.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use crate::store::TicketId;
|
||||
use ticket_fields::{TicketDescription, TicketTitle};
|
||||
|
||||
#[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,
|
||||
}
|
||||
66
exercises/07_threads/08_client/src/lib.rs
Normal file
66
exercises/07_threads/08_client/src/lib.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
use crate::data::{Ticket, TicketDraft};
|
||||
use crate::store::{TicketId, TicketStore};
|
||||
use std::sync::mpsc::{Receiver, Sender};
|
||||
|
||||
pub mod data;
|
||||
pub mod store;
|
||||
|
||||
#[derive(Clone)]
|
||||
// TODO: flesh out the client implementation.
|
||||
pub struct TicketStoreClient {}
|
||||
|
||||
impl TicketStoreClient {
|
||||
// Feel free to panic on all errors, for simplicity.
|
||||
pub fn insert(&self, draft: TicketDraft) -> TicketId {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn get(&self, id: TicketId) -> Option<Ticket> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn launch() -> TicketStoreClient {
|
||||
let (sender, receiver) = std::sync::mpsc::channel();
|
||||
std::thread::spawn(move || server(receiver));
|
||||
todo!()
|
||||
}
|
||||
|
||||
// No longer public! This becomes an internal detail of the library now.
|
||||
enum Command {
|
||||
Insert {
|
||||
draft: TicketDraft,
|
||||
response_channel: Sender<TicketId>,
|
||||
},
|
||||
Get {
|
||||
id: TicketId,
|
||||
response_channel: Sender<Option<Ticket>>,
|
||||
},
|
||||
}
|
||||
|
||||
fn server(receiver: Receiver<Command>) {
|
||||
let mut store = TicketStore::new();
|
||||
loop {
|
||||
match receiver.recv() {
|
||||
Ok(Command::Insert {
|
||||
draft,
|
||||
response_channel,
|
||||
}) => {
|
||||
let id = store.add_ticket(draft);
|
||||
let _ = response_channel.send(id);
|
||||
}
|
||||
Ok(Command::Get {
|
||||
id,
|
||||
response_channel,
|
||||
}) => {
|
||||
let ticket = store.get(id);
|
||||
let _ = response_channel.send(ticket.cloned());
|
||||
}
|
||||
Err(_) => {
|
||||
// There are no more senders, so we can safely break
|
||||
// and shut down the server.
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
37
exercises/07_threads/08_client/src/store.rs
Normal file
37
exercises/07_threads/08_client/src/store.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use crate::data::{Status, Ticket, TicketDraft};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct TicketId(u64);
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TicketStore {
|
||||
tickets: BTreeMap<TicketId, Ticket>,
|
||||
counter: u64,
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
21
exercises/07_threads/08_client/tests/insert.rs
Normal file
21
exercises/07_threads/08_client/tests/insert.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use client::data::{Status, TicketDraft};
|
||||
use client::launch;
|
||||
use ticket_fields::test_helpers::{ticket_description, ticket_title};
|
||||
|
||||
#[test]
|
||||
fn insert_works() {
|
||||
// Notice how much simpler the test is now that we have a client to handle the details!
|
||||
let client = launch();
|
||||
let draft = TicketDraft {
|
||||
title: ticket_title(),
|
||||
description: ticket_description(),
|
||||
};
|
||||
let ticket_id = client.insert(draft.clone());
|
||||
|
||||
let client2 = client.clone();
|
||||
let ticket = client2.get(ticket_id).unwrap();
|
||||
assert_eq!(ticket_id, ticket.id);
|
||||
assert_eq!(ticket.status, Status::ToDo);
|
||||
assert_eq!(ticket.title, draft.title);
|
||||
assert_eq!(ticket.description, draft.description);
|
||||
}
|
||||
Reference in New Issue
Block a user