moth/cmd/transpile/category.go

48 lines
985 B
Go
Raw Normal View History

2019-08-17 16:00:15 -06:00
package main
import (
2019-08-18 21:59:06 -06:00
"log"
"strconv"
2020-08-28 17:41:17 -06:00
"github.com/spf13/afero"
)
2019-08-18 21:59:06 -06:00
2020-08-28 17:41:17 -06:00
// NewCategory returns a new category for the given path in the given fs.
func NewCategory(fs afero.Fs, cat string) Category {
return Category{
2020-09-01 20:12:57 -06:00
Fs: NewBasePathFs(fs, cat),
2019-08-18 21:59:06 -06:00
}
}
2020-08-28 17:41:17 -06:00
// Category represents an on-disk category.
type Category struct {
afero.Fs
}
2019-08-18 21:59:06 -06:00
2020-08-28 17:41:17 -06:00
// Puzzles returns a list of puzzle values.
func (c Category) Puzzles() ([]int, error) {
puzzleEntries, err := afero.ReadDir(c, ".")
2019-08-18 21:59:06 -06:00
if err != nil {
return nil, err
}
2020-08-28 17:41:17 -06:00
puzzles := make([]int, 0, len(puzzleEntries))
for _, ent := range puzzleEntries {
if !ent.IsDir() {
continue
}
2020-08-28 17:41:17 -06:00
if points, err := strconv.Atoi(ent.Name()); err != nil {
log.Println("Skipping non-numeric directory", ent.Name())
continue
} else {
puzzles = append(puzzles, points)
}
}
2020-08-28 17:41:17 -06:00
return puzzles, nil
}
2020-09-01 20:12:57 -06:00
// PuzzleDir returns the PuzzleDir associated with points.
func (c Category) PuzzleDir(points int) *PuzzleDir {
return NewPuzzleDir(c.Fs, points)
2019-08-18 21:59:06 -06:00
}