summaryrefslogtreecommitdiffstats
path: root/internal/bot/format.go
diff options
context:
space:
mode:
authorMurilo Ijanc <[email protected]>2026-07-27 21:12:05 -0300
committerMurilo Ijanc <[email protected]>2026-07-27 21:12:05 -0300
commit97b49ae7081b989274b313532ff17c71694097a9 (patch)
tree6595e0d5a39d04971a71e886bc3f8cc589741f5d /internal/bot/format.go
parent83ce95cf10fd9e9d581362453c558239d37ae06e (diff)
downloadtelegram-97b49ae7081b989274b313532ff17c71694097a9.tar.gz
Read the faixa from the client instead of fetching it againHEADv0.2.1main
cep.is/go v0.2.0 exposes the municipality as Error.Faixa, which is what internal/lookup existed to work around: v0.1.0 discarded the 404 body, so the bot re-issued the request just to read the block back. The extra GET on the 404 path is gone, and with it the second timeout, the body size cap and the "is it still a 404?" check that guarded it. The replies are byte for byte what they were -- the formatting tests did not change a single assertion, only the type they build.
Diffstat (limited to 'internal/bot/format.go')
-rw-r--r--internal/bot/format.go33
1 files changed, 27 insertions, 6 deletions
diff --git a/internal/bot/format.go b/internal/bot/format.go
index b94dee4..86a0ac9 100644
--- a/internal/bot/format.go
+++ b/internal/bot/format.go
@@ -9,8 +9,6 @@ import (
"time"
cepis "cep.is/go"
-
- "cep.is/telegrambot/internal/lookup"
)
// ParseMode is the formatting Telegram applies to every message this bot sends.
@@ -197,14 +195,14 @@ const (
// CEPs are spread over nearly five thousand numbers — so its ends are the
// extremes of what exists, not a list of what is valid, and the reply says so.
func notFoundMessage(err error) string {
- faixa := lookup.FaixaOf(err)
+ faixa := faixaOf(err)
if faixa == nil || faixa.Localidade == "" {
return notFoundHead + "\n" + notFoundAdvice
}
var b strings.Builder
b.WriteString(notFoundHead + "\n")
- b.WriteString("Ele cairia em <b>" + esc(faixa.Place()) + "</b>")
+ b.WriteString("Ele cairia em <b>" + esc(place(faixa)) + "</b>")
count := faixaCount(faixa)
span := faixaSpan(faixa)
@@ -225,10 +223,33 @@ func notFoundMessage(err error) string {
return b.String()
}
+// faixaOf returns the municipality the API named on a 404, or nil when it named
+// none. Any error is safe to pass, including nil: only a *cepis.Error carrying
+// a faixa yields one.
+func faixaOf(err error) *cepis.Faixa {
+ var cerr *cepis.Error
+ if errors.As(err, &cerr) {
+ return cerr.Faixa
+ }
+ return nil
+}
+
+// place is the municipality as it is written to a user: "Areal/RJ", or just
+// "Areal" when the API sent no UF.
+func place(faixa *cepis.Faixa) string {
+ if faixa == nil {
+ return ""
+ }
+ if faixa.UF == "" {
+ return faixa.Localidade
+ }
+ return faixa.Localidade + "/" + faixa.UF
+}
+
// faixaCount renders how many CEPs the municipality has, or "" when the API did
// not say. Without it the two ends of the range read as a count of one big
// block, which is the misreading the number exists to prevent.
-func faixaCount(faixa *lookup.Faixa) string {
+func faixaCount(faixa *cepis.Faixa) string {
switch {
case faixa.CEPs <= 0:
return ""
@@ -241,7 +262,7 @@ func faixaCount(faixa *lookup.Faixa) string {
// faixaSpan renders the ends of the range, or "" when they are missing or are
// the same number — "de X a X" is not a range, and printing a single CEP there
// would hand the user a guess to try.
-func faixaSpan(faixa *lookup.Faixa) string {
+func faixaSpan(faixa *cepis.Faixa) string {
first, last := maskCEP(faixa.CEPIni), maskCEP(faixa.CEPFim)
if first == "" || last == "" || first == last {
return ""