aboutsummaryrefslogtreecommitdiff
path: root/forged/internal/misc
diff options
context:
space:
mode:
Diffstat (limited to 'forged/internal/misc')
-rw-r--r--forged/internal/misc/back.go11
-rw-r--r--forged/internal/misc/deploy.go22
-rw-r--r--forged/internal/misc/iter.go23
-rw-r--r--forged/internal/misc/misc.go5
-rw-r--r--forged/internal/misc/panic.go19
-rw-r--r--forged/internal/misc/slices.go17
-rw-r--r--forged/internal/misc/trivial.go48
-rw-r--r--forged/internal/misc/unsafe.go20
-rw-r--r--forged/internal/misc/url.go118
-rw-r--r--forged/internal/misc/usock.go23
10 files changed, 0 insertions, 306 deletions
diff --git a/forged/internal/misc/back.go b/forged/internal/misc/back.go
deleted file mode 100644
index 5351359..0000000
--- a/forged/internal/misc/back.go
+++ /dev/null
@@ -1,11 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-only
-// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
-
-package misc
-
-// ErrorBack wraps a value and a channel for communicating an associated error.
-// Typically used to get an error response after sending data across a channel.
-type ErrorBack[T any] struct {
- Content T
- ErrorChan chan error
-}
diff --git a/forged/internal/misc/deploy.go b/forged/internal/misc/deploy.go
deleted file mode 100644
index 3ee5f92..0000000
--- a/forged/internal/misc/deploy.go
+++ /dev/null
@@ -1,22 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-only
-// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
-
-package misc
-
-import (
- "io"
- "io/fs"
- "os"
-)
-
-// DeployBinary copies the contents of a binary file to the target destination path.
-// The destination file is created with executable permissions.
-func DeployBinary(src fs.File, dst string) (err error) {
- var dstFile *os.File
- if dstFile, err = os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o755); err != nil {
- return err
- }
- defer dstFile.Close()
- _, err = io.Copy(dstFile, src)
- return err
-}
diff --git a/forged/internal/misc/iter.go b/forged/internal/misc/iter.go
deleted file mode 100644
index 61a96f4..0000000
--- a/forged/internal/misc/iter.go
+++ /dev/null
@@ -1,23 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-only
-// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
-
-package misc
-
-import "iter"
-
-// iterSeqLimit returns an iterator equivalent to the supplied one, but stops
-// after n iterations.
-func IterSeqLimit[T any](s iter.Seq[T], n uint) iter.Seq[T] {
- return func(yield func(T) bool) {
- var iterations uint
- for v := range s {
- if iterations > n-1 {
- return
- }
- if !yield(v) {
- return
- }
- iterations++
- }
- }
-}
diff --git a/forged/internal/misc/misc.go b/forged/internal/misc/misc.go
deleted file mode 100644
index e9e10ab..0000000
--- a/forged/internal/misc/misc.go
+++ /dev/null
@@ -1,5 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-only
-// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
-
-// Package misc provides miscellaneous functions and other definitions.
-package misc
diff --git a/forged/internal/misc/panic.go b/forged/internal/misc/panic.go
deleted file mode 100644
index 34c49c5..0000000
--- a/forged/internal/misc/panic.go
+++ /dev/null
@@ -1,19 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-only
-// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
-
-package misc
-
-// FirstOrPanic returns the value or panics if the error is non-nil.
-func FirstOrPanic[T any](v T, err error) T {
- if err != nil {
- panic(err)
- }
- return v
-}
-
-// NoneOrPanic panics if the provided error is non-nil.
-func NoneOrPanic(err error) {
- if err != nil {
- panic(err)
- }
-}
diff --git a/forged/internal/misc/slices.go b/forged/internal/misc/slices.go
deleted file mode 100644
index 3ad0211..0000000
--- a/forged/internal/misc/slices.go
+++ /dev/null
@@ -1,17 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-only
-// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
-
-package misc
-
-import "strings"
-
-// sliceContainsNewlines returns true if and only if the given slice contains
-// one or more strings that contains newlines.
-func SliceContainsNewlines(s []string) bool {
- for _, v := range s {
- if strings.Contains(v, "\n") {
- return true
- }
- }
- return false
-}
diff --git a/forged/internal/misc/trivial.go b/forged/internal/misc/trivial.go
deleted file mode 100644
index e59c17e..0000000
--- a/forged/internal/misc/trivial.go
+++ /dev/null
@@ -1,48 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-only
-// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
-
-package misc
-
-import (
- "net/url"
- "strings"
-)
-
-// These are all trivial functions that are intended to be used in HTML
-// templates.
-
-// FirstLine returns the first line of a string.
-func FirstLine(s string) string {
- before, _, _ := strings.Cut(s, "\n")
- return before
-}
-
-// PathEscape escapes the input as an URL path segment.
-func PathEscape(s string) string {
- return url.PathEscape(s)
-}
-
-// QueryEscape escapes the input as an URL query segment.
-func QueryEscape(s string) string {
- return url.QueryEscape(s)
-}
-
-// Dereference dereferences a pointer.
-func Dereference[T any](p *T) T {
- return *p
-}
-
-// DereferenceOrZero dereferences a pointer. If the pointer is nil, the zero
-// value of its associated type is returned instead.
-func DereferenceOrZero[T any](p *T) T {
- if p != nil {
- return *p
- }
- var z T
- return z
-}
-
-// Minus subtracts two numbers.
-func Minus(a, b int) int {
- return a - b
-}
diff --git a/forged/internal/misc/unsafe.go b/forged/internal/misc/unsafe.go
deleted file mode 100644
index 6c2192f..0000000
--- a/forged/internal/misc/unsafe.go
+++ /dev/null
@@ -1,20 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-only
-// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
-
-package misc
-
-import "unsafe"
-
-// StringToBytes converts a string to a byte slice without copying the string.
-// Memory is borrowed from the string.
-// The resulting byte slice must not be modified in any form.
-func StringToBytes(s string) (bytes []byte) {
- return unsafe.Slice(unsafe.StringData(s), len(s))
-}
-
-// BytesToString converts a byte slice to a string without copying the bytes.
-// Memory is borrowed from the byte slice.
-// The source byte slice must not be modified.
-func BytesToString(b []byte) string {
- return unsafe.String(unsafe.SliceData(b), len(b))
-}
diff --git a/forged/internal/misc/url.go b/forged/internal/misc/url.go
deleted file mode 100644
index 346ff76..0000000
--- a/forged/internal/misc/url.go
+++ /dev/null
@@ -1,118 +0,0 @@
-// SPDX-License-Identifier: AGPL-3.0-only
-// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
-
-package misc
-
-import (
- "net/http"
- "net/url"
- "strings"
-)
-
-// ParseReqURI parses an HTTP request URL, and returns a slice of path segments
-// and the query parameters. It handles %2F correctly.
-func ParseReqURI(requestURI string) (segments []string, params url.Values, err error) {
- path, paramsStr, _ := strings.Cut(requestURI, "?")
-
- segments, err = PathToSegments(path)
- if err != nil {
- return
- }
-
- params, err = url.ParseQuery(paramsStr)
- return
-}
-
-// PathToSegments splits a path into unescaped segments. It handles %2F correctly.
-func PathToSegments(path string) (segments []string, err error) {
- segments = strings.Split(strings.TrimPrefix(path, "/"), "/")
-
- for i, segment := range segments {
- segments[i], err = url.PathUnescape(segment)
- if err != nil {
- return
- }
- }
-
- return
-}
-
-// RedirectDir returns true and redirects the user to a version of the URL with
-// a trailing slash, if and only if the request URL does not already have a
-// trailing slash.
-func RedirectDir(writer http.ResponseWriter, request *http.Request) bool {
- requestURI := request.RequestURI
-
- pathEnd := strings.IndexAny(requestURI, "?#")
- var path, rest string
- if pathEnd == -1 {
- path = requestURI
- } else {
- path = requestURI[:pathEnd]
- rest = requestURI[pathEnd:]
- }
-
- if !strings.HasSuffix(path, "/") {
- http.Redirect(writer, request, path+"/"+rest, http.StatusSeeOther)
- return true
- }
- return false
-}
-
-// RedirectNoDir returns true and redirects the user to a version of the URL
-// without a trailing slash, if and only if the request URL has a trailing
-// slash.
-func RedirectNoDir(writer http.ResponseWriter, request *http.Request) bool {
- requestURI := request.RequestURI
-
- pathEnd := strings.IndexAny(requestURI, "?#")
- var path, rest string
- if pathEnd == -1 {
- path = requestURI
- } else {
- path = requestURI[:pathEnd]
- rest = requestURI[pathEnd:]
- }
-
- if strings.HasSuffix(path, "/") {
- http.Redirect(writer, request, strings.TrimSuffix(path, "/")+rest, http.StatusSeeOther)
- return true
- }
- return false
-}
-
-// RedirectUnconditionally unconditionally redirects the user back to the
-// current page while preserving query parameters.
-func RedirectUnconditionally(writer http.ResponseWriter, request *http.Request) {
- requestURI := request.RequestURI
-
- pathEnd := strings.IndexAny(requestURI, "?#")
- var path, rest string
- if pathEnd == -1 {
- path = requestURI
- } else {
- path = requestURI[:pathEnd]
- rest = requestURI[pathEnd:]
- }
-
- http.Redirect(writer, request, path+rest, http.StatusSeeOther)
-}
-
-// SegmentsToURL joins URL segments to the path component of a URL.
-// Each segment is escaped properly first.
-func SegmentsToURL(segments []string) string {
- for i, segment := range segments {
- segments[i] = url.PathEscape(segment)
- }
- return strings.Join(segments, "/")
-}
-
-// AnyContain returns true if and only if ss contains a string that contains c.
-func AnyContain(ss []string, c string) bool {
- for _, s := range ss {
- if strings.Contains(s, c) {
- return true
- }
- }
- return false
-}
diff --git a/forged/internal/misc/usock.go b/forged/internal/misc/usock.go
deleted file mode 100644
index 357fa43..0000000
--- a/forged/internal/misc/usock.go
+++ /dev/null
@@ -1,23 +0,0 @@
-package misc
-
-import (
- "errors"
- "fmt"
- "net"
- "syscall"
-)
-
-func ListenUnixSocket(path string) (listener net.Listener, replaced bool, err error) {
- listener, err = net.Listen("unix", path)
- if errors.Is(err, syscall.EADDRINUSE) {
- replaced = true
- if unlinkErr := syscall.Unlink(path); unlinkErr != nil {
- return listener, false, fmt.Errorf("remove existing socket %q: %w", path, unlinkErr)
- }
- listener, err = net.Listen("unix", path)
- }
- if err != nil {
- return listener, replaced, fmt.Errorf("listen on unix socket %q: %w", path, err)
- }
- return listener, replaced, nil
-}