feat: import interface

This commit is contained in:
abelith committed 2026-08-21 15:27:37 +05:00
1 parent f97e9b0aa9
commit 69af7eaee3
3 files changed
+66

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
package importer
import (
"context"
"fmt"
"github.com/abelith/spellbook/internal/models"
"io"
"sync"
)
type Importer interface {
Import(ctx context.Context, r io.Reader) (<-chan models.Spell, <-chan error, error)
}
var (
importers = make(map[string]Importer)
mu sync.Mutex
)
func Register(name string, imp Importer) {
mu.Lock()
defer mu.Unlock()
if imp == nil {
panic("importer: Importer cannot be nil")
}
if _, ok := importers[name]; ok {
panic("importer: Register called twice for " + name)
}
importers[name] = imp
}
func Import(ctx context.Context, name string, r io.Reader) (<-chan models.Spell, <-chan error, error) {
mu.Lock()
imp, ok := importers[name]
mu.Unlock()
if !ok {
return nil, nil, fmt.Errorf("importer %s not found", name)
}
return imp.Import(ctx, r)
}