-
Notifications
You must be signed in to change notification settings - Fork 0
/
hanjie.go
40 lines (31 loc) · 806 Bytes
/
hanjie.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Package hanjie contains any entities and instruments to read, write and validate puzzles.
package hanjie
import (
"io"
"gopkg.in/yaml.v3"
"github.com/alexeyco/hanjie/ast"
)
// Validator interface.
type Validator interface {
Validate(ast.PuzzleSet) error
}
// Read set of puzzles from io.Reader.
func Read(r io.Reader, options ...Option) (*ast.PuzzleSet, error) {
var puzzleSet ast.PuzzleSet
if err := yaml.NewDecoder(r).Decode(&puzzleSet); err != nil {
return nil, err
}
o := newOptions()
for _, opt := range options {
opt(&o)
}
return &puzzleSet, nil
}
// Write set of puzzles to io.Writer.
func Write(w io.Writer, puzzleSet *ast.PuzzleSet, options ...Option) error {
o := newOptions()
for _, opt := range options {
opt(&o)
}
return yaml.NewEncoder(w).Encode(puzzleSet)
}