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,23 +10,23 @@ import (
)
type Result[In, Out any] struct {
consumed, failed bool
value Out
next State[In]
message Message
consumed, succeeded bool
value Out
next State[In]
message Message
}
func Fail[In, Out any](consumed bool, msg Message) Result[In, Out] {
return Result[In, Out]{
consumed: consumed,
failed: true,
message: msg,
consumed: consumed,
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,17 +35,17 @@ 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,
value: value,
consumed: consumed,
next: next,
message: msg,
succeeded: true,
value: value,
consumed: consumed,
next: next,
message: msg,
}
}
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")