diff options
| author | Ophestra <cat@gensokyo.uk> | 2026-01-03 22:02:15 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2026-01-03 22:03:25 +0900 |
| commit | 863d3dcf9f51015f800a26c5dc63576f114d4380 (patch) | |
| tree | 4d1131f7fb2b34e0cd568296e5d0d8f2a42d81d5 /internal/pkg/pkg.go | |
| parent | 8ad990906555d843d1212c5eb8a1ac194031426c (diff) | |
internal/pkg: wrap checksum string encoding
This wraps base64.URLEncoding.EncodeToString for cleaner call site.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/pkg/pkg.go')
| -rw-r--r-- | internal/pkg/pkg.go | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/internal/pkg/pkg.go b/internal/pkg/pkg.go index 40bad6ff..64df5df4 100644 --- a/internal/pkg/pkg.go +++ b/internal/pkg/pkg.go @@ -24,6 +24,16 @@ type ( ID Checksum ) +// Encode is abbreviation for base64.URLEncoding.EncodeToString(checksum[:]). +func Encode(checksum Checksum) string { + return base64.URLEncoding.EncodeToString(checksum[:]) +} + +// encode is abbreviation for base64.URLEncoding.EncodeToString(checksum[:]). +func encode(checksum *Checksum) string { + return base64.URLEncoding.EncodeToString(checksum[:]) +} + // MustDecode decodes a string representation of [Checksum] and panics if there // is a decoding error or the resulting data is too short. func MustDecode(s string) (checksum Checksum) { @@ -143,7 +153,7 @@ func (c *Cache) LoadFile(id ID) ( ) { pathname = c.base.Append( dirIdentifier, - base64.URLEncoding.EncodeToString(id[:]), + Encode(id), ) c.mu.RLock() @@ -160,15 +170,15 @@ type ChecksumMismatchError struct { } func (e *ChecksumMismatchError) Error() string { - return "got " + base64.URLEncoding.EncodeToString(e.Got[:]) + - " instead of " + base64.URLEncoding.EncodeToString(e.Want[:]) + return "got " + Encode(e.Got) + + " instead of " + Encode(e.Want) } // pathname returns the content-addressed pathname for a [Checksum]. func (c *Cache) pathname(checksum *Checksum) *check.Absolute { return c.base.Append( dirChecksum, - base64.URLEncoding.EncodeToString(checksum[:]), + encode(checksum), ) } @@ -176,7 +186,7 @@ func (c *Cache) pathname(checksum *Checksum) *check.Absolute { func (c *Cache) pathnameIdent(id *ID) *check.Absolute { return c.base.Append( dirIdentifier, - base64.URLEncoding.EncodeToString(id[:]), + encode((*Checksum)(id)), ) } |
