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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
#![cfg_attr(not(test), no_std)]
#![deny(missing_docs)]
#![doc = include_str!("../README.md")]

// Assumptions made in this crate:
//
// - flash erase size is quite big, aka, this is a paged flash
// - flash write size is quite small, so it writes words an not full pages
// - flash read size is 1, so the flash is byte addressable

use core::{fmt::Debug, ops::Range};
use embedded_storage::nor_flash::NorFlash;

#[cfg(test)]
mod mock_flash;

/// Get a storage item from the flash.
/// Only the last stored item of the given key is returned.
///
/// If no value with the key is found, None is returned.
pub fn fetch_item<I: StorageItem, S: NorFlash>(
    flash: &mut S,
    flash_range: Range<u32>,
    search_key: I::Key,
) -> Result<Option<I>, Error<I::Error, S::Error>> {
    assert_eq!(flash_range.start % S::ERASE_SIZE as u32, 0);
    assert_eq!(flash_range.end % S::ERASE_SIZE as u32, 0);

    assert!(S::ERASE_SIZE >= S::WRITE_SIZE * 3);
    assert_eq!(S::READ_SIZE, 1);

    // We need to find the page we were last using. This should be the only partial open page.
    let mut last_used_page =
        find_first_page::<I, S>(flash, flash_range.clone(), 0, PageState::PartialOpen)?;

    if last_used_page.is_none() {
        // In the event that all pages are still open or the last used page was just closed, we search for the first open page.
        // If the page one before that is closed, then that's the last used page.
        if let Some(first_open_page) =
            find_first_page::<I, S>(flash, flash_range.clone(), 0, PageState::Open)?
        {
            let previous_page = previous_page::<S>(flash_range.clone(), first_open_page);
            if get_page_state::<I, S>(flash, flash_range.clone(), previous_page)?
                == PageState::Closed
            {
                last_used_page = Some(previous_page);
            } else {
                // The page before the open page is not closed, so it must be open.
                // This means that all pages are open and that we don't have any items yet.
                return Ok(None);
            }
        } else {
            // There are no open pages, so everything must be closed.
            // Something is up and this should never happen.
            // To recover, we will just erase all the flash.
            flash
                .erase(flash_range.start, flash_range.end)
                .map_err(Error::Storage)?;
            return Ok(None);
        }
    }

    // We must now find the most recent storage item with the key that was asked for.
    // If we don't find it in the current page, then we check again in the previous page if that page is closed.

    let mut current_page_to_check = last_used_page.unwrap();
    let mut newest_found_item = None;

    loop {
        for found_item_result in
            read_page_items::<I, S>(flash, flash_range.clone(), current_page_to_check)?
        {
            let found_item = found_item_result?.0;
            if found_item.key() == search_key {
                newest_found_item = Some(found_item);
            }
        }

        // We've found the item! We can stop searching
        if newest_found_item.is_some() {
            break;
        }

        // We have not found the item. We've got to look in the previous page, but only if that page is closed and contains data.
        let previous_page = previous_page::<S>(flash_range.clone(), current_page_to_check);

        if get_page_state::<I, S>(flash, flash_range.clone(), previous_page)? != PageState::Closed {
            // We've looked through all the pages with data and couldn't find the item
            return Ok(None);
        }

        current_page_to_check = previous_page;
    }

    Ok(newest_found_item)
}

/// Store an item into flash memory.
/// It will overwrite the last value that has the same key.
///
/// Because const-generics are not fully done in Rust yet, you will have to provide the `PAGE_BUFFER_SIZE`, which has
/// to be the same value as the `ERASE_SIZE` of the flash.
pub fn store_item<I: StorageItem, S: NorFlash, const PAGE_BUFFER_SIZE: usize>(
    flash: &mut S,
    flash_range: Range<u32>,
    item: I,
) -> Result<(), Error<I::Error, S::Error>> {
    assert_eq!(flash_range.start % S::ERASE_SIZE as u32, 0);
    assert_eq!(flash_range.end % S::ERASE_SIZE as u32, 0);

    assert!(S::ERASE_SIZE >= S::WRITE_SIZE * 3);
    assert_eq!(S::READ_SIZE, 1);

    assert_eq!(PAGE_BUFFER_SIZE, S::ERASE_SIZE);

    return store_item_inner::<I, S, PAGE_BUFFER_SIZE>(flash, flash_range, item, 0);

    fn store_item_inner<I: StorageItem, S: NorFlash, const PAGE_BUFFER_SIZE: usize>(
        flash: &mut S,
        flash_range: Range<u32>,
        item: I,
        recursion_level: usize,
    ) -> Result<(), Error<I::Error, S::Error>> {
        // Check if we're in an infinite recursion which happens when
        if recursion_level == get_pages::<S>(flash_range.clone(), 0).count() {
            return Err(Error::FullStorage);
        }

        let mut next_page_to_use = None;

        // If there is a partial open page, we try to write in that first if there is enough space
        if let Some(partial_open_page) =
            find_first_page::<I, S>(flash, flash_range.clone(), 0, PageState::PartialOpen)?
        {
            // We've got to search where the free space is since the page starts with items present already

            let page_data_start_address =
                calculate_page_address::<S>(flash_range.clone(), partial_open_page)
                    + S::WRITE_SIZE as u32;
            let page_data_end_address =
                calculate_page_end_address::<S>(flash_range.clone(), partial_open_page)
                    - S::WRITE_SIZE as u32;

            let mut last_start_address = page_data_start_address;

            for found_item_result in
                read_page_items::<I, S>(flash, flash_range.clone(), partial_open_page)?
            {
                let (_, item_address, item_size) = found_item_result?;
                last_start_address = item_address + item_size as u32;
            }

            let available_bytes_in_page = (page_data_end_address - last_start_address) as usize;

            let mut buffer = [0xFF; MAX_STORAGE_ITEM_SIZE];
            match item
                .serialize_into(&mut buffer[..MAX_STORAGE_ITEM_SIZE.min(available_bytes_in_page)])
            {
                Ok(mut used_bytes) => {
                    // The item fits, so let's write it to flash
                    // We must round up the used size because we can only write with full words
                    if used_bytes % S::WRITE_SIZE > 0 {
                        used_bytes += S::WRITE_SIZE - (used_bytes % S::WRITE_SIZE);
                    }

                    flash
                        .write(last_start_address, &buffer[..used_bytes])
                        .map_err(Error::Storage)?;
                    return Ok(());
                }
                Err(e) if e.is_buffer_too_small() => {
                    // The item doesn't fit here, so we need to close this page and move to the next
                    close_page::<I, S>(flash, flash_range.clone(), partial_open_page)?;
                    next_page_to_use = Some(next_page::<S>(flash_range.clone(), partial_open_page));
                }
                Err(e) => {
                    return Err(Error::Item(e));
                }
            }
        }

        // If we get here, there was no partial page found or the partial page has now been closed because the item didn't fit.
        // If there was a partial page, then we need to look at the next page. If it's open we just use it and if it's closed we must erase it.
        // If there was no partial page, we just use the first open page.

        match next_page_to_use {
            Some(next_page_to_use) => {
                let next_page_state =
                    get_page_state::<I, S>(flash, flash_range.clone(), next_page_to_use)?;

                if next_page_state == PageState::Open {
                    partial_close_page::<I, S>(flash, flash_range.clone(), next_page_to_use)?;
                } else {
                    let mut page_cache_buffer = [0; PAGE_BUFFER_SIZE];

                    // So the next page isn't open. We must clear it.
                    // But in that process we can lose information. A value could only be stored once in the page we're now gonna clear.
                    // So we must read the full page into ram, clear the page and then add the now missing value back.
                    flash
                        .read(
                            calculate_page_address::<S>(flash_range.clone(), next_page_to_use),
                            &mut page_cache_buffer,
                        )
                        .map_err(Error::Storage)?;
                    flash
                        .erase(
                            calculate_page_address::<S>(flash_range.clone(), next_page_to_use),
                            calculate_page_end_address::<S>(flash_range.clone(), next_page_to_use),
                        )
                        .map_err(Error::Storage)?;

                    partial_close_page::<I, S>(flash, flash_range.clone(), next_page_to_use)?;

                    // Now add back any messages we now miss
                    // Because the page is already cleared, we can just search for the message keys through the normal API
                    // And also because partial page writes go before this, we can just write the items through the normal API

                    let mut old_data_slice =
                        &page_cache_buffer[S::WRITE_SIZE..S::ERASE_SIZE - S::WRITE_SIZE];

                    while !old_data_slice.iter().all(|b| *b == 0xFF) {
                        let (item, mut used_bytes) =
                            I::deserialize_from(old_data_slice).map_err(Error::Item)?;

                        if fetch_item::<I, S>(flash, flash_range.clone(), item.key())?.is_none() {
                            store_item_inner::<I, S, PAGE_BUFFER_SIZE>(
                                flash,
                                flash_range.clone(),
                                item,
                                recursion_level, // We don't need to increase the recursion level here because the old item will always fit on the new page
                            )?;
                        }

                        // Round up to the nearest word
                        if used_bytes % S::WRITE_SIZE > 0 {
                            used_bytes += S::WRITE_SIZE - (used_bytes % S::WRITE_SIZE);
                        }

                        old_data_slice = &old_data_slice[used_bytes..];
                    }
                }
            }
            None => {
                // There's no partial open page, so we just gotta turn the first open page into a partial open one
                let first_open_page = match find_first_page::<I, S>(
                    flash,
                    flash_range.clone(),
                    0,
                    PageState::Open,
                )? {
                    Some(first_open_page) => first_open_page,
                    None => {
                        // Uh oh, no open pages.
                        // Something has gone wrong.
                        // We should never get here.
                        // Let's recover
                        flash
                            .erase(flash_range.start, flash_range.end)
                            .map_err(Error::Storage)?;

                        0
                    }
                };

                partial_close_page::<I, S>(flash, flash_range.clone(), first_open_page)?;
            }
        }

        // If we get here, we just freshly partially closed a new page, so this should succeed
        store_item_inner::<I, S, PAGE_BUFFER_SIZE>(flash, flash_range, item, recursion_level + 1)
    }
}

fn find_first_page<I: StorageItem, S: NorFlash>(
    flash: &mut S,
    flash_range: Range<u32>,
    starting_page_index: usize,
    page_state: PageState,
) -> Result<Option<usize>, Error<I::Error, S::Error>> {
    for page_index in get_pages::<S>(flash_range.clone(), starting_page_index) {
        if page_state == get_page_state::<I, S>(flash, flash_range.clone(), page_index)? {
            return Ok(Some(page_index));
        }
    }

    Ok(None)
}

/// Get all pages in the flash range from the given start to end (that might wrap back to 0)
fn get_pages<S: NorFlash>(
    flash_range: Range<u32>,
    starting_page_index: usize,
) -> impl Iterator<Item = usize> {
    let page_count = flash_range.len() / S::ERASE_SIZE;
    flash_range
        .step_by(S::ERASE_SIZE)
        .enumerate()
        .map(|(index, _)| index)
        .cycle()
        .skip(starting_page_index)
        .take(page_count)
}

fn next_page<S: NorFlash>(flash_range: Range<u32>, page_index: usize) -> usize {
    let page_count = flash_range.len() / S::ERASE_SIZE;
    (page_index + 1) % page_count
}

fn previous_page<S: NorFlash>(flash_range: Range<u32>, page_index: usize) -> usize {
    let page_count = flash_range.len() / S::ERASE_SIZE;

    match page_index.checked_sub(1) {
        Some(new_page_index) => new_page_index,
        None => page_count - 1,
    }
}

fn calculate_page_address<S: NorFlash>(flash_range: Range<u32>, page_index: usize) -> u32 {
    flash_range.start + (S::ERASE_SIZE * page_index) as u32
}
fn calculate_page_end_address<S: NorFlash>(flash_range: Range<u32>, page_index: usize) -> u32 {
    flash_range.start + (S::ERASE_SIZE * (page_index + 1)) as u32
}

fn get_page_state<I: StorageItem, S: NorFlash>(
    flash: &mut S,
    flash_range: Range<u32>,
    page_index: usize,
) -> Result<PageState, Error<I::Error, S::Error>> {
    let page_address = calculate_page_address::<S>(flash_range, page_index);

    let mut buffer = [0; 16];
    flash
        .read(page_address, &mut buffer[..S::READ_SIZE])
        .map_err(Error::Storage)?;
    let start_marker = buffer[0];

    if start_marker != MARKER {
        // The page start is not marked, so it is unused
        return Ok(PageState::Open);
    }

    // The page start is marked, so it can be full or partially full
    // We need to look at the end marker to know

    flash
        .read(
            page_address + (S::ERASE_SIZE - S::READ_SIZE) as u32,
            &mut buffer[..S::READ_SIZE],
        )
        .map_err(Error::Storage)?;
    let end_marker = buffer[S::READ_SIZE - 1];

    if end_marker != MARKER {
        // The page end is not marked, so it is only partially filled and thus open
        return Ok(PageState::PartialOpen);
    }

    // Both start and end are marked, so this page is closed
    Ok(PageState::Closed)
}

fn read_page_items<I: StorageItem, S: NorFlash>(
    flash: &mut S,
    flash_range: Range<u32>,
    page_index: usize,
) -> Result<
    impl Iterator<Item = Result<(I, u32, usize), Error<I::Error, S::Error>>> + '_,
    Error<I::Error, S::Error>,
> {
    let mut read_buffer = [0xFF; MAX_STORAGE_ITEM_SIZE];
    let page_data_start_address =
        calculate_page_address::<S>(flash_range.clone(), page_index) + S::WRITE_SIZE as u32;
    let page_data_end_address =
        calculate_page_end_address::<S>(flash_range.clone(), page_index) - S::WRITE_SIZE as u32;
    let mut read_buffer_start_index_into_page = 0;

    flash
        .read(
            page_data_start_address,
            &mut read_buffer[0..MAX_STORAGE_ITEM_SIZE
                .min((page_data_end_address - page_data_start_address) as usize)],
        )
        .map_err(Error::Storage)?;

    Ok(core::iter::from_fn(move || {
        // Now we deserialize the items from the buffer one by one
        // Every time we proceed, we remove the bytes we used and fill the buffer back up

        if read_buffer.iter().all(|b| *b == 0xFF) {
            // The entire buffer is in the erased state, so we know that the rest is empty
            return None;
        }

        match I::deserialize_from(&read_buffer) {
            Ok((item, mut used_bytes)) => {
                // We can only write in whole words, so we round up the used bytes so the math works
                if used_bytes % S::WRITE_SIZE > 0 {
                    used_bytes += S::WRITE_SIZE - (used_bytes % S::WRITE_SIZE);
                }

                let return_item = Ok((
                    item,
                    page_data_start_address + read_buffer_start_index_into_page as u32,
                    used_bytes,
                ));

                read_buffer_start_index_into_page += used_bytes;
                read_buffer.copy_within(used_bytes.., 0);
                // We need to replenish the used_bytes at the end of the buffer
                let replenish_slice = &mut read_buffer[MAX_STORAGE_ITEM_SIZE - used_bytes..];

                let replenish_start_address = page_data_start_address
                    + read_buffer_start_index_into_page as u32
                    + MAX_STORAGE_ITEM_SIZE as u32
                    - used_bytes as u32;

                let unread_bytes_left_in_page =
                    page_data_end_address.saturating_sub(replenish_start_address);

                let (read_slice, fill_slice) = replenish_slice
                    .split_at_mut((unread_bytes_left_in_page as usize).min(replenish_slice.len()));

                if !read_slice.is_empty() {
                    if let Err(e) = flash
                        .read(replenish_start_address, read_slice)
                        .map_err(Error::Storage)
                    {
                        return Some(Err(e));
                    }
                }
                fill_slice.fill(0xFF);

                Some(return_item)
            }
            Err(e) => {
                // We can't deserialize an item, so something must have gone wrong.
                // We shouldn't ever get here.
                // To recover, we erase the flash.
                if let Err(e) = flash
                    .erase(flash_range.start, flash_range.end)
                    .map_err(Error::Storage)
                {
                    return Some(Err(e));
                }
                Some(Err(Error::Item(e)))
            }
        }
    }))
}

/// Fully closes a page by writing both the start and end marker
fn close_page<I: StorageItem, S: NorFlash>(
    flash: &mut S,
    flash_range: Range<u32>,
    page_index: usize,
) -> Result<(), Error<I::Error, S::Error>> {
    partial_close_page::<I, S>(flash, flash_range.clone(), page_index)?;

    let current_state = get_page_state::<I, S>(flash, flash_range.clone(), page_index)?;

    if current_state != PageState::PartialOpen {
        return Ok(());
    }

    let buffer = [MARKER; 16];
    // Close the end marker
    flash
        .write(
            calculate_page_end_address::<S>(flash_range, page_index) - S::WRITE_SIZE as u32,
            &buffer[..S::WRITE_SIZE],
        )
        .map_err(Error::Storage)?;

    Ok(())
}

/// Partially close a page by writing the start marker
fn partial_close_page<I: StorageItem, S: NorFlash>(
    flash: &mut S,
    flash_range: Range<u32>,
    page_index: usize,
) -> Result<(), Error<I::Error, S::Error>> {
    let current_state = get_page_state::<I, S>(flash, flash_range.clone(), page_index)?;

    if current_state != PageState::Open {
        return Ok(());
    }

    let buffer = [MARKER; 16];
    // Close the start marker
    flash
        .write(
            calculate_page_address::<S>(flash_range, page_index),
            &buffer[..S::WRITE_SIZE],
        )
        .map_err(Error::Storage)?;

    Ok(())
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PageState {
    Closed,
    PartialOpen,
    Open,
}

const MARKER: u8 = 0;

/// A way of serializing and deserializing items in the storage.
///
/// A serialized byte pattern of all `0xFF` is invalid and must never be the result of the `serialize_into` function
/// and `deserialize_from` must always return an error for it.
///
/// The given buffer to serialize in and deserialize from is never bigger than [MAX_STORAGE_ITEM_SIZE] bytes, so make sure the item is
/// smaller than that.
pub trait StorageItem {
    /// The key type of the key-value pair
    type Key: Eq;
    /// The error type for serialization and deserialization
    type Error: StorageItemError;

    /// Serialize the key-value item into the given buffer.
    /// Returns the number of bytes the buffer was filled with or an error.
    ///
    /// The serialized bytes must not all be `0xFF`. One way to prevent this is to serialize an extra 0 byte at the end if that is the case.
    fn serialize_into(&self, buffer: &mut [u8]) -> Result<usize, Self::Error>;
    /// Deserialize the key-value item from the given buffer.
    /// The buffer is likely bigger than the size of the item.
    fn deserialize_from(buffer: &[u8]) -> Result<(Self, usize), Self::Error>
    where
        Self: Sized;

    /// The key of the key-value item. It is used by the storage to know what the key of this item is.
    fn key(&self) -> Self::Key;
}

/// The maximum size in bytes that a storage item can be
pub const MAX_STORAGE_ITEM_SIZE: usize = 512;

/// A trait that the storage item error needs to implement
pub trait StorageItemError: Debug {
    /// Returns true if the error indicates that the buffer is too small to contain the storage item
    fn is_buffer_too_small(&self) -> bool;
}

/// The main error type
#[non_exhaustive]
#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error<I, S> {
    /// A storage item error
    Item(I),
    /// An error in the storage (flash)
    Storage(S),
    /// The item cannot be stored anymore because the storage is full.
    /// If you get this error some data may be lost.
    FullStorage,
}

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

    type MockFlash = mock_flash::MockFlashBase<4, 4, 64>;
    type MockFlashBig = mock_flash::MockFlashBase<4, 4, 256>;
    type MockFlashTiny = mock_flash::MockFlashBase<2, 1, 32>;

    #[derive(Debug, PartialEq, Eq)]
    struct MockStorageItem {
        key: u8,
        value: u8,
    }

    #[derive(Debug, PartialEq, Eq)]
    enum MockStorageItemError {
        BufferTooSmall,
        InvalidKey,
    }

    impl StorageItemError for MockStorageItemError {
        fn is_buffer_too_small(&self) -> bool {
            matches!(self, MockStorageItemError::BufferTooSmall)
        }
    }

    impl StorageItem for MockStorageItem {
        type Key = u8;

        type Error = MockStorageItemError;

        fn serialize_into(&self, buffer: &mut [u8]) -> Result<usize, Self::Error> {
            if buffer.len() < 2 {
                return Err(MockStorageItemError::BufferTooSmall);
            }

            // The serialized value must not be all 0xFF
            if self.key == 0xFF {
                return Err(MockStorageItemError::InvalidKey);
            }

            buffer[0] = self.key;
            buffer[1] = self.value;

            Ok(2)
        }

        fn deserialize_from(buffer: &[u8]) -> Result<(Self, usize), Self::Error>
        where
            Self: Sized,
        {
            if buffer.len() < 2 {
                return Err(MockStorageItemError::BufferTooSmall);
            }

            if buffer[0] == 0xFF {
                return Err(MockStorageItemError::InvalidKey);
            }

            Ok((
                Self {
                    key: buffer[0],
                    value: buffer[1],
                },
                2,
            ))
        }

        fn key(&self) -> Self::Key {
            self.key
        }
    }

    #[test]
    fn test_find_pages() {
        // Page setup:
        // 0: closed
        // 1: closed
        // 2: partial-open
        // 3: open

        let mut flash = MockFlash::new();
        // Page 0 markers
        flash.write(0x000, &[MARKER, 0, 0, 0]).unwrap();
        flash.write(0x100 - 4, &[0, 0, 0, MARKER]).unwrap();
        // Page 1 markers
        flash.write(0x100, &[MARKER, 0, 0, 0]).unwrap();
        flash.write(0x200 - 4, &[0, 0, 0, MARKER]).unwrap();
        // Page 2 markers
        flash.write(0x200, &[MARKER, 0, 0, 0]).unwrap();

        assert_eq!(
            find_first_page::<MockStorageItem, _>(&mut flash, 0x000..0x400, 0, PageState::Open)
                .unwrap(),
            Some(3)
        );
        assert_eq!(
            find_first_page::<MockStorageItem, _>(
                &mut flash,
                0x000..0x400,
                0,
                PageState::PartialOpen
            )
            .unwrap(),
            Some(2)
        );
        assert_eq!(
            find_first_page::<MockStorageItem, _>(
                &mut flash,
                0x000..0x400,
                1,
                PageState::PartialOpen
            )
            .unwrap(),
            Some(2)
        );
        assert_eq!(
            find_first_page::<MockStorageItem, _>(
                &mut flash,
                0x000..0x400,
                2,
                PageState::PartialOpen
            )
            .unwrap(),
            Some(2)
        );
        assert_eq!(
            find_first_page::<MockStorageItem, _>(&mut flash, 0x000..0x400, 3, PageState::Open)
                .unwrap(),
            Some(3)
        );
        assert_eq!(
            find_first_page::<MockStorageItem, _>(
                &mut flash,
                0x000..0x200,
                0,
                PageState::PartialOpen
            )
            .unwrap(),
            None
        );

        assert_eq!(
            find_first_page::<MockStorageItem, _>(&mut flash, 0x000..0x400, 0, PageState::Closed)
                .unwrap(),
            Some(0)
        );
        assert_eq!(
            find_first_page::<MockStorageItem, _>(&mut flash, 0x000..0x400, 1, PageState::Closed)
                .unwrap(),
            Some(1)
        );
        assert_eq!(
            find_first_page::<MockStorageItem, _>(&mut flash, 0x000..0x400, 2, PageState::Closed)
                .unwrap(),
            Some(0)
        );
        assert_eq!(
            find_first_page::<MockStorageItem, _>(&mut flash, 0x000..0x400, 3, PageState::Closed)
                .unwrap(),
            Some(0)
        );
        assert_eq!(
            find_first_page::<MockStorageItem, _>(&mut flash, 0x200..0x400, 0, PageState::Closed)
                .unwrap(),
            None
        );
    }

    #[test]
    fn store_and_fetch() {
        let mut flash = MockFlashBig::new();
        let flash_range = 0x000..0x1000;

        let item = fetch_item::<MockStorageItem, _>(&mut flash, flash_range.clone(), 0).unwrap();
        assert_eq!(item, None);

        let item = fetch_item::<MockStorageItem, _>(&mut flash, flash_range.clone(), 60).unwrap();
        assert_eq!(item, None);

        let item = fetch_item::<MockStorageItem, _>(&mut flash, flash_range.clone(), 0xFF).unwrap();
        assert_eq!(item, None);

        store_item::<_, _, 1024>(
            &mut flash,
            flash_range.clone(),
            MockStorageItem { key: 0, value: 5 },
        )
        .unwrap();
        store_item::<_, _, 1024>(
            &mut flash,
            flash_range.clone(),
            MockStorageItem { key: 0, value: 6 },
        )
        .unwrap();

        let item = fetch_item::<MockStorageItem, _>(&mut flash, flash_range.clone(), 0)
            .unwrap()
            .unwrap();
        assert_eq!(item.key, 0);
        assert_eq!(item.value, 6);

        store_item::<_, _, 1024>(
            &mut flash,
            flash_range.clone(),
            MockStorageItem { key: 1, value: 2 },
        )
        .unwrap();

        let item = fetch_item::<MockStorageItem, _>(&mut flash, flash_range.clone(), 0)
            .unwrap()
            .unwrap();
        assert_eq!(item.key, 0);
        assert_eq!(item.value, 6);

        let item = fetch_item::<MockStorageItem, _>(&mut flash, flash_range.clone(), 1)
            .unwrap()
            .unwrap();
        assert_eq!(item.key, 1);
        assert_eq!(item.value, 2);

        for index in 0..4000 {
            store_item::<_, _, 1024>(
                &mut flash,
                flash_range.clone(),
                MockStorageItem {
                    key: (index % 10) as u8,
                    value: (index % 10) as u8 * 2,
                },
            )
            .unwrap();
        }

        for i in 0..10 {
            let item = fetch_item::<MockStorageItem, _>(&mut flash, flash_range.clone(), i)
                .unwrap()
                .unwrap();
            assert_eq!(item.key, i);
            assert_eq!(item.value, i * 2);
        }

        for _ in 0..4000 {
            store_item::<_, _, 1024>(
                &mut flash,
                flash_range.clone(),
                MockStorageItem { key: 11, value: 0 },
            )
            .unwrap();
        }

        for i in 0..10 {
            let item = fetch_item::<MockStorageItem, _>(&mut flash, flash_range.clone(), i)
                .unwrap()
                .unwrap();
            assert_eq!(item.key, i);
            assert_eq!(item.value, i * 2);
        }
    }

    #[test]
    fn store_too_many_items() {
        let mut tiny_flash = MockFlashTiny::new();

        for i in 0..30 {
            store_item::<_, _, 32>(
                &mut tiny_flash,
                0x00..0x40,
                MockStorageItem {
                    key: i as u8,
                    value: i as u8,
                },
            )
            .unwrap();
        }

        assert_eq!(
            store_item::<_, _, 32>(
                &mut tiny_flash,
                0x00..0x40,
                MockStorageItem {
                    key: 31 as u8,
                    value: 31 as u8,
                },
            ),
            Err(Error::FullStorage)
        );

        for i in 0..30 {
            fetch_item::<MockStorageItem, _>(&mut tiny_flash, 0x00..0x40, i as u8)
                .unwrap()
                .unwrap();
        }
    }
}