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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/// What directions [`SliceExt`]`::{`[`slice_lossy`]`, `[`slice_lossy_mut`]`}` are biased towards.
///
/// For `str` this has the effect of going in those directions if
/// the `start` and/or `end` bound is between char boundaries.
///
/// For `[T]` this has no effect and
/// it is recommended to use `()` as a parameter instead
/// (`()` is converted to [`SliceBias::OUT`](#associatedconstant.OUT)).
///
/// # Example
/// 
/// ### String
/// 
/// ```
/// use core_extensions::SliceExt;
/// use core_extensions::slices::SliceBias;
///
/// let word = "niño";
///
/// assert_eq!(
///     word.char_indices().collect::<Vec<_>>(),
///     &[(0, 'n'), (1, 'i'), (2, 'ñ'), (4, 'o')]
/// );
///
/// assert_eq!(word.slice_lossy(0..1000, ()), word);
/// assert_eq!(word.slice_lossy(10..10000, ()), "");
/// assert_eq!(word.slice_lossy(0..4, ()), "niñ");
/// assert_eq!(word.slice_lossy(0..3, ()), "niñ");
/// assert_eq!(word.slice_lossy(0..2, ()), "ni");
/// assert_eq!(word.slice_lossy(3..3, ()), "ñ");
/// assert_eq!(word.slice_lossy(3..4, ()), "ñ");
/// assert_eq!(word.slice_lossy(2..3, ()), "ñ");
///
/// assert_eq!(word.slice_lossy(0..1000, SliceBias::OUT), word);
/// assert_eq!(word.slice_lossy(10..10000, SliceBias::OUT), "");
/// assert_eq!(word.slice_lossy(0..4, SliceBias::OUT), "niñ");
/// assert_eq!(word.slice_lossy(0..3, SliceBias::OUT), "niñ");
/// assert_eq!(word.slice_lossy(0..2, SliceBias::OUT), "ni");
/// assert_eq!(word.slice_lossy(3..3, SliceBias::OUT), "ñ");
/// assert_eq!(word.slice_lossy(3..4, SliceBias::OUT), "ñ");
/// assert_eq!(word.slice_lossy(2..3, SliceBias::OUT), "ñ");
///
/// assert_eq!(word.slice_lossy(0..10000, SliceBias::IN), word);
/// assert_eq!(word.slice_lossy(10..10000, SliceBias::IN), "");
/// assert_eq!(word.slice_lossy(0..4, SliceBias::IN), "niñ");
/// assert_eq!(word.slice_lossy(0..3, SliceBias::IN), "ni");
/// assert_eq!(word.slice_lossy(0..2, SliceBias::IN), "ni");
/// assert_eq!(word.slice_lossy(3..3, SliceBias::IN), "");
/// assert_eq!(word.slice_lossy(3..4, SliceBias::IN), "");
/// assert_eq!(word.slice_lossy(2..3, SliceBias::IN), "");
///
/// assert_eq!(word.slice_lossy(0..1000, SliceBias::LEFT), word);
/// assert_eq!(word.slice_lossy(10..10000, SliceBias::LEFT), "");
/// assert_eq!(word.slice_lossy(0..4, SliceBias::LEFT), "niñ");
/// assert_eq!(word.slice_lossy(0..3, SliceBias::LEFT), "ni");
/// assert_eq!(word.slice_lossy(0..2, SliceBias::LEFT), "ni");
/// assert_eq!(word.slice_lossy(3..3, SliceBias::LEFT), "");
/// assert_eq!(word.slice_lossy(3..4, SliceBias::LEFT), "ñ");
/// assert_eq!(word.slice_lossy(2..3, SliceBias::LEFT), "");
///
/// assert_eq!(word.slice_lossy(0..1000, SliceBias::RIGHT), word);
/// assert_eq!(word.slice_lossy(10..10000, SliceBias::RIGHT), "");
/// assert_eq!(word.slice_lossy(0..4, SliceBias::RIGHT), "niñ");
/// assert_eq!(word.slice_lossy(0..3, SliceBias::RIGHT), "niñ");
/// assert_eq!(word.slice_lossy(0..2, SliceBias::RIGHT), "ni");
/// assert_eq!(word.slice_lossy(3..3, SliceBias::RIGHT), "");
/// assert_eq!(word.slice_lossy(3..4, SliceBias::RIGHT), "");
/// assert_eq!(word.slice_lossy(2..3, SliceBias::RIGHT), "ñ");
///
///
/// ```
///
/// [`SliceExt`]: ./trait.SliceExt.html
/// [`slice_lossy`]: ./trait.SliceExt.html#tymethod.slice_lossy
/// [`slice_lossy_mut`]: ./trait.SliceExt.html#tymethod.slice_lossy_mut
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SliceBias {
    /// bias of the start bound
    pub start: BiasDirection,
    /// bias of the end bound
    pub end: BiasDirection,
}

/// The direction a range bound is moved towards to make the bound a valid index.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BiasDirection {
    /// Means that the bound is biased to lower indices
    Left,
    /// Means that the bound is biased to higher indices
    Right,
}

impl SliceBias {
    /// Biased inwards, start bounds go right, end bounds go left.
    pub const IN: Self = Self {
        start: BiasDirection::Right,
        end: BiasDirection::Left,
    };
    /// Biased outwards, start bounds go left, end bounds go right.
    pub const OUT: Self = Self {
        start: BiasDirection::Left,
        end: BiasDirection::Right,
    };
    /// Biased leftwards, both bounds go left.
    pub const LEFT: Self = Self {
        start: BiasDirection::Left,
        end: BiasDirection::Left,
    };
    /// Biased rightwards. both bounds go right.
    pub const RIGHT: Self = Self {
        start: BiasDirection::Right,
        end: BiasDirection::Right,
    };
}

/// Returns a [`SliceBias::OUT`](#associatedconstant.OUT)
impl From<()> for SliceBias {
    fn from(_: ()) -> Self {
        Self::OUT
    }
}

impl From<BiasDirection> for SliceBias {
    fn from(dir: BiasDirection) -> Self {
        Self {
            start: dir,
            end: dir,
        }
    }
}

impl From<(BiasDirection,)> for SliceBias {
    fn from((dir,): (BiasDirection,)) -> Self {
        Self {
            start: dir,
            end: dir,
        }
    }
}

impl From<(BiasDirection, BiasDirection)> for SliceBias {
    fn from((start, end): (BiasDirection, BiasDirection)) -> Self {
        Self { start, end }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    use rand::{Rand, Rng};

    impl Rand for SliceBias {
        fn rand<R: Rng>(rng: &mut R) -> Self {
            fn rand_dir<R: Rng>(rng: &mut R) -> BiasDirection {
                match rng.gen_range::<u8>(0, 2) {
                    0 => BiasDirection::Left,
                    _=> BiasDirection::Right,
                }
            }

            SliceBias{
                start: rand_dir(rng),
                end: rand_dir(rng),
            }
        }
    }

    #[test]
    fn doc_comments() {
        use SliceExt;
        let word = "niño";

        assert_eq!(word.slice_lossy(0..1000, SliceBias::OUT), word);
        assert_eq!(word.slice_lossy(10..10000, SliceBias::OUT), "");
        assert_eq!(word.slice_lossy(0..4, SliceBias::OUT), "niñ");
        assert_eq!(word.slice_lossy(0..3, SliceBias::OUT), "niñ");
        assert_eq!(word.slice_lossy(0..2, SliceBias::OUT), "ni");

        assert_eq!(word.slice_lossy(0..10000, SliceBias::IN), word);
        assert_eq!(word.slice_lossy(10..10000, SliceBias::IN), "");
        assert_eq!(word.slice_lossy(0..4, SliceBias::IN), "niñ");
        assert_eq!(word.slice_lossy(0..3, SliceBias::IN), "ni");
        assert_eq!(word.slice_lossy(0..2, SliceBias::IN), "ni");
        assert_eq!(word.slice_lossy(3..3, SliceBias::IN), "");
        assert_eq!(word.slice_lossy(3..4, SliceBias::IN), "");
        assert_eq!(word.slice_lossy(2..3, SliceBias::IN), "");

        assert_eq!(word.slice_lossy(0..1000, SliceBias::LEFT), word);
        assert_eq!(word.slice_lossy(10..10000, SliceBias::LEFT), "");
        assert_eq!(word.slice_lossy(0..4, SliceBias::LEFT), "niñ");
        assert_eq!(word.slice_lossy(0..3, SliceBias::LEFT), "ni");
        assert_eq!(word.slice_lossy(0..2, SliceBias::LEFT), "ni");
        assert_eq!(word.slice_lossy(3..3, SliceBias::LEFT), "");
        assert_eq!(word.slice_lossy(3..4, SliceBias::LEFT), "ñ");
        assert_eq!(word.slice_lossy(2..3, SliceBias::LEFT), "");

        assert_eq!(word.slice_lossy(0..1000, SliceBias::RIGHT), word);
        assert_eq!(word.slice_lossy(10..10000, SliceBias::RIGHT), "");
        assert_eq!(word.slice_lossy(0..4, SliceBias::RIGHT), "niñ");
        assert_eq!(word.slice_lossy(0..3, SliceBias::RIGHT), "niñ");
        assert_eq!(word.slice_lossy(0..2, SliceBias::RIGHT), "ni");
        assert_eq!(word.slice_lossy(3..3, SliceBias::RIGHT), "");
        assert_eq!(word.slice_lossy(3..4, SliceBias::RIGHT), "");
        assert_eq!(word.slice_lossy(2..3, SliceBias::RIGHT), "ñ");
    }

}