diff options
| author | Paul Buetow <paul@buetow.org> | 2024-09-30 10:51:49 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-09-30 10:51:49 +0300 |
| commit | cbdce79d25206573d1f79c9c647dcaa251b69463 (patch) | |
| tree | 20f3bcbf76b28e8577a0dba3eb586b33e73a7699 /internal/entry/entry.go | |
| parent | 4d904e44a18acec779ef1d09de5d30e4890d1918 (diff) | |
add entry "class"
Diffstat (limited to 'internal/entry/entry.go')
| -rw-r--r-- | internal/entry/entry.go | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/internal/entry/entry.go b/internal/entry/entry.go new file mode 100644 index 0000000..4e837a0 --- /dev/null +++ b/internal/entry/entry.go @@ -0,0 +1,75 @@ +package entry + +import ( + "fmt" + "strings" + "time" + + "codeberg.org/snonux/gos/internal/format" +) + +type State int + +const ( + Unknown State = iota + Queued + Posted +) + +func (s State) String() string { + switch s { + case Unknown: + return "unknown" + case Queued: + return "queued" + case Posted: + return "posted" + default: + panic(fmt.Sprintf("unknown state: %d", int(s))) + } +} + +// The time this code was written a:round, actually. +const oldestValidTime = "20240922-102800" + +type Entry struct { + Path string + Time time.Time + State State +} + +// filePath format: /foo/foobarbaz.something.here.txt.STAMP.{posted,queued} +func New(filePath string) (Entry, error) { + ent := Entry{Path: filePath} + + // We want to get the STAMP! + parts := strings.Split(filePath, ".") + if len(parts) < 4 { + return ent, fmt.Errorf("not a valid entry path: %s", filePath) + } + + switch parts[len(parts)-1] { + case "queued": + ent.State = Queued + case "posted": + ent.State = Posted + default: + return ent, fmt.Errorf("can't parse state from path: %s", filePath) + } + + var err error + if ent.Time, err = time.Parse(format.Time, parts[len(parts)-2]); err != nil { + return ent, err + } + + oldestValid, err := time.Parse(format.Time, oldestValidTime) + if err != nil { + panic(err) + } + + if ent.Time.Before(oldestValid) { + return ent, fmt.Errorf("entry time does not seem legic, it is too old: %v", ent.Time) + } + + return ent, nil +} |
