diff options
Diffstat (limited to 'internal/bot/format.go')
| -rw-r--r-- | internal/bot/format.go | 33 |
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 "" |