cortexkit_usage_steps/
latest.rs1use std::collections::HashMap;
2
3use crate::{Cursor, ReadReply, StepRow};
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum Applied {
9 Added,
11 Replaced,
13}
14
15#[derive(Clone, Debug, Default)]
24pub struct LatestRows {
25 rows: Vec<StepRow>,
26 index: HashMap<(String, String), usize>,
27 resume_after: Option<Cursor>,
28}
29
30impl LatestRows {
31 pub fn new() -> Self {
32 Self::default()
33 }
34
35 pub fn apply(&mut self, row: StepRow) -> Applied {
37 let key = (row.harness.clone(), row.step_ref.clone());
38 match self.index.get(&key) {
39 Some(&slot) => {
40 self.rows[slot] = row;
41 Applied::Replaced
42 }
43 None => {
44 self.index.insert(key, self.rows.len());
45 self.rows.push(row);
46 Applied::Added
47 }
48 }
49 }
50
51 pub fn apply_reply(&mut self, reply: ReadReply) {
54 self.resume_after = Some(reply.resume_after().clone());
55 for row in reply.rows {
56 self.apply(row);
57 }
58 }
59
60 pub fn resume_after(&self) -> Option<&Cursor> {
63 self.resume_after.as_ref()
64 }
65
66 pub fn get(&self, harness: &str, step_ref: &str) -> Option<&StepRow> {
68 self.index
69 .get(&(harness.to_owned(), step_ref.to_owned()))
70 .map(|&slot| &self.rows[slot])
71 }
72
73 pub fn iter(&self) -> impl Iterator<Item = &StepRow> {
76 self.rows.iter()
77 }
78
79 pub fn len(&self) -> usize {
80 self.rows.len()
81 }
82
83 pub fn is_empty(&self) -> bool {
84 self.rows.is_empty()
85 }
86
87 pub fn clear(&mut self) {
89 *self = Self::default();
90 }
91}