-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
475 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use std::fmt::Debug; | ||
use std::fmt::Display; | ||
use std::ops::Range; | ||
use std::ops::RangeFrom; | ||
use std::ops::RangeFull; | ||
use std::ops::RangeInclusive; | ||
use std::ops::RangeTo; | ||
use std::ops::RangeToInclusive; | ||
use std::slice::SliceIndex; | ||
|
||
use crate::Event; | ||
|
||
/// A checkpoint in a `ghcid-ng` run. | ||
/// | ||
/// [`crate::GhcidNg`] provides methods for asserting that events are logged, or waiting for | ||
/// events to be logged in the future. | ||
/// | ||
/// To avoid searching thousands of log events for each assertion, and to provide greater | ||
/// granularity for assertions, you can additionally assert that events are logged between | ||
/// particular checkpoints. | ||
/// | ||
/// Checkpoints can be constructed with [`crate::GhcidNg::first_checkpoint`], | ||
/// [`crate::GhcidNg::current_checkpoint`], and [`crate::GhcidNg::checkpoint`]. | ||
#[derive(Debug, Clone, Copy)] | ||
pub struct Checkpoint(pub(crate) usize); | ||
|
||
impl Checkpoint { | ||
/// Convert this checkpoint into an index. | ||
pub fn into_index(self) -> usize { | ||
self.0 | ||
} | ||
} | ||
|
||
impl Display for Checkpoint { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.0) | ||
} | ||
} | ||
|
||
/// A type that can be used to index a set of checkpoints. | ||
pub trait CheckpointIndex: Clone { | ||
/// The resulting index type. | ||
type Index: SliceIndex<[Vec<Event>], Output = [Vec<Event>]> + Debug; | ||
|
||
/// Convert the value into an index. | ||
fn into_index(self) -> Self::Index; | ||
} | ||
|
||
impl CheckpointIndex for Checkpoint { | ||
type Index = RangeInclusive<usize>; | ||
|
||
fn into_index(self) -> Self::Index { | ||
let index = self.into_index(); | ||
index..=index | ||
} | ||
} | ||
|
||
impl CheckpointIndex for Range<Checkpoint> { | ||
type Index = Range<usize>; | ||
|
||
fn into_index(self) -> Self::Index { | ||
self.start.into_index()..self.end.into_index() | ||
} | ||
} | ||
|
||
impl CheckpointIndex for RangeFrom<Checkpoint> { | ||
type Index = RangeFrom<usize>; | ||
|
||
fn into_index(self) -> Self::Index { | ||
self.start.into_index().. | ||
} | ||
} | ||
|
||
impl CheckpointIndex for RangeFull { | ||
type Index = RangeFull; | ||
|
||
fn into_index(self) -> Self::Index { | ||
self | ||
} | ||
} | ||
|
||
impl CheckpointIndex for RangeInclusive<Checkpoint> { | ||
type Index = RangeInclusive<usize>; | ||
|
||
fn into_index(self) -> Self::Index { | ||
self.start().into_index()..=self.end().into_index() | ||
} | ||
} | ||
|
||
impl CheckpointIndex for RangeTo<Checkpoint> { | ||
type Index = RangeTo<usize>; | ||
|
||
fn into_index(self) -> Self::Index { | ||
..self.end.into_index() | ||
} | ||
} | ||
|
||
impl CheckpointIndex for RangeToInclusive<Checkpoint> { | ||
type Index = RangeToInclusive<usize>; | ||
|
||
fn into_index(self) -> Self::Index { | ||
..=self.end.into_index() | ||
} | ||
} |
Oops, something went wrong.