-fn parse_announcement(line: String) -> Option<(u64, u64, ChannelAnnouncement)> {
- let mut fields = line.split('|');
- let timestamp = fields.next().expect("Invalid ChannelAnnouncement line");
- let funding_sats = fields.next().expect("Invalid ChannelAnnouncement line");
- let ann_hex = fields.next().expect("Invalid ChannelAnnouncement line");
- let ann_bytes = Vec::<u8>::from_hex(ann_hex).ok().expect("Invalid ChannelAnnouncement hex");
- let announcement = Readable::read(&mut &ann_bytes[..]);
- debug_assert!(announcement.is_ok());
- Some((timestamp.parse().ok()?, funding_sats.parse().ok()?, announcement.ok()?))
+struct AnnouncementIter(BufReader<File>);
+impl Iterator for AnnouncementIter {
+ type Item = (u64, u64, ChannelAnnouncement);
+ fn next(&mut self) -> Option<(u64, u64, ChannelAnnouncement)> {
+ macro_rules! read { ($bytes: expr) => { {
+ if let Err(_) = self.0.read_exact(&mut $bytes[..]) {
+ return None;
+ }
+ } } }
+ let mut ts_bytes = [0u8; 8];
+ read!(ts_bytes);
+ let mut funding_bytes = [0u8; 8];
+ read!(funding_bytes);
+ let mut len_bytes = [0u8; 2];
+ read!(len_bytes);
+ let len = u16::from_le_bytes(len_bytes) as usize;
+ let mut announcement_bytes = vec![0; len];
+ read!(announcement_bytes);
+ let announcement = Readable::read(&mut &announcement_bytes[..]);
+ debug_assert!(announcement.is_ok());
+ Some((u64::from_le_bytes(ts_bytes), u64::from_le_bytes(funding_bytes), announcement.ok()?))
+ }