100 exercises to learn Rust
This commit is contained in:
71
exercises/06_ticket_management/05_iter/src/lib.rs
Normal file
71
exercises/06_ticket_management/05_iter/src/lib.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
use ticket_fields::{TicketDescription, TicketTitle};
|
||||
|
||||
// TODO: Provide an `iter` method that returns an iterator over `&Ticket` items.
|
||||
#[derive(Clone)]
|
||||
pub struct TicketStore {
|
||||
tickets: Vec<Ticket>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Ticket {
|
||||
title: TicketTitle,
|
||||
description: TicketDescription,
|
||||
status: Status,
|
||||
}
|
||||
|
||||
impl IntoIterator for &TicketStore {
|
||||
type Item = &Ticket;
|
||||
type IntoIter = impl Iterator<Item = &Ticket>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.tickets.iter()
|
||||
}
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
}
|
||||
|
||||
#[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.iter().collect();
|
||||
assert_eq!(tickets, tickets2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user