all repos — searchix @ d99fb7b6585837a02f43d1c3f37ed172baa1bf5d

Search engine for NixOS, nix-darwin, home-manager and NUR users

fix: panic when source order > number of sources

Fixes: https://todo.sr.ht/~alanpearce/searchix/13
Alan Pearce alan@alanpearce.eu
Tue, 11 Mar 2025 19:35:48 +0100
commit

d99fb7b6585837a02f43d1c3f37ed172baa1bf5d

parent

fd19126c36267a65c0f3b5b5e27009a2b7899368

1 files changed, 11 insertions(+), 4 deletions(-)

jump to
M internal/server/mux.gointernal/server/mux.go
@@ -1,14 +1,18 @@ package server
 
 import (
+	"cmp"
 	"context"
 	"encoding/xml"
 	"fmt"
+	"maps"
 	"math"
 	"net/http"
 	"net/url"
 	"path"
+	"slices"
 	"strconv"
+	"strings"
 	"time"
 
 	"go.alanpearce.eu/searchix/frontend"
@@ -47,10 +51,13 @@ ) }
 
 func sortSources(ss map[string]*config.Source) {
-	sources = make([]*config.Source, len(ss))
-	for _, v := range ss {
-		sources[v.Order] = v
-	}
+	sources = slices.SortedFunc(maps.Values(ss), func(a, b *config.Source) int {
+		return cmp.Or(
+			cmp.Compare(a.Order, b.Order),
+			strings.Compare(a.Key, b.Key),
+			strings.Compare(a.Name, b.Name),
+		)
+	})
 }
 
 func NewMux(