Files
damus/nostrdb/NdbBlocksIterator.swift
William Casarin 28a06af534 Switch over to use use blocks from nostrdb
This is still kind of broken until queries are switched over to nostrdb.
Will do this next

Signed-off-by: William Casarin <jb55@jb55.com>
2025-08-11 16:40:01 -07:00

60 lines
1.3 KiB
Swift

//
// NdbBlockIterator.swift
// damus
//
// Created by William Casarin on 2024-01-25.
//
import Foundation
struct BlocksIterator: IteratorProtocol {
typealias Element = NdbBlock
var done: Bool
var iter: ndb_block_iterator
var note: NdbNote
mutating func next() -> NdbBlock? {
guard iter.blocks != nil,
let ptr = ndb_blocks_iterate_next(&iter) else {
done = true
return nil
}
let block_ptr = ndb_block_ptr(ptr: ptr)
return NdbBlock(block_ptr)
}
init(note: NdbNote, blocks: NdbBlocks) {
let content = ndb_note_content(note.note.ptr)
self.iter = ndb_block_iterator(content: content, blocks: nil, block: ndb_block(), p: nil)
ndb_blocks_iterate_start(content, blocks.as_ptr(), &self.iter)
self.done = false
self.note = note
}
}
struct BlocksSequence: Sequence {
let blocks: NdbBlocks
let note: NdbNote
init(note: NdbNote, blocks: NdbBlocks) {
self.blocks = blocks
self.note = note
}
func makeIterator() -> BlocksIterator {
return .init(note: note, blocks: blocks)
}
func collect() -> [NdbBlock] {
var xs = [NdbBlock]()
for x in self {
xs.append(x)
}
return xs
}
}