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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//! # Traits Module
//!
//! The `traits` module defines traits used by the `edtui_jagged` library for
//! specific functionalities.
use crate::{index::RowIndex, Index2, Jagged};

/// A helper trait used for indexing operations of a jagged array.
pub trait JaggedIndex<T> {
    type Output: Sized;

    fn get(self, array: &Jagged<T>) -> Option<&Self::Output>;
    fn get_mut(self, array: &mut Jagged<T>) -> Option<&mut Self::Output>;
}

pub trait JaggedRemove<T> {
    type Output: Sized;
    fn remove(self, array: &mut Jagged<T>) -> Self::Output;
}

impl<T> JaggedIndex<T> for Index2 {
    type Output = T;

    fn get(self, array: &Jagged<T>) -> Option<&Self::Output> {
        array.data.get(self.row).and_then(|line| line.get(self.col))
    }

    fn get_mut(self, array: &mut Jagged<T>) -> Option<&mut Self::Output> {
        array
            .data
            .get_mut(self.row)
            .and_then(|line| line.get_mut(self.col))
    }
}

impl<T> JaggedRemove<T> for Index2 {
    type Output = T;

    fn remove(self, array: &mut Jagged<T>) -> Self::Output {
        array.data[self.row].remove(self.col)
    }
}

impl<T> JaggedIndex<T> for RowIndex {
    type Output = Vec<T>;

    fn get(self, array: &Jagged<T>) -> Option<&Self::Output> {
        array.data.get(self.0)
    }

    fn get_mut(self, array: &mut Jagged<T>) -> Option<&mut Self::Output> {
        array.data.get_mut(self.0)
    }
}

impl<T> JaggedRemove<T> for RowIndex {
    type Output = Vec<T>;

    fn remove(self, array: &mut Jagged<T>) -> Self::Output {
        array.data.remove(self.0)
    }
}

/// A helper trait used for data operations of a jagged array.
pub trait JaggedSlice<T> {
    type Index: JaggedIndex<T>;
    fn push_into(self, array: &mut Jagged<T>);
    fn insert_into(self, index: Self::Index, array: &mut Jagged<T>);
}

/// An index representing a specific row in a jagged array.
#[derive(Default, Debug, PartialEq, Eq)]
pub struct RowSlice<T> {
    data: Vec<T>,
}

impl<T> From<Vec<T>> for RowSlice<T> {
    fn from(val: Vec<T>) -> Self {
        Self::new(val)
    }
}

impl<T> RowSlice<T> {
    /// Instantiates a new `RowSlice` from a vector.
    #[must_use]
    pub fn new(data: Vec<T>) -> Self {
        Self { data }
    }
}

impl<T> JaggedSlice<T> for T {
    type Index = Index2;

    fn push_into(self, array: &mut Jagged<T>) {
        if let Some(row) = array.get_mut(RowIndex::new(array.len().saturating_sub(1))) {
            row.push(self);
        }
    }

    fn insert_into(self, index: Self::Index, array: &mut Jagged<T>) {
        if let Some(line) = array.get_mut(RowIndex::new(index.row)) {
            line.insert(index.col, self);
        }
    }
}

impl<T> JaggedSlice<T> for RowSlice<T>
where
    T: std::fmt::Debug + Clone,
{
    type Index = RowIndex;

    fn push_into(self, array: &mut Jagged<T>) {
        array.data.push(self.data);
    }

    fn insert_into(self, index: Self::Index, array: &mut Jagged<T>) {
        array.data.insert(index.0, self.data);
    }
}

impl<T> JaggedSlice<T> for Vec<T> {
    type Index = RowIndex;

    fn push_into(self, array: &mut Jagged<T>) {
        array.data.push(self);
    }

    fn insert_into(self, index: Self::Index, array: &mut Jagged<T>) {
        array.data.insert(index.0, self);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    fn test_data() -> Jagged<char> {
        Jagged::<char>::from(
            "Hello\n\
            World",
        )
    }

    #[test]
    fn test_get_index() {
        let data = test_data();
        let index = Index2::new(0, 4);

        assert_eq!(index.get(&data), Some(&'o'));
    }

    #[test]
    fn test_get_row() {
        let data = test_data();
        let index = RowIndex::new(1);

        assert_eq!(index.get(&data), Some(&vec!['W', 'o', 'r', 'l', 'd']));
    }
}