Skip to content

Commit e5e06d4

Browse files
committed
cm: replace custom error type with two custom error types
This is a workaround for tinygo-org/tinygo#4810.
1 parent 68d072d commit e5e06d4

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

cm/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
44

5+
## [Unreleased]
6+
7+
### Fixed
8+
9+
- Updated error handling to address a [build issue](https://github.com/tinygo-org/tinygo/issues/4810) in TinyGo.
10+
511
## [v0.2.1] — 2025-03-16
612

713
### Fixed

cm/case.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ func CaseUnmarshaler[T ~uint8 | ~uint16 | ~uint32](cases []string) func(v *T, te
99
if len(cases) <= linearScanThreshold {
1010
return func(v *T, text []byte) error {
1111
if len(text) == 0 {
12-
return errEmpty
12+
return &emptyTextError{}
1313
}
1414
s := string(text)
1515
for i := 0; i < len(cases); i++ {
@@ -18,7 +18,7 @@ func CaseUnmarshaler[T ~uint8 | ~uint16 | ~uint32](cases []string) func(v *T, te
1818
return nil
1919
}
2020
}
21-
return errNoMatchingCase
21+
return &noMatchingCaseError{}
2222
}
2323
}
2424

@@ -29,11 +29,11 @@ func CaseUnmarshaler[T ~uint8 | ~uint16 | ~uint32](cases []string) func(v *T, te
2929

3030
return func(v *T, text []byte) error {
3131
if len(text) == 0 {
32-
return errEmpty
32+
return &emptyTextError{}
3333
}
3434
c, ok := m[string(text)]
3535
if !ok {
36-
return errNoMatchingCase
36+
return &noMatchingCaseError{}
3737
}
3838
*v = c
3939
return nil
@@ -42,15 +42,10 @@ func CaseUnmarshaler[T ~uint8 | ~uint16 | ~uint32](cases []string) func(v *T, te
4242

4343
const linearScanThreshold = 16
4444

45-
var (
46-
errEmpty = &stringError{"empty text"}
47-
errNoMatchingCase = &stringError{"no matching case"}
48-
)
45+
type emptyTextError struct{}
4946

50-
type stringError struct {
51-
err string
52-
}
47+
func (*emptyTextError) Error() string { return "empty text" }
5348

54-
func (err *stringError) Error() string {
55-
return err.err
56-
}
49+
type noMatchingCaseError struct{}
50+
51+
func (*noMatchingCaseError) Error() string { return "no matching case" }

0 commit comments

Comments
 (0)