-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathengine.go
More file actions
49 lines (40 loc) · 756 Bytes
/
engine.go
File metadata and controls
49 lines (40 loc) · 756 Bytes
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
41
42
43
44
45
46
47
48
49
package ticketfile
import (
"bufio"
"fmt"
"io"
"sync"
)
type Converter interface {
Convert(cmd Command) ([]byte, error)
}
type Engine struct {
conv Converter
w *bufio.Writer
mu sync.Mutex
}
func NewEngine(w io.Writer, c Converter) *Engine {
return &Engine{
conv: c,
w: bufio.NewWriter(w),
}
}
func (e *Engine) Render(r io.Reader) error {
e.mu.Lock()
defer e.mu.Unlock()
cmds, err := parse(r)
if err != nil {
return fmt.Errorf("parsing error : %s", err)
}
for _, cmd := range cmds {
rawBytes, err := e.conv.Convert(cmd)
if err != nil {
return fmt.Errorf("converter error : %s", err)
}
_, err = e.w.Write(rawBytes)
if err != nil {
return fmt.Errorf("write error : %s", err)
}
}
return e.w.Flush()
}