aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorJeff Halter <868228+jhalter@users.noreply.github.com>2026-03-15 20:48:45 -0700
committerJeff Halter <868228+jhalter@users.noreply.github.com>2026-03-15 20:48:45 -0700
commit92f4b7af1073b6cdd0786ce2090d6e446e0903ad (patch)
tree4cfb332a36aa093d6bb4238967f1b0728fbb498f /internal
parentf78ab940a8c09702be3e0ba310ac56e02fa76701 (diff)
Extract shared readFrom helper to deduplicate io.Reader boilerplate
13 types implemented identical offset-based io.Reader patterns with 5-7 lines of copy-and-offset-tracking code each. Extract a shared readFrom(p, offset, data) helper and reduce each Read() method to a one-liner delegating to it.
Diffstat (limited to 'internal')
-rw-r--r--internal/mobius/agreement.go11
-rw-r--r--internal/mobius/news.go11
-rw-r--r--internal/mobius/read_helper.go14
3 files changed, 16 insertions, 20 deletions
diff --git a/internal/mobius/agreement.go b/internal/mobius/agreement.go
index c2a67c5..d9cf2d4 100644
--- a/internal/mobius/agreement.go
+++ b/internal/mobius/agreement.go
@@ -2,7 +2,6 @@ package mobius
import (
"fmt"
- "io"
"os"
"path/filepath"
"strings"
@@ -60,15 +59,7 @@ func (a *Agreement) Read(p []byte) (int, error) {
a.mu.Lock()
defer a.mu.Unlock()
- if a.readOffset >= len(a.data) {
- return 0, io.EOF // All bytes have been read
- }
-
- n := copy(p, a.data[a.readOffset:])
-
- a.readOffset += n
-
- return n, nil
+ return readFrom(p, &a.readOffset, a.data)
}
func (a *Agreement) Seek(offset int64, _ int) (int64, error) {
diff --git a/internal/mobius/news.go b/internal/mobius/news.go
index c63c7f7..c94794c 100644
--- a/internal/mobius/news.go
+++ b/internal/mobius/news.go
@@ -2,7 +2,6 @@ package mobius
import (
"fmt"
- "io"
"os"
"slices"
"strings"
@@ -49,15 +48,7 @@ func (f *FlatNews) Read(p []byte) (int, error) {
f.mu.Lock()
defer f.mu.Unlock()
- if f.readOffset >= len(f.data) {
- return 0, io.EOF // All bytes have been read
- }
-
- n := copy(p, f.data[f.readOffset:])
-
- f.readOffset += n
-
- return n, nil
+ return readFrom(p, &f.readOffset, f.data)
}
// Write implements io.Writer for flat news.
diff --git a/internal/mobius/read_helper.go b/internal/mobius/read_helper.go
new file mode 100644
index 0000000..ab4002a
--- /dev/null
+++ b/internal/mobius/read_helper.go
@@ -0,0 +1,14 @@
+package mobius
+
+import "io"
+
+// readFrom is a shared helper for types that implement io.Reader via an
+// offset-based copy from a serialized byte slice.
+func readFrom(p []byte, offset *int, data []byte) (int, error) {
+ if *offset >= len(data) {
+ return 0, io.EOF
+ }
+ n := copy(p, data[*offset:])
+ *offset += n
+ return n, nil
+}