initial commit

This commit is contained in:
2025-02-19 10:34:15 +05:30
commit b9cb4c290a
355 changed files with 18626 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
[package]
name = "without_channels"
version = "0.1.0"
edition = "2021"
[dependencies]
ticket_fields = { path = "../../../helpers/ticket_fields" }

View 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,
}

View File

@@ -0,0 +1,7 @@
// TODO: You don't actually have to change anything in the library itself!
// We mostly had to **remove** code (the client type, the launch function, the command enum)
// that's no longer necessary.
// Fix the `todo!()` in the testing code and see how the new design can be used.
pub mod data;
pub mod store;

View File

@@ -0,0 +1,40 @@
use std::collections::BTreeMap;
use std::sync::{Arc, RwLock};
use crate::data::{Status, Ticket, TicketDraft};
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct TicketId(u64);
#[derive(Clone)]
pub struct TicketStore {
tickets: BTreeMap<TicketId, Arc<RwLock<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,
};
let ticket = Arc::new(RwLock::new(ticket));
self.tickets.insert(id, ticket);
id
}
pub fn get(&self, id: TicketId) -> Option<Arc<RwLock<Ticket>>> {
self.tickets.get(&id).cloned()
}
}

View File

@@ -0,0 +1,40 @@
use std::sync::{Arc, RwLock};
use std::thread::spawn;
use ticket_fields::test_helpers::{ticket_description, ticket_title};
use without_channels::data::TicketDraft;
use without_channels::store::TicketStore;
#[test]
fn works() {
let store = todo!();
let store1 = store.clone();
let client1 = spawn(move || {
let draft = TicketDraft {
title: ticket_title(),
description: ticket_description(),
};
store1.write().unwrap().add_ticket(draft)
});
let store2 = store.clone();
let client2 = spawn(move || {
let draft = TicketDraft {
title: ticket_title(),
description: ticket_description(),
};
store2.write().unwrap().add_ticket(draft)
});
let ticket_id1 = client1.join().unwrap();
let ticket_id2 = client2.join().unwrap();
let reader = store.read().unwrap();
let ticket1 = reader.get(ticket_id1).unwrap();
assert_eq!(ticket_id1, ticket1.read().unwrap().id);
let ticket2 = reader.get(ticket_id2).unwrap();
assert_eq!(ticket_id2, ticket2.read().unwrap().id);
}