diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-07-03 04:11:38 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-07-03 04:36:59 +0900 |
| commit | 087959e81bcd52104676ccacedd605e6491b4376 (patch) | |
| tree | 11cd6eccbfd9278f6c99d33191b320bb4e728888 /internal/app/state/id.go | |
| parent | e6967b8bbb5ceec3abbd002f52cff1167a969e9e (diff) | |
app: remove split implementation
It is completely nonsensical and highly error-prone to have multiple implementations of this in the same build. This should be switched at compile time instead therefore the split packages are pointless.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/app/state/id.go')
| -rw-r--r-- | internal/app/state/id.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/internal/app/state/id.go b/internal/app/state/id.go new file mode 100644 index 00000000..11bbc3fe --- /dev/null +++ b/internal/app/state/id.go @@ -0,0 +1,48 @@ +package state + +import ( + "crypto/rand" + "encoding/hex" + "errors" + "fmt" +) + +type ID [16]byte + +var ( + ErrInvalidLength = errors.New("string representation must have a length of 32") +) + +func (a *ID) String() string { + return hex.EncodeToString(a[:]) +} + +func NewAppID(id *ID) error { + _, err := rand.Read(id[:]) + return err +} + +func ParseAppID(id *ID, s string) error { + if len(s) != 32 { + return ErrInvalidLength + } + + for i, b := range s { + if b < '0' || b > 'f' { + return fmt.Errorf("invalid char %q at byte %d", b, i) + } + + v := uint8(b) + if v > '9' { + v = 10 + v - 'a' + } else { + v -= '0' + } + if i%2 == 0 { + v <<= 4 + } + id[i/2] += v + } + + return nil +} |
