-
Notifications
You must be signed in to change notification settings - Fork 3
/
task.go
92 lines (80 loc) · 1.42 KB
/
task.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package pomo
import (
"fmt"
"time"
)
type Status int
const (
Todo Status = iota
Doing
Done
)
func (s Status) String() string {
switch s {
case Todo:
return "todo"
case Doing:
return "doing"
case Done:
return "done"
default:
return "unknown"
}
}
func ParseStatus(s string) (Status, error) {
switch s {
case "todo":
return Todo, nil
case "doing":
return Doing, nil
case "done":
return Done, nil
default:
return 0, fmt.Errorf("unknown status: %s", s)
}
}
type Task struct {
Status Status
UpdatedAt time.Time
Name string
Notes string
}
func (t Task) MarshalYAML() (any, error) {
var updatedAt string
if !t.UpdatedAt.IsZero() {
updatedAt = t.UpdatedAt.Format(time.RFC3339Nano)
}
return task{
Status: t.Status.String(),
Name: t.Name,
Notes: t.Notes,
UpdatedAt: updatedAt,
}, nil
}
func (t *Task) UnmarshalYAML(unmarshal func(any) error) error {
var data task
if err := unmarshal(&data); err != nil {
return err
}
status, err := ParseStatus(data.Status)
if err != nil {
return err
}
updatedAt, err := parseTime(data.UpdatedAt)
if err != nil {
return err
}
*t = Task{
Status: status,
Name: data.Name,
Notes: data.Notes,
UpdatedAt: updatedAt,
}
return nil
}
type task struct {
Status string `yaml:"status"`
Name string `yaml:"name"`
Notes string `yaml:"notes,omitempty"`
UpdatedAt string `yaml:"updatedAt,omitempty"`
}