-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
47 lines (40 loc) · 1.31 KB
/
command.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
41
42
43
44
45
46
47
package fanet
import "fmt"
type Command interface {
Sentence() string
}
type commandParser func(*tokenizer) (Command, error)
func makeCommandParser[C Command](f func(*tokenizer) (C, error)) commandParser {
return func(tok *tokenizer) (Command, error) {
return f(tok)
}
}
var commandParsers = map[string]commandParser{
"#DBG": makeCommandParser(parseDBGCommand),
"#DGJ": makeCommandParser(parseDGJCommand),
"#DGL": makeCommandParser(parseDGLCommand),
"#DGP": makeCommandParser(parseDGPCommand),
"#DGV": makeCommandParser(parseDGVCommand),
"#FAP": makeCommandParser(parseFAPCommand),
"#FAT": makeCommandParser(parseFATCommand),
"#FAX": makeCommandParser(parseFAXCommand),
"#FNA": makeCommandParser(parseFNACommand),
"#FNC": makeCommandParser(parseFNCCommand),
"#FNM": makeCommandParser(parseFNMCommand),
"#FNS": makeCommandParser(parseFNSCommand),
"#FNT": makeCommandParser(parseFNTCommand),
}
// ParseCommand parses a response from data.
func ParseCommand(data []byte) (Command, error) {
tok := newTokenizer(data)
header := tok.header()
commandParser, ok := commandParsers[header]
if !ok {
return nil, fmt.Errorf("%s: unknown command", header)
}
return commandParser(tok)
}
// ParseCommandString parses a response from s.
func ParseCommandString(s string) (Command, error) {
return ParseCommand([]byte(s))
}