GitRoot

Craft your forge, Build your project, Grow your community freely
 1// SPDX-FileCopyrightText: 2026 Romain Maneschi <romain@gitroot.dev>
 2//
 3// SPDX-License-Identifier: MIT
 4
 5package gitroot
 6
 7import (
 8	"iter"
 9
10	"gitroot.dev/libs/golang/plugin/model"
11)
12
13type Iterator[T any] struct {
14	name         string
15	deserializer func(data string) T
16}
17
18func NewIterator[T any](name string, deserializer func(data string) T) Iterator[T] {
19	return Iterator[T]{
20		name:         name,
21		deserializer: deserializer,
22	}
23}
24
25func (i Iterator[T]) Next() (T, bool) {
26	ptr, size := stringToPtr(i.name)
27	ptrSize := _iteratorNext(ptr, size)
28	ptrItem := uint32(ptrSize >> 32)
29	sizeItem := uint32(ptrSize)
30	item := model.IteratorItemFromString(ptrToString(ptrItem, sizeItem))
31	var t T
32	if item.Exist {
33		data := i.deserializer(item.Item)
34		return data, true
35	}
36	return t, false
37}
38
39func (i Iterator[T]) ToSeq() iter.Seq[T] {
40	return func(yield func(T) bool) {
41		for {
42			val, ok := i.Next()
43			if !ok {
44				return
45			}
46			if !yield(val) {
47				return
48			}
49		}
50	}
51}