// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.

package iofilter

import (
	"io"
	"log/slog"
)

type ReadFilter func([]byte, int, error) (int, error)

type Reader struct {
	input io.Reader
	filter ReadFilter
}

func NewReader(reader io.Reader, filter ReadFilter) *Reader {
	return &Reader{
		input: reader,
		filter: filter,
	}
}

func (r *Reader) Read(p []byte) (n int, err error) {
	n, err = r.input.Read(p)
	if r.filter != nil {
		return r.filter(p, n, err)
	} else {
		slog.Warn("iofilter.Reader.Read() - nil filter") 
	}
	return n, err
}
