func ReadFile(filename string, w io.Writer, filter Filter) error {
	input, err := os.ReadFile(filename)
	if err != nil {
		return fmt.Errorf("failed to open file: %s", err)
	}
	o := NewEditOperator(filter)
	output, err := o.Apply(input, filename)
	if err != nil {
		return err
	}
	if _, err := w.Write(output); err != nil {
		return fmt.Errorf("failed to write output: %s", err)
	}
	return nil
}
ReadFile is a helper method which builds an EditorOperator from a given
filter and applies it to a single file.
The outputs are written to stream.
package editor
import (
	"bytes"
	"fmt"
	"io"
	"os"
)
type EditOperator struct {
	source    Source
	filter    Filter
	formatter Formatter
}
var _ Operator = (*EditOperator)(nil)
func NewEditOperator(filter Filter) Operator {
	return &EditOperator{
		source:    NewParserSource(),
		filter:    filter,
		formatter: NewDefaultFormatter(),
	}
}
func (o *EditOperator) Apply(input []byte, filename string) ([]byte, error) {
	inFile, err := o.source.Source(input, filename)
	if err != nil {
		return nil, err
	}
	tmpFile, err := o.filter.Filter(inFile)
	if err != nil {
		return nil, err
	}
	output := tmpFile.BuildTokens(nil).Bytes()
	if bytes.Equal(input, output) {
		return output, nil
	}
	return o.formatter.Format(tmpFile)
}
func EditStream(r io.Reader, w io.Writer, filename string, filter Filter) error {
	input, err := io.ReadAll(r)
	if err != nil {
		return fmt.Errorf("failed to read input: %s", err)
	}
	o := NewEditOperator(filter)
	output, err := o.Apply(input, filename)
	if err != nil {
		return err
	}
	if _, err := w.Write(output); err != nil {
		return fmt.Errorf("failed to write output: %s", err)
	}
	return nil
}
func UpdateFile(filename string, filter Filter) error {
	input, err := os.ReadFile(filename)
	if err != nil {
		return fmt.Errorf("failed to open file: %s", err)
	}
	o := NewEditOperator(filter)
	output, err := o.Apply(input, filename)
	if err != nil {
		return err
	}
	if bytes.Equal(input, output) {
		return nil
	}
	if err = os.WriteFile(filename, output, os.ModePerm); err != nil {
		return fmt.Errorf("failed to write file: %s", err)
	}
	return nil
}