2024-04-19 12:57:41 +03:00
|
|
|
package format
|
|
|
|
|
2024-04-25 11:38:41 +03:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"slices"
|
2024-05-01 21:03:26 +03:00
|
|
|
|
|
|
|
"git.numtide.com/numtide/treefmt/walk"
|
2024-04-25 11:38:41 +03:00
|
|
|
)
|
2024-04-19 12:57:41 +03:00
|
|
|
|
|
|
|
type Pipeline struct {
|
|
|
|
sequence []*Formatter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pipeline) Add(f *Formatter) {
|
|
|
|
p.sequence = append(p.sequence, f)
|
2024-04-25 11:38:41 +03:00
|
|
|
// sort by priority in ascending order
|
|
|
|
slices.SortFunc(p.sequence, func(a, b *Formatter) int {
|
|
|
|
return a.config.Priority - b.config.Priority
|
|
|
|
})
|
2024-04-19 12:57:41 +03:00
|
|
|
}
|
|
|
|
|
2024-05-01 21:03:26 +03:00
|
|
|
func (p *Pipeline) Wants(path *walk.File) bool {
|
2024-04-19 12:57:41 +03:00
|
|
|
var match bool
|
|
|
|
for _, f := range p.sequence {
|
|
|
|
match = f.Wants(path)
|
|
|
|
if match {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return match
|
|
|
|
}
|
|
|
|
|
2024-05-01 21:03:26 +03:00
|
|
|
func (p *Pipeline) Apply(ctx context.Context, paths []*walk.File) error {
|
2024-04-19 12:57:41 +03:00
|
|
|
for _, f := range p.sequence {
|
2024-04-25 11:17:51 +03:00
|
|
|
if err := f.Apply(ctx, paths, len(p.sequence) > 1); err != nil {
|
2024-04-19 12:57:41 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|