Files
go-fa-api/ids.go
2026-05-25 22:27:18 +02:00

37 lines
1.0 KiB
Go

package fa
import (
"fmt"
"strconv"
)
// SubmissionID identifies a submission. FA exposes these as positive integers
// in URLs of the form /view/{id}/.
type SubmissionID int64
func (id SubmissionID) String() string { return strconv.FormatInt(int64(id), 10) }
// JournalID identifies a journal entry. URL form: /journal/{id}/.
type JournalID int64
func (id JournalID) String() string { return strconv.FormatInt(int64(id), 10) }
// CommentID identifies a comment within a submission or journal page.
type CommentID int64
func (id CommentID) String() string { return strconv.FormatInt(int64(id), 10) }
// parseID parses a positive integer from a string and returns it as T.
// Returns 0 and a wrapped error if s is empty or non-numeric. Used by parsers
// to extract IDs from hrefs.
func parseID[T ~int64](s string) (T, error) {
if s == "" {
return 0, fmt.Errorf("parse id: empty string")
}
n, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0, fmt.Errorf("parse id %q: %w", s, err)
}
return T(n), nil
}