Result is failed by default

This commit is contained in:
Brandon Dyck 2024-09-13 10:44:30 -06:00
parent 0a149acf46
commit 7c7d0fef9b
3 changed files with 21 additions and 20 deletions

View File

@ -1,4 +1,3 @@
Result should be Failed by default
Test RuneReader Test RuneReader
Test Regexp Test Regexp
Add SPDX tags Add SPDX tags

View File

@ -10,7 +10,7 @@ import (
) )
type Result[In, Out any] struct { type Result[In, Out any] struct {
consumed, failed bool consumed, succeeded bool
value Out value Out
next State[In] next State[In]
message Message message Message
@ -19,14 +19,14 @@ type Result[In, Out any] struct {
func Fail[In, Out any](consumed bool, msg Message) Result[In, Out] { func Fail[In, Out any](consumed bool, msg Message) Result[In, Out] {
return Result[In, Out]{ return Result[In, Out]{
consumed: consumed, consumed: consumed,
failed: true, succeeded: false,
message: msg, message: msg,
} }
} }
func (r Result[In, Out]) Failed() (ok, consumed bool, msg Message) { func (r Result[In, Out]) Failed() (failed, consumed bool, msg Message) {
ok = r.failed failed = !r.succeeded
if ok { if failed {
consumed = r.consumed consumed = r.consumed
msg = r.message msg = r.message
} }
@ -35,7 +35,7 @@ func (r Result[In, Out]) Failed() (ok, consumed bool, msg Message) {
func Succeed[In, Out any](consumed bool, value Out, next State[In], msg Message) Result[In, Out] { func Succeed[In, Out any](consumed bool, value Out, next State[In], msg Message) Result[In, Out] {
return Result[In, Out]{ return Result[In, Out]{
failed: false, succeeded: true,
value: value, value: value,
consumed: consumed, consumed: consumed,
next: next, next: next,
@ -43,9 +43,9 @@ func Succeed[In, Out any](consumed bool, value Out, next State[In], msg Message)
} }
} }
func (r Result[In, Out]) Succeeded() (ok, consumed bool, value Out, next State[In], msg Message) { func (r Result[In, Out]) Succeeded() (succeeded, consumed bool, value Out, next State[In], msg Message) {
ok = !r.failed succeeded = r.succeeded
if ok { if succeeded {
consumed = r.consumed consumed = r.consumed
value = r.value value = r.value
next = r.next next = r.next

View File

@ -56,8 +56,10 @@ func TestSlice(t *testing.T) {
r := ptest.ErrReaderAt(expectedErr) r := ptest.ErrReaderAt(expectedErr)
c := cursor.NewReaderAt(r) c := cursor.NewReaderAt(r)
s := rapid.SliceOfN(rapid.Byte(), 0, 100).Draw(t, "s") s := rapid.SliceOfN(rapid.Byte(), 0, 100).Draw(t, "s")
_, err := gigaparsec.Slice(s)(gigaparsec.MakeState(c)) result, err := gigaparsec.Slice(s)(gigaparsec.MakeState(c))
failed, _, _ := result.Failed()
test.ErrorIs(t, err, expectedErr) test.ErrorIs(t, err, expectedErr)
test.True(t, failed)
})) }))
t.Run("succeeds when contents match", rapid.MakeCheck(func(t *rapid.T) { t.Run("succeeds when contents match", rapid.MakeCheck(func(t *rapid.T) {
input := rapid.SliceOfN(rapid.Byte(), 1, -1).Draw(t, "input") input := rapid.SliceOfN(rapid.Byte(), 1, -1).Draw(t, "input")