servicepoint 0.16.0

A rust library for the CCCB Service Point Display.
Documentation
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
use crate::{
    containers::absolute_bounds_to_abs_range, DataRef, Grid, GridMut, Window,
    WindowMut,
};
use std::{
    fmt::Debug,
    ops::RangeBounds,
    slice::{Iter, IterMut},
};

/// A type that can be stored in a [`ValueGrid`], e.g. [char], [u8].
pub trait Value: Sized + Default + Copy + Clone + Debug {}
impl<T: Sized + Default + Copy + Clone + Debug> Value for T {}

/// A 2D grid of values.
///
/// The memory layout is the one the display expects in [`crate::Command`]s.
///
/// This structure can be used with any type that implements the [Value] trait.
/// You can also use the concrete type aliases provided in this crate, e.g. [`crate::CharGrid`] and [`crate::ByteGrid`].
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ValueGrid<T: Value> {
    width: usize,
    height: usize,
    data: Vec<T>,
}

/// Error type for methods that change a whole column or row at once
#[derive(thiserror::Error, Debug, PartialEq)]
pub enum SetValueSeriesError {
    #[error("The index {index} was out of bounds for size {size}")]
    /// The index {index} was out of bounds for size {size}
    OutOfBounds {
        /// the index where access was tried
        index: usize,
        /// the size in that direction
        size: usize,
    },
    #[error("The provided series was expected to have a length of {expected}, but was {actual}")]
    /// The provided series was expected to have a length of {expected}, but was {actual}
    InvalidLength {
        /// actual size of the provided series
        actual: usize,
        /// expected size
        expected: usize,
    },
}

impl<T: Value> ValueGrid<T> {
    /// Creates a new [`ValueGrid`] with the specified dimensions.
    ///
    /// # Arguments
    ///
    /// - width: size in x-direction
    /// - height: size in y-direction
    ///
    /// returns: [`ValueGrid`] initialized to default value.
    #[must_use]
    pub fn new(width: usize, height: usize) -> Self {
        assert!(width < isize::MAX as usize);
        assert!(height < isize::MAX as usize);
        Self {
            data: vec![Default::default(); width * height],
            width,
            height,
        }
    }

    /// Loads a [`ValueGrid`] with the specified dimensions from the provided data.
    ///
    /// returns: [`ValueGrid`] that contains a copy of the provided data,
    /// or None if the dimensions do not match the data size.
    #[must_use]
    pub fn load(width: usize, height: usize, data: &[T]) -> Option<Self> {
        assert!(width < isize::MAX as usize);
        assert!(height < isize::MAX as usize);
        if width * height != data.len() {
            return None;
        }
        Some(Self {
            data: Vec::from(data),
            width,
            height,
        })
    }

    /// Creates a [`ValueGrid`] with the specified width from the provided data,
    /// wrapping to as many rows as needed,
    /// without copying the vec.
    ///
    /// returns: [`ValueGrid`] that contains the provided data,
    /// or None if the data size is not divisible by the width.
    ///
    /// # Examples
    ///
    /// ```
    /// # use servicepoint::ValueGrid;
    /// let grid = ValueGrid::from_vec(2, vec![0, 1, 2, 3, 4, 5]).unwrap();
    /// ```
    #[must_use]
    pub fn from_vec(width: usize, data: Vec<T>) -> Option<Self> {
        let len = data.len();
        let height = len / width;
        assert!(width < isize::MAX as usize);
        assert!(height < isize::MAX as usize);
        if len % width != 0 {
            return None;
        }
        Some(Self {
            width,
            height,
            data,
        })
    }

    #[must_use]
    pub(crate) fn from_raw_parts_unchecked(
        width: usize,
        height: usize,
        data: Vec<T>,
    ) -> Self {
        debug_assert_eq!(data.len(), width * height);
        Self {
            width,
            height,
            data,
        }
    }

    /// Iterate over all cells in [`ValueGrid`].
    ///
    /// Order is equivalent to the following loop:
    /// ```
    /// # use servicepoint::{ByteGrid, Grid};
    /// # let grid = ByteGrid::new(2,2);
    /// for y in 0..grid.height() {
    ///     for x in 0..grid.width() {
    ///         grid.get(x, y);
    ///     }
    /// }
    /// ```
    pub fn iter(&self) -> impl Iterator<Item = &T> {
        self.data.iter()
    }

    /// Iterate over all rows in [`ValueGrid`] top to bottom.
    pub fn iter_rows(&self) -> impl Iterator<Item = Iter<'_, T>> {
        IterGridRows { grid: self, row: 0 }
    }

    /// Returns an iterator that allows modifying each value.
    ///
    /// The iterator yields all cells from top left to bottom right.
    pub fn iter_mut(&mut self) -> IterMut<'_, T> {
        self.data.iter_mut()
    }

    /// Get a mutable reference to the current value at the specified position.
    ///
    /// # Arguments
    ///
    /// - `x` and `y`: position of the cell
    ///
    /// # Panics
    ///
    /// When accessing `x` or `y` out of bounds.
    pub fn get_ref_mut(&mut self, x: usize, y: usize) -> &mut T {
        self.assert_in_bounds(x, y);
        &mut self.data[x + y * self.width]
    }

    /// Get a mutable reference to the current value at the specified position if position is in bounds.
    ///
    /// # Arguments
    ///
    /// - `x` and `y`: position of the cell
    ///
    /// returns: Reference to cell or None
    pub fn get_ref_mut_optional(
        &mut self,
        x: usize,
        y: usize,
    ) -> Option<&mut T> {
        if self.is_in_bounds(x, y) {
            Some(&mut self.data[x + y * self.width])
        } else {
            None
        }
    }

    /// Convert between `ValueGrid` types.
    ///
    /// See also [`Iterator::map`].
    ///
    /// # Examples
    ///
    /// Use logic written for u8s and then convert to [Brightness] values for sending in a [Command].
    /// ```
    /// # fn foo(grid: &mut ByteGrid) {}
    /// # use servicepoint::*;
    /// let mut grid: ByteGrid = ByteGrid::new(TILE_WIDTH, TILE_HEIGHT);
    /// foo(&mut grid);
    /// let grid: BrightnessGrid = grid.map(Brightness::saturating_from);
    /// let command = BrightnessGridCommand { origin: Origin::ZERO, grid };
    /// ```
    /// [Brightness]: [crate::Brightness]
    /// [Command]: [crate::Command]
    #[must_use]
    pub fn map<TConverted, F>(&self, f: F) -> ValueGrid<TConverted>
    where
        TConverted: Value,
        F: Fn(T) -> TConverted,
    {
        let data = self
            .data_ref()
            .iter()
            .map(|elem| f(*elem))
            .collect::<Vec<_>>();
        ValueGrid {
            width: self.width(),
            height: self.height(),
            data,
        }
    }

    /// Enumerates all values in the grid.
    pub fn enumerate(
        &self,
    ) -> impl Iterator<Item = (usize, usize, T)> + use<'_, T> {
        EnumerateGrid {
            grid: self,
            column: 0,
            row: 0,
        }
    }

    #[must_use]
    /// Creates a window into the grid.
    ///
    /// Returns None in case the window does not fit.
    pub fn window(
        &self,
        xs: impl RangeBounds<usize>,
        ys: impl RangeBounds<usize>,
    ) -> Option<Window<'_, T, Self>> {
        let xs = absolute_bounds_to_abs_range(xs, self.width)?;
        let ys = absolute_bounds_to_abs_range(ys, self.height)?;
        Window::new(self, xs, ys)
    }

    /// Creates a mutable window into the grid.
    ///
    /// Returns None in case the window does not fit.
    pub fn window_mut(
        &mut self,
        xs: impl RangeBounds<usize>,
        ys: impl RangeBounds<usize>,
    ) -> Option<WindowMut<'_, T, Self>> {
        let xs = absolute_bounds_to_abs_range(xs, self.width)?;
        let ys = absolute_bounds_to_abs_range(ys, self.height)?;
        WindowMut::new(self, xs, ys)
    }
}

/// Errors that can occur when loading a grid
#[derive(Debug, thiserror::Error, PartialEq)]
pub enum TryLoadValueGridError {
    #[error("The provided dimensions do not match with the data size")]
    /// The provided dimensions do not match with the data size
    InvalidDimensions,
}

impl<T: Value> Grid<T> for ValueGrid<T> {
    fn get_optional(&self, x: usize, y: usize) -> Option<T> {
        if self.is_in_bounds(x, y) {
            Some(self.data[x + y * self.width])
        } else {
            None
        }
    }

    fn width(&self) -> usize {
        self.width
    }

    fn height(&self) -> usize {
        self.height
    }
}

impl<T: Value> GridMut<T> for ValueGrid<T> {
    /// Sets the value of the cell at the specified position in the grid.
    ///
    /// # Arguments
    ///
    /// - `x` and `y`: position of the cell
    /// - `value`: the value to write to the cell
    ///
    /// # Panics
    ///
    /// When accessing `x` or `y` out of bounds.
    fn set_optional(&mut self, x: usize, y: usize, value: T) -> bool {
        if self.is_in_bounds(x, y) {
            self.data[x + y * self.width] = value;
            true
        } else {
            false
        }
    }

    fn fill(&mut self, value: T) {
        self.data.fill(value);
    }
}

impl<T: Value> DataRef<T> for ValueGrid<T> {
    /// Get the underlying byte rows mutable
    fn data_ref_mut(&mut self) -> &mut [T] {
        self.data.as_mut_slice()
    }

    /// Get the underlying byte rows read only
    fn data_ref(&self) -> &[T] {
        self.data.as_slice()
    }
}

impl<T: Value> From<ValueGrid<T>> for Vec<T> {
    /// Turn into the underlying [`Vec<u8>`] containing the rows of bytes.
    fn from(value: ValueGrid<T>) -> Self {
        value.data
    }
}

impl<T: Value> From<&ValueGrid<T>> for Vec<T> {
    /// Turn into the underlying [`Vec<u8>`] containing the rows of bytes.
    fn from(value: &ValueGrid<T>) -> Self {
        value.data.clone()
    }
}

impl<T: Value, G: Grid<T>> From<&G> for ValueGrid<T> {
    fn from(grid: &G) -> Self {
        let width = grid.width();
        let height = grid.height();
        let mut result = Self::new(width, height);
        result.deref_assign(grid);
        result
    }
}

/// An iterator iver the rows in a [`ValueGrid`]
#[must_use]
struct IterGridRows<'t, T: Value> {
    grid: &'t ValueGrid<T>,
    row: usize,
}

impl<'t, T: Value> Iterator for IterGridRows<'t, T> {
    type Item = Iter<'t, T>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.row >= self.grid.height {
            return None;
        }

        let start = self.row * self.grid.width;
        let end = start + self.grid.width;
        let result = self.grid.data[start..end].iter();
        self.row += 1;
        Some(result)
    }
}

struct EnumerateGrid<'t, T: Value> {
    grid: &'t ValueGrid<T>,
    row: usize,
    column: usize,
}

impl<T: Value> Iterator for EnumerateGrid<'_, T> {
    type Item = (usize, usize, T);

    fn next(&mut self) -> Option<Self::Item> {
        if self.row >= self.grid.height {
            return None;
        }

        let result =
            Some((self.column, self.row, self.grid.get(self.column, self.row)));
        self.column += 1;
        if self.column == self.grid.width {
            self.column = 0;
            self.row += 1;
        }
        result
    }
}

#[cfg(test)]
mod tests {
    use crate::{DataRef, Grid, GridMut, ValueGrid};

    #[test]
    fn fill() {
        let mut grid = ValueGrid::<usize>::new(2, 2);
        assert_eq!(grid.data, [0x00, 0x00, 0x00, 0x00]);

        grid.fill(42);
        assert_eq!(grid.data, [42; 4]);
    }

    #[test]
    fn get_set() {
        let mut grid = ValueGrid::new(2, 2);
        assert_eq!(grid.get(0, 0), 0);
        assert_eq!(grid.get(1, 1), 0);

        grid.set(0, 0, 42);
        grid.set(1, 0, 23);
        assert_eq!(grid.data, [42, 23, 0, 0]);

        assert_eq!(grid.get(0, 0), 42);
        assert_eq!(grid.get(1, 0), 23);
        assert_eq!(grid.get(1, 1), 0);
    }

    #[test]
    fn load() {
        let mut grid = ValueGrid::new(2, 3);
        for x in 0..grid.width {
            for y in 0..grid.height {
                grid.set(x, y, (x + y) as u8);
            }
        }

        assert_eq!(grid.data, [0, 1, 1, 2, 2, 3]);

        let data: Vec<u8> = grid.into();

        let grid = ValueGrid::load(2, 3, &data).unwrap();
        assert_eq!(grid.data, [0, 1, 1, 2, 2, 3]);
    }

    #[test]
    fn mut_data_ref() {
        let mut vec = ValueGrid::new(2, 2);

        let data_ref = vec.data_ref_mut();
        data_ref.copy_from_slice(&[1, 2, 3, 4]);

        assert_eq!(vec.data, [1, 2, 3, 4]);
        assert_eq!(vec.get(1, 0), 2);
    }

    #[test]
    fn iter() {
        let mut vec = ValueGrid::new(2, 2);
        vec.set(1, 1, 5);

        let mut iter = vec.iter();
        assert_eq!(*iter.next().unwrap(), 0);
        assert_eq!(*iter.next().unwrap(), 0);
        assert_eq!(*iter.next().unwrap(), 0);
        assert_eq!(*iter.next().unwrap(), 5);
    }

    #[test]
    fn iter_mut() {
        let mut vec = ValueGrid::new(2, 3);
        for (index, cell) in vec.iter_mut().enumerate() {
            *cell = index as u8;
        }

        assert_eq!(vec.data_ref(), [0, 1, 2, 3, 4, 5]);
    }

    #[test]
    fn iter_rows() {
        let vec = ValueGrid::load(2, 3, &[0, 1, 1, 2, 2, 3]).unwrap();
        for (y, row) in vec.iter_rows().enumerate() {
            for (x, val) in row.enumerate() {
                assert_eq!(*val, (x + y) as u8);
            }
        }
    }

    #[test]
    #[should_panic]
    fn out_of_bounds_x() {
        let mut vec = ValueGrid::load(2, 2, &[0, 1, 2, 3]).unwrap();
        vec.set(2, 1, 5);
    }

    #[test]
    #[should_panic]
    fn out_of_bounds_y() {
        let vec = ValueGrid::load(2, 2, &[0, 1, 2, 3]).unwrap();
        _ = vec.get(1, 2);
    }

    #[test]
    fn ref_mut() {
        let mut vec =
            ValueGrid::from_vec(3, vec![0, 1, 2, 3, 4, 5, 6, 7, 8]).unwrap();

        let top_left = vec.get_ref_mut(0, 0);
        *top_left += 5;
        let somewhere = vec.get_ref_mut(2, 1);
        *somewhere = 42;

        assert_eq!(None, vec.get_ref_mut_optional(3, 2));
        assert_eq!(None, vec.get_ref_mut_optional(2, 3));
        assert_eq!(Some(&mut 5), vec.get_ref_mut_optional(0, 0));
        assert_eq!(Some(&mut 42), vec.get_ref_mut_optional(2, 1));
        assert_eq!(Some(&mut 8), vec.get_ref_mut_optional(2, 2));
    }

    #[test]
    fn wrap() {
        let grid = ValueGrid::from_vec(2, vec![0, 1, 2, 3, 4, 5]).unwrap();
        assert_eq!(grid.height(), 3);

        let grid = ValueGrid::from_vec(4, vec![0, 1, 2, 3, 4, 5]);
        assert_eq!(grid, None);
    }

    #[test]
    fn load_invalid_size() {
        assert_eq!(ValueGrid::load(2, 2, &[1, 2, 3]), None);
    }

    #[test]
    fn enumerate() {
        let grid = ValueGrid::load(2, 3, &[0, 1, 2, 3, 4, 5]).unwrap();
        let values = grid.enumerate().collect::<Vec<_>>();
        assert_eq!(
            values,
            vec![
                (0, 0, 0),
                (1, 0, 1),
                (0, 1, 2),
                (1, 1, 3),
                (0, 2, 4),
                (1, 2, 5)
            ]
        );
    }
}