From 985651bbabce5f9d4b8f0e8103610b67db4147ea Mon Sep 17 00:00:00 2001 From: tjpcc Date: Wed, 30 Aug 2023 10:31:28 -0600 Subject: [slog backend] re-ordering for readability --- slog/backend.go | 96 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/slog/backend.go b/slog/backend.go index 613601c..b68bf90 100644 --- a/slog/backend.go +++ b/slog/backend.go @@ -39,7 +39,7 @@ func NewBackend(logger log.Logger, waitTime time.Duration) (nntpserver.Backend, waitTime = DefaultWaitTime } - b := &backend{logger: logger, waitTime: waitTime, index: make([]indexEntry, 0)} + b := &backend{logger: logger, waitTime: waitTime, index: []indexEntry{}} if err := b.refreshIndex(); err != nil { return nil, err } @@ -53,11 +53,6 @@ type backend struct { index []indexEntry } -func (b backend) debug(keyvals ...any) error { return level.Debug(b.logger).Log(keyvals...) } -func (b backend) info(keyvals ...any) error { return level.Info(b.logger).Log(keyvals...) } -func (b backend) warn(keyvals ...any) error { return level.Warn(b.logger).Log(keyvals...) } -func (b backend) err(keyvals ...any) error { return level.Error(b.logger).Log(keyvals...) } - func (b backend) ListGroups(max int) ([]*nntp.Group, error) { return []*nntp.Group{group}, nil } @@ -158,48 +153,10 @@ func (b backend) Authorized() bool { return func (b backend) AllowPost() bool { return true } func (b backend) Authenticate(_, _ string) (nntpserver.Backend, error) { return nil, nil } -type indexEntry struct { - id string - ts time.Time - title string - user string - author string -} - -const indexTimeFmt = "2006-01-02 15:04:05.999999" - -func (ie *indexEntry) UnmarshalJSON(b []byte) error { - var tgt struct { - Timestamp string - Id string - Title string - } - if err := json.Unmarshal(b, &tgt); err != nil { - return err - } - - ts, err := time.Parse(indexTimeFmt, tgt.Timestamp) - if err != nil { - return err - } - - ie.id = tgt.Id - ie.ts = ts - ie.title = tgt.Title - return nil -} - -func (ie *indexEntry) MarshalJSON() ([]byte, error) { - return json.Marshal(map[string]any{ - "id": ie.id, - "timestamp": ie.ts.Format(indexTimeFmt), - "title": ie.title, - }) -} - -func (ie indexEntry) messageID() string { - return fmt.Sprintf("<%s.%s>", ie.id, ie.author) -} +func (b backend) debug(keyvals ...any) error { return level.Debug(b.logger).Log(keyvals...) } +func (b backend) info(keyvals ...any) error { return level.Info(b.logger).Log(keyvals...) } +func (b backend) warn(keyvals ...any) error { return level.Warn(b.logger).Log(keyvals...) } +func (b backend) err(keyvals ...any) error { return level.Error(b.logger).Log(keyvals...) } func (b *backend) refreshIndex() error { now := time.Now() @@ -255,6 +212,49 @@ func (b *backend) refreshIndex() error { return nil } +type indexEntry struct { + id string + ts time.Time + title string + user string + author string +} + +const indexTimeFmt = "2006-01-02 15:04:05.999999" + +func (ie *indexEntry) UnmarshalJSON(b []byte) error { + var tgt struct { + Timestamp string + Id string + Title string + } + if err := json.Unmarshal(b, &tgt); err != nil { + return err + } + + ts, err := time.Parse(indexTimeFmt, tgt.Timestamp) + if err != nil { + return err + } + + ie.id = tgt.Id + ie.ts = ts + ie.title = tgt.Title + return nil +} + +func (ie *indexEntry) MarshalJSON() ([]byte, error) { + return json.Marshal(map[string]any{ + "id": ie.id, + "timestamp": ie.ts.Format(indexTimeFmt), + "title": ie.title, + }) +} + +func (ie indexEntry) messageID() string { + return fmt.Sprintf("<%s.%s>", ie.id, ie.author) +} + func myIndexPath() string { return path.Join(os.Getenv("HOME"), ".slog", "index") } -- cgit v1.2.3