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
//! A module for [`Punctuated<T, P>`](Punctuated), a series of items to parse of type T separated
//! by punction of type `P`.

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::fmt;
use crate::io::{Read, Seek};
use crate::{BinRead, ReadOptions, BinResult};

/// A type for seperated data. Since parsing for this type is ambiguous, you must manually specify
/// a parser using the `parse_with` attribute.
///
/// ## Example
///
/// ```rust
/// # use binread::{*, io::*};
/// use binread::punctuated::Punctuated;
///
/// #[derive(BinRead)]
/// struct MyList {
///     #[br(parse_with = Punctuated::separated)]
///     #[br(count = 3)]
///     x: Punctuated<u16, u8>,
/// }
///
/// # let mut x = Cursor::new(b"\0\x03\0\0\x02\x01\0\x01");
/// # let y: MyList = x.read_be().unwrap();
/// # assert_eq!(*y.x, vec![3, 2, 1]);
/// # assert_eq!(y.x.seperators, vec![0, 1]);
/// ```
pub struct Punctuated<T: BinRead, P: BinRead> {
    data: Vec<T>,
    pub seperators: Vec<P>,
}

impl<C: Copy + 'static, T: BinRead<Args = C>, P: BinRead<Args = ()>> Punctuated<T, P> {
    /// A parser for values seperated by another value, with no trailing punctuation.
    ///
    /// Requires a specified count.
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use binread::{*, io::*};
    /// use binread::punctuated::Punctuated;
    ///
    /// #[derive(BinRead)]
    /// struct MyList {
    ///     #[br(parse_with = Punctuated::separated)]
    ///     #[br(count = 3)]
    ///     x: Punctuated<u16, u8>,
    /// }
    ///
    /// # let mut x = Cursor::new(b"\0\x03\0\0\x02\x01\0\x01");
    /// # let y: MyList = x.read_be().unwrap();
    /// # assert_eq!(*y.x, vec![3, 2, 1]);
    /// # assert_eq!(y.x.seperators, vec![0, 1]);
    /// ```
    pub fn separated<R: Read + Seek>(reader: &mut R, options: &ReadOptions, args: C) -> BinResult<Self> {
        let count = match options.count {
            Some(x) => x,
            None => panic!("Missing count for Punctuated"),
        };

        let mut data = Vec::with_capacity(count);
        let mut seperators = Vec::with_capacity(count.max(1) - 1);

        for i in 0..count {
            data.push(T::read_options(reader, &options, args)?);
            if i + 1 != count {
                seperators.push(P::read_options(reader, options, ())?);
            }
        }

        Ok(Self { data, seperators })
    }

    /// A parser for values seperated by another value, with trailing punctuation.
    ///
    /// Requires a specified count.
    pub fn separated_trailing<R: Read + Seek>(reader: &mut R, options: &ReadOptions, args: C) -> BinResult<Self> {
        let count = match options.count {
            Some(x) => x,
            None => panic!("Missing count for Punctuated"),
        };

        let mut data = Vec::with_capacity(count);
        let mut seperators = Vec::with_capacity(count);

        for _ in 0..count {
            data.push(T::read_options(reader, &options, args)?);
            seperators.push(P::read_options(reader, options, ())?);
        }

        Ok(Self { data, seperators })
    }
}

impl<T: BinRead + fmt::Debug, P: BinRead> fmt::Debug for Punctuated<T, P> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.data.fmt(f)
    }
}

impl<T: BinRead, P: BinRead> core::ops::Deref for Punctuated<T, P> {
    type Target = Vec<T>;

    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

impl<T: BinRead, P: BinRead> core::ops::DerefMut for Punctuated<T, P> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.data
    }
}

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

    use binread::{BinRead, BinReaderExt, io::Cursor};

    #[derive(BinRead, Clone, Copy, Debug)]
    #[br(magic = 1u8)]
    struct One;

    #[derive(BinRead, Clone, Copy, Debug)]
    #[br(magic = 2u8)]
    struct Two;

    #[derive(BinRead)]
    struct PunctuatedTest {
        count: u8,

        #[br(count = count)]
        #[br(parse_with = Punctuated::separated)]
        list: Punctuated<One, Two>,
    }

    #[derive(BinRead)]
    struct PunctuatedTestTrailing {
        count: u8,

        #[br(count = count)]
        #[br(parse_with = Punctuated::separated_trailing)]
        list: Punctuated<One, Two>,
    }

    #[derive(BinRead)]
    struct MissingCount {
        #[br(parse_with = Punctuated::separated)]
        _list: Punctuated<One, Two>,
    }

    #[derive(BinRead)]
    struct MissingCountTrailing {
        #[br(parse_with = Punctuated::separated_trailing)]
        _list: Punctuated<One, Two>,
    }

    const TEST_DATA: &[u8] = b"\x03\x01\x02\x01\x02\x01";
    const TEST_DATA_TRAILING: &[u8] = b"\x03\x01\x02\x01\x02\x01\x02";

    #[test]
    fn punctuated() {
        let mut x = Cursor::new(TEST_DATA);

        let y: PunctuatedTest = x.read_be().unwrap();

        assert_eq!(y.count, 3);
        assert_eq!(y.list.len(), 3);

        // This behavior may be reworked later
        assert_eq!(format!("{:?}", y.list), "[One, One, One]");
    }

    #[test]
    fn punctuated_trailing() {
        let mut x = Cursor::new(TEST_DATA_TRAILING);

        let mut y: PunctuatedTestTrailing = x.read_be().unwrap();

        assert_eq!(y.count, 3);
        assert_eq!(y.list.len(), 3);

        let y = &mut *y.list;
        y[0] = y[1];
    }

    #[test]
    #[should_panic]
    fn missing_count() {
        let mut x = Cursor::new(TEST_DATA);

        let _: MissingCount = x.read_be().unwrap();
    }

    #[test]
    #[should_panic]
    fn missing_count_trailing() {
        let mut x = Cursor::new(TEST_DATA);

        let _: MissingCountTrailing = x.read_be().unwrap();
    }
}