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 Regexp
Add SPDX tags

View File

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

View File

@ -56,8 +56,10 @@ func TestSlice(t *testing.T) {
r := ptest.ErrReaderAt(expectedErr)
c := cursor.NewReaderAt(r)
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.True(t, failed)
}))
t.Run("succeeds when contents match", rapid.MakeCheck(func(t *rapid.T) {
input := rapid.SliceOfN(rapid.Byte(), 1, -1).Draw(t, "input")