summaryrefslogtreecommitdiffstats
path: root/src/minusplus.rs
blob: e37c801c14ac647ae2798c0f136dc0323c6ecab2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use std::ops::{Index, IndexMut};

/// Represent data related to removed/minus and added/plus lines which
/// can be indexed with [`MinusPlusIndex::{Plus`](MinusPlusIndex::Plus)`,`[`Minus}`](MinusPlusIndex::Minus).
#[derive(Debug, PartialEq, Eq)]
pub struct MinusPlus<T> {
    pub minus: T,
    pub plus: T,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MinusPlusIndex {
    Minus,
    Plus,
}

pub use MinusPlusIndex::*;

impl<T> Index<MinusPlusIndex> for MinusPlus<T> {
    type Output = T;
    fn index(&self, side: MinusPlusIndex) -> &Self::Output {
        match side {
            Minus => &self.minus,
            Plus => &self.plus,
        }
    }
}

impl<T> IndexMut<MinusPlusIndex> for MinusPlus<T> {
    fn index_mut(&mut self, side: MinusPlusIndex) -> &mut Self::Output {
        match side {
            Minus => &mut self.minus,
            Plus => &mut self.plus,
        }
    }
}

impl<T> MinusPlus<T> {
    pub fn new(minus: T, plus: T) -> Self {
        MinusPlus { minus, plus }
    }
}

impl<T: Default> Default for MinusPlus<T> {
    fn default() -> Self {
        Self {
            minus: T::default(),
            plus: T::default(),
        }
    }
}