initial commit
This commit is contained in:
7
exercises/06_ticket_management/06_lifetimes/Cargo.toml
Normal file
7
exercises/06_ticket_management/06_lifetimes/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "lifetime"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
ticket_fields = { path = "../../../helpers/ticket_fields" }
|
||||
66
exercises/06_ticket_management/06_lifetimes/src/lib.rs
Normal file
66
exercises/06_ticket_management/06_lifetimes/src/lib.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
use ticket_fields::{TicketDescription, TicketTitle};
|
||||
|
||||
// TODO: Implement the `IntoIterator` trait for `&TicketStore` so that the test compiles and passes.
|
||||
#[derive(Clone)]
|
||||
pub struct TicketStore {
|
||||
tickets: Vec<Ticket>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Ticket {
|
||||
pub title: TicketTitle,
|
||||
pub description: TicketDescription,
|
||||
pub status: Status,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Copy, PartialEq)]
|
||||
pub enum Status {
|
||||
ToDo,
|
||||
InProgress,
|
||||
Done,
|
||||
}
|
||||
|
||||
impl TicketStore {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
tickets: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_ticket(&mut self, ticket: Ticket) {
|
||||
self.tickets.push(ticket);
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> std::slice::Iter<Ticket> {
|
||||
self.tickets.iter()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use ticket_fields::test_helpers::{ticket_description, ticket_title};
|
||||
|
||||
#[test]
|
||||
fn add_ticket() {
|
||||
let mut store = TicketStore::new();
|
||||
|
||||
let ticket = Ticket {
|
||||
title: ticket_title(),
|
||||
description: ticket_description(),
|
||||
status: Status::ToDo,
|
||||
};
|
||||
store.add_ticket(ticket);
|
||||
|
||||
let ticket = Ticket {
|
||||
title: ticket_title(),
|
||||
description: ticket_description(),
|
||||
status: Status::InProgress,
|
||||
};
|
||||
store.add_ticket(ticket);
|
||||
|
||||
let tickets: Vec<&Ticket> = store.iter().collect();
|
||||
let tickets2: Vec<&Ticket> = (&store).into_iter().collect();
|
||||
assert_eq!(tickets, tickets2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user