1use serde::{Deserialize, Serialize};
2use std::fmt::Display;
3
4#[derive(Clone, Serialize, Deserialize, Debug)]
5pub enum SliceBegin {
6 Mark(String),
7 Index(usize),
8}
9
10#[derive(Clone, Serialize, Deserialize, Debug)]
11pub enum SliceEnd {
12 Mark(String),
13 Count(usize),
14}
15
16#[derive(Clone, Serialize, Deserialize, Debug)]
17pub struct Slice {
18 pub begin: SliceBegin,
19 pub end: SliceEnd,
20 pub rounds: Option<Vec<usize>>,
21}
22impl Default for Slice {
23 fn default() -> Self {
24 Self {
25 begin: SliceBegin::Index(0),
26 end: SliceEnd::Count(0),
27 rounds: None,
28 }
29 }
30}
31impl Slice {
32 pub fn new(begin: SliceBegin, end: SliceEnd, rounds: Option<Vec<usize>>) -> Self {
33 Self { begin, end, rounds }
34 }
35 pub fn not_in_round(&self, round: usize) -> bool {
36 self.rounds.is_some()
37 && self
38 .rounds
39 .clone()
40 .unwrap()
41 .iter()
42 .find(|&x| *x == round)
43 .is_none()
44 }
45 pub fn in_round(&self, round: usize) -> bool {
46 !self.not_in_round(round)
47 }
48}
49impl Display for SliceBegin {
50 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51 write!(f, "{:?}", self)
52 }
53}
54impl Display for SliceEnd {
55 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56 write!(f, "{:?}", self)
57 }
58}
59impl Display for Slice {
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 write!(f, "<Slice>({}-{}", self.begin, self.end)?;
62 if let Some(ref rounds) = self.rounds {
63 write!(f, " R:{:?}", rounds)?;
64 }
65 write!(f, ")")
66 }
67}