Compare commits

...

2 Commits

Author SHA1 Message Date
9bc063a05b completed 06 ticket management
Some checks failed
CI / build (push) Failing after 1m16s
CI / gravity (push) Has been skipped
CI / is_fresh (push) Successful in 26s
CI / formatter (push) Failing after 17s
2025-04-07 12:12:47 +05:30
199b57aad8 completed till 11 2025-04-02 17:54:30 +05:30
9 changed files with 88 additions and 14 deletions

View File

@@ -7,6 +7,14 @@ pub struct TicketStore {
tickets: Vec<Ticket>, tickets: Vec<Ticket>,
} }
impl TicketStore {
fn in_progress(&self) -> impl Iterator<Item = &Ticket> {
self.tickets
.iter()
.filter(|&a| a.status == Status::InProgress)
}
}
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct Ticket { pub struct Ticket {
pub title: TicketTitle, pub title: TicketTitle,

View File

@@ -33,7 +33,10 @@ impl TicketStore {
// that can be infallibly converted into a `Ticket`. // that can be infallibly converted into a `Ticket`.
// This can make it nicer to use the method, as it removes the syntax noise of `.into()` // This can make it nicer to use the method, as it removes the syntax noise of `.into()`
// from the calling site. It can worsen the quality of the compiler error messages, though. // from the calling site. It can worsen the quality of the compiler error messages, though.
pub fn add_ticket(&mut self, ticket: impl Into<Ticket>) { pub fn add_ticket<T>(&mut self, ticket: T)
where
T: Into<Ticket>,
{
self.tickets.push(ticket.into()); self.tickets.push(ticket.into());
} }
} }

View File

@@ -1,6 +1,9 @@
// TODO: Define a function named `sum` that takes a reference to a slice of `u32` and returns the sum of all // TODO: Define a function named `sum` that takes a reference to a slice of `u32` and returns the sum of all
// elements in the slice. // elements in the slice.
fn sum(v: &[u32]) -> u32 {
v.iter().sum()
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@@ -1,6 +1,14 @@
// TODO: Define a function named `squared` that raises all `i32`s within a slice to the power of 2. // TODO: Define a function named `squared` that raises all `i32`s within a slice to the power of 2.
// The slice should be modified in place. // The slice should be modified in place.
fn squared(v: &mut [i32]) {
for val in v.iter_mut() {
*val = val.pow(2);
}
// let b = v.iter().map(|a| a.pow(2)).collect::<Vec<_>>().as_slice();
// v = b;
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@@ -11,6 +11,7 @@ use ticket_fields::{TicketDescription, TicketTitle};
#[derive(Clone)] #[derive(Clone)]
pub struct TicketStore { pub struct TicketStore {
tickets: Vec<Ticket>, tickets: Vec<Ticket>,
total: usize,
} }
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]
@@ -41,11 +42,25 @@ impl TicketStore {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
tickets: Vec::new(), tickets: Vec::new(),
total: 0,
} }
} }
pub fn add_ticket(&mut self, ticket: Ticket) { pub fn add_ticket(&mut self, ticket: TicketDraft) -> TicketId {
self.tickets.push(ticket); 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]);
} }
} }

View File

@@ -1,5 +1,7 @@
// TODO: Implement `Index<&TicketId>` and `Index<TicketId>` for `TicketStore`. // TODO: Implement `Index<&TicketId>` and `Index<TicketId>` for `TicketStore`.
use std::ops::Index;
use ticket_fields::{TicketDescription, TicketTitle}; use ticket_fields::{TicketDescription, TicketTitle};
#[derive(Clone)] #[derive(Clone)]
@@ -58,6 +60,20 @@ impl TicketStore {
} }
} }
impl Index<TicketId> 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)] #[cfg(test)]
mod tests { mod tests {
use crate::{Status, TicketDraft, TicketStore}; use crate::{Status, TicketDraft, TicketStore};

View File

@@ -1,6 +1,6 @@
// TODO: Implement `IndexMut<&TicketId>` and `IndexMut<TicketId>` for `TicketStore`. // TODO: Implement `IndexMut<&TicketId>` and `IndexMut<TicketId>` for `TicketStore`.
use std::ops::Index; use std::ops::{Index, IndexMut};
use ticket_fields::{TicketDescription, TicketTitle}; use ticket_fields::{TicketDescription, TicketTitle};
#[derive(Clone)] #[derive(Clone)]
@@ -75,6 +75,18 @@ impl Index<&TicketId> for TicketStore {
} }
} }
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()
}
}
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)] #[cfg(test)]
mod tests { mod tests {
use crate::{Status, TicketDraft, TicketStore}; use crate::{Status, TicketDraft, TicketStore};

View File

@@ -11,7 +11,7 @@ pub struct TicketStore {
counter: u64, counter: u64,
} }
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct TicketId(u64); pub struct TicketId(u64);
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
@@ -38,7 +38,7 @@ pub enum Status {
impl TicketStore { impl TicketStore {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
tickets: todo!(), tickets: HashMap::new(),
counter: 0, counter: 0,
} }
} }
@@ -52,16 +52,16 @@ impl TicketStore {
description: ticket.description, description: ticket.description,
status: Status::ToDo, status: Status::ToDo,
}; };
todo!(); self.tickets.insert(id, ticket);
id id
} }
pub fn get(&self, id: TicketId) -> Option<&Ticket> { pub fn get(&self, id: TicketId) -> Option<&Ticket> {
todo!() self.tickets.get(&id)
} }
pub fn get_mut(&mut self, id: TicketId) -> Option<&mut Ticket> { pub fn get_mut(&mut self, id: TicketId) -> Option<&mut Ticket> {
todo!() self.tickets.get_mut(&id)
} }
} }

View File

@@ -13,7 +13,16 @@ pub struct TicketStore {
counter: u64, 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); pub struct TicketId(u64);
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
@@ -40,7 +49,7 @@ pub enum Status {
impl TicketStore { impl TicketStore {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
tickets: todo!(), tickets: BTreeMap::new(),
counter: 0, counter: 0,
} }
} }
@@ -54,16 +63,16 @@ impl TicketStore {
description: ticket.description, description: ticket.description,
status: Status::ToDo, status: Status::ToDo,
}; };
todo!(); self.tickets.insert(id, ticket);
id id
} }
pub fn get(&self, id: TicketId) -> Option<&Ticket> { pub fn get(&self, id: TicketId) -> Option<&Ticket> {
todo!() self.tickets.get(&id)
} }
pub fn get_mut(&mut self, id: TicketId) -> Option<&mut Ticket> { pub fn get_mut(&mut self, id: TicketId) -> Option<&mut Ticket> {
todo!() self.tickets.get_mut(&id)
} }
} }