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
//! ## Iterator types
//!
//! This module contains the types that can be used to iterate over the structs in the `assembly_fdb::mem` module.

use crate::handle::{Handle, RefHandle, SliceIterHandle};

use super::{
    c::{FDBBucketHeaderC, FDBFieldDataC, FDBRowHeaderC, FDBRowHeaderListEntryC, FDBTableHeaderC},
    get_field, get_row_header_list_entry, map_table_header, Bucket, Field, Row, Table,
};
use assembly_core::buffer::{Buffer, CastError};

/// Iterator created by [`Tables::iter`][`super::Tables::iter`]
#[derive(Clone)]
pub struct TableIter<'a> {
    inner: Handle<'a, std::slice::Iter<'a, FDBTableHeaderC>>,
}

impl<'a> TableIter<'a> {
    pub(super) fn new(inner: &RefHandle<'a, [FDBTableHeaderC]>) -> Self {
        Self {
            inner: inner.map_val(<[FDBTableHeaderC]>::iter),
        }
    }
}

impl<'a> Iterator for TableIter<'a> {
    type Item = Result<Table<'a>, CastError>;

    fn next(&mut self) -> Option<Self::Item> {
        self.inner
            .raw_mut()
            .next()
            .map(|raw| self.inner.wrap(raw))
            .map(map_table_header)
    }
}

fn bucket_rows(b: Bucket) -> RowHeaderIter {
    b.row_iter()
}

type FnBucketToRowIter<'a> = fn(Bucket<'a>) -> RowHeaderIter<'a>;

/// Iterator produced by [`Table::row_iter`]
#[derive(Clone)]
pub struct TableRowIter<'a> {
    inner: std::iter::FlatMap<BucketIter<'a>, RowHeaderIter<'a>, FnBucketToRowIter<'a>>,
}

impl<'a> TableRowIter<'a> {
    /// Create a new row iter from a bucket iter
    pub fn new(inner: BucketIter<'a>) -> Self {
        Self {
            inner: inner.flat_map(bucket_rows as FnBucketToRowIter<'a>),
        }
    }
}

impl<'a> Iterator for TableRowIter<'a> {
    type Item = Row<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next()
    }
}

/// Iterator produced by [`Table::bucket_iter`]
#[derive(Clone)]
pub struct BucketIter<'a> {
    inner: Handle<'a, std::slice::Iter<'a, FDBBucketHeaderC>>,
}

impl<'a> BucketIter<'a> {
    /// Create a new bucket iter
    pub fn new(inner: &RefHandle<'a, [FDBBucketHeaderC]>) -> Self {
        Self {
            inner: inner.map_val(<[FDBBucketHeaderC]>::iter),
        }
    }
}

impl<'a> Iterator for BucketIter<'a> {
    type Item = Bucket<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        self.inner
            .next()
            .map(|b| {
                b.map_extract()
                    .map_val(|b| b.row_header_list_head_addr)
                    .map(get_row_header_list_entry)
                    .transpose()
            })
            .map(|inner| Bucket { inner })
    }
}

/// Struct that implements [`Bucket::row_iter`].
#[derive(Clone)]
pub struct RowHeaderIter<'a> {
    next: Option<RefHandle<'a, FDBRowHeaderListEntryC>>,
}

impl<'a> RowHeaderIter<'a> {
    /// Create a new instance of this iterator
    pub fn new(next: Option<RefHandle<'a, FDBRowHeaderListEntryC>>) -> Self {
        Self { next }
    }
}

impl<'a> Iterator for RowHeaderIter<'a> {
    type Item = Row<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(next) = self.next {
            let entry = next.map_extract();
            self.next = entry
                .map_val(|e| e.row_header_list_next_addr)
                .map(get_row_header_list_entry)
                .transpose();

            let row_header = entry
                .map(|b, r| b.cast::<FDBRowHeaderC>(r.row_header_addr))
                .map_extract();
            let fields = row_header
                .map(|b, r| b.cast_slice::<FDBFieldDataC>(r.fields.base_offset, r.fields.count));

            Some(Row::new(fields))
        } else {
            None
        }
    }
}

/// An iterator over fields in a row
#[derive(Clone)]
pub struct FieldIter<'a> {
    inner: SliceIterHandle<'a, FDBFieldDataC>,
}

impl<'a> FieldIter<'a> {
    pub(crate) fn new(inner: RefHandle<'a, [FDBFieldDataC]>) -> Self {
        Self {
            inner: inner.map_val(<[FDBFieldDataC]>::iter),
        }
    }
}

impl<'a> Iterator for FieldIter<'a> {
    type Item = Field<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next().map(|r| r.map(get_field).into_raw())
    }
}