-
Notifications
You must be signed in to change notification settings - Fork 2
/
hwrule.go
52 lines (48 loc) · 1.17 KB
/
hwrule.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
48
49
50
51
52
package sb
import (
"path/filepath"
"github.com/NTHU-lsalab/sb/intrange"
"github.com/NTHU-lsalab/sb/pb"
"github.com/BurntSushi/toml"
)
func LoadHomework(filename string) *pb.Homework {
hw := new(struct {
Target string
Runner string
Files []*pb.SourceFile
PenaltyTime toml.Primitive `toml:"penalty_time"`
Cases []string
})
metadata, err := toml.DecodeFile(filename, hw)
if err != nil {
panic(err)
}
name := filepath.Base(filename)
name = name[:len(name)-len(filepath.Ext(name))]
if hw.Target == "" {
hw.Target = name
}
var penaltyTime float64
err = metadata.PrimitiveDecode(hw.PenaltyTime, &penaltyTime)
if err != nil {
var penaltyTimeAsInt int64
err = metadata.PrimitiveDecode(hw.PenaltyTime, &penaltyTimeAsInt)
if err != nil {
panic(err)
}
penaltyTime = float64(penaltyTimeAsInt)
}
var expandedCases []string
for _, casestr := range hw.Cases {
expandedCases = append(expandedCases, intrange.MustExpand(casestr)...)
}
hw.Cases = expandedCases
return &pb.Homework{
Name: name,
Target: hw.Target,
Runner: hw.Runner,
Files: hw.Files,
PenaltyTime: penaltyTime,
Cases: hw.Cases,
}
}