From bb64ec86646fd622a1adac54e36ce6c3ab435555 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Sun, 5 Nov 2023 00:16:31 -0700 Subject: [PATCH] initial commits --- boolean/errors.go | 20 ------------------ boolean/parse.go | 18 ++++++++-------- boolean/parsefalse.go | 42 ++++++++++++++++++++------------------ boolean/parsefalse_test.go | 12 +++++------ boolean/parsetrue.go | 36 +++++++++++++++++--------------- boolean/parsetrue_test.go | 12 +++++------ 6 files changed, 61 insertions(+), 79 deletions(-) delete mode 100644 boolean/errors.go diff --git a/boolean/errors.go b/boolean/errors.go deleted file mode 100644 index a91f696..0000000 --- a/boolean/errors.go +++ /dev/null @@ -1,20 +0,0 @@ -package rfc8259boolean - -import ( - "sourcecode.social/reiver/go-erorr" -) - -const ( - errNilDestination = erorr.Error("rfc8259: nil destination") - errNilRuneReader = erorr.Error("rfc8259: nil rune-reader") - errNilRuneScanner = erorr.Error("rfc8259: nil rune-scanner") - errUnexpectedEndOfFile = erorr.Error("rfc8259: unexpected end-of-file") -) - -func errProblemReadingRune(err error) error { - return erorr.Errorf("rfc8259: problem with JSON parser — problem reading rune: %w", err) -} - -func errProblemUnreadingRune(err error, r rune) error { - return erorr.Errorf("rfc8259: internal-error with JSON parser — problem unreading rune %q (%U): %w", r, r, err) -} diff --git a/boolean/parse.go b/boolean/parse.go index e67902e..b25a766 100644 --- a/boolean/parse.go +++ b/boolean/parse.go @@ -4,6 +4,8 @@ import ( "io" "sourcecode.social/reiver/go-erorr" + + "sourcecode.social/reiver/go-rfc8259/errors" ) // Parse tries to parse a JSON boolean literal — i.e., either 'false' or 'true'. @@ -17,7 +19,7 @@ import ( // // ... // // var value rfc8259boolean.Boolean -// err := rfc8259.ParseTrue(rs, &value) +// err := rfc8259.Parse(rs, &value) // // if nil != err { // return err @@ -26,10 +28,10 @@ import ( // fmt.Printf("value = %#v\n", value) func Parse(runescanner io.RuneScanner, dst *Boolean) error { if nil == runescanner { - return errNilRuneScanner + return rfc8259errors.ErrNilRuneScanner } if nil == dst { - return errNilDestination + return rfc8259errors.ErrNilDestination } var r rune @@ -39,26 +41,26 @@ func Parse(runescanner io.RuneScanner, dst *Boolean) error { r, _, err = runescanner.ReadRune() if nil != err { if io.EOF == err { - return errUnexpectedEndOfFile + return rfc8259errors.ErrUnexpectedEndOfFile } - return errProblemReadingRune(err) + return rfc8259errors.ErrProblemReadingRune(err) } if err := runescanner.UnreadRune(); nil != err { - return errProblemUnreadingRune(err, r) + return rfc8259errors.ErrProblemUnreadingRune(err, r) } } switch r { case 'f': - if err := ParseFalse(runescanner); nil != err { + if err := parseFalse(runescanner); nil != err { return err } *dst = False() return nil case 't': - if err := ParseTrue(runescanner); nil != err { + if err := parseTrue(runescanner); nil != err { return err } *dst = True() diff --git a/boolean/parsefalse.go b/boolean/parsefalse.go index 3415fd6..a025050 100644 --- a/boolean/parsefalse.go +++ b/boolean/parsefalse.go @@ -4,6 +4,8 @@ import ( "io" "sourcecode.social/reiver/go-erorr" + + "sourcecode.social/reiver/go-rfc8259/errors" ) func errProblemParsingBooleanFalse(expected rune, actual rune) error { @@ -16,32 +18,32 @@ func errProblemParsingBooleanFalse(expected rune, actual rune) error { // // Example usage: // -// var rr io.RuneReader +// var runescanner io.RuneScanner // // // ... // -// err := rfc8259.ParseFalse(rr) +// err := rfc8259.ParseFalse(runescanner) // // if nil != err { // fmt.Printf("We did NOT have a 'false', but instead got the error: %s\n", err) // } else { // fmt.Print("We had a 'false'\n") // } -func ParseFalse(rr io.RuneReader) error { - if nil == rr { - return errNilRuneReader +func parseFalse(runescanner io.RuneScanner) error { + if nil == runescanner { + return rfc8259errors.ErrNilRuneScanner } { const expected rune = 'f' - r, _, err := rr.ReadRune() + r, _, err := runescanner.ReadRune() if nil != err { if io.EOF == err { - return errUnexpectedEndOfFile + return rfc8259errors.ErrUnexpectedEndOfFile } - return errProblemReadingRune(err) + return rfc8259errors.ErrProblemReadingRune(err) } if expected != r { @@ -52,13 +54,13 @@ func ParseFalse(rr io.RuneReader) error { { const expected rune = 'a' - r, _, err := rr.ReadRune() + r, _, err := runescanner.ReadRune() if nil != err { if io.EOF == err { - return errUnexpectedEndOfFile + return rfc8259errors.ErrUnexpectedEndOfFile } - return errProblemReadingRune(err) + return rfc8259errors.ErrProblemReadingRune(err) } if expected != r { @@ -69,13 +71,13 @@ func ParseFalse(rr io.RuneReader) error { { const expected rune = 'l' - r, _, err := rr.ReadRune() + r, _, err := runescanner.ReadRune() if nil != err { if io.EOF == err { - return errUnexpectedEndOfFile + return rfc8259errors.ErrUnexpectedEndOfFile } - return errProblemReadingRune(err) + return rfc8259errors.ErrProblemReadingRune(err) } if expected != r { @@ -86,13 +88,13 @@ func ParseFalse(rr io.RuneReader) error { { const expected rune = 's' - r, _, err := rr.ReadRune() + r, _, err := runescanner.ReadRune() if nil != err { if io.EOF == err { - return errUnexpectedEndOfFile + return rfc8259errors.ErrUnexpectedEndOfFile } - return errProblemReadingRune(err) + return rfc8259errors.ErrProblemReadingRune(err) } if expected != r { @@ -103,13 +105,13 @@ func ParseFalse(rr io.RuneReader) error { { const expected rune = 'e' - r, _, err := rr.ReadRune() + r, _, err := runescanner.ReadRune() if nil != err { if io.EOF == err { - return errUnexpectedEndOfFile + return rfc8259errors.ErrUnexpectedEndOfFile } - return errProblemReadingRune(err) + return rfc8259errors.ErrProblemReadingRune(err) } if expected != r { diff --git a/boolean/parsefalse_test.go b/boolean/parsefalse_test.go index fe5e8e3..4cb0a63 100644 --- a/boolean/parsefalse_test.go +++ b/boolean/parsefalse_test.go @@ -1,4 +1,4 @@ -package rfc8259boolean_test +package rfc8259boolean import ( "testing" @@ -7,8 +7,6 @@ import ( "io" "sourcecode.social/reiver/go-utf8" - - "sourcecode.social/reiver/go-rfc8259/boolean" ) func TestParseFalse_success(t *testing.T) { @@ -16,9 +14,9 @@ func TestParseFalse_success(t *testing.T) { var p []byte = []byte("false") var reader io.Reader = bytes.NewReader(p) - var runereader io.RuneReader = utf8.NewRuneReader(reader) + var runescanner io.RuneScanner = utf8.NewRuneScanner(reader) - err := rfc8259boolean.ParseFalse(runereader) + err := parseFalse(runescanner) if nil != err { t.Errorf("Did not expect an error but actually got one.") @@ -223,9 +221,9 @@ func TestParseFalse_failure(t *testing.T) { for testNumber, test := range tests { var reader io.Reader = bytes.NewReader(test.Value) - var runereader io.RuneReader = utf8.NewRuneReader(reader) + var runescanner io.RuneScanner = utf8.NewRuneScanner(reader) - err := rfc8259boolean.ParseFalse(runereader) + err := parseFalse(runescanner) if nil == err { t.Errorf("Expected an error but did not actually get one.") diff --git a/boolean/parsetrue.go b/boolean/parsetrue.go index 0625ffa..9ed904f 100644 --- a/boolean/parsetrue.go +++ b/boolean/parsetrue.go @@ -4,6 +4,8 @@ import ( "io" "sourcecode.social/reiver/go-erorr" + + "sourcecode.social/reiver/go-rfc8259/errors" ) func errProblemParsingBooleanTrue(expected rune, actual rune) error { @@ -16,32 +18,32 @@ func errProblemParsingBooleanTrue(expected rune, actual rune) error { // // Example usage: // -// var rr io.RuneReader +// var runescanner io.RuneScanner // // // ... // -// err := rfc8259.ParseTrue(rr) +// err := rfc8259.ParseTrue(runescanner) // // if nil != err { // fmt.Printf("We did NOT have a 'true', but instead got the error: %s\n", err) // } else { // fmt.Print("We had a 'true'\n") // } -func ParseTrue(rr io.RuneReader) error { - if nil == rr { - return errNilRuneReader +func parseTrue(runescanner io.RuneScanner) error { + if nil == runescanner { + return rfc8259errors.ErrNilRuneScanner } { const expected rune = 't' - r, _, err := rr.ReadRune() + r, _, err := runescanner.ReadRune() if nil != err { if io.EOF == err { - return errUnexpectedEndOfFile + return rfc8259errors.ErrUnexpectedEndOfFile } - return errProblemReadingRune(err) + return rfc8259errors.ErrProblemReadingRune(err) } if expected != r { @@ -52,13 +54,13 @@ func ParseTrue(rr io.RuneReader) error { { const expected rune = 'r' - r, _, err := rr.ReadRune() + r, _, err := runescanner.ReadRune() if nil != err { if io.EOF == err { - return errUnexpectedEndOfFile + return rfc8259errors.ErrUnexpectedEndOfFile } - return errProblemReadingRune(err) + return rfc8259errors.ErrProblemReadingRune(err) } if expected != r { @@ -69,13 +71,13 @@ func ParseTrue(rr io.RuneReader) error { { const expected rune = 'u' - r, _, err := rr.ReadRune() + r, _, err := runescanner.ReadRune() if nil != err { if io.EOF == err { - return errUnexpectedEndOfFile + return rfc8259errors.ErrUnexpectedEndOfFile } - return errProblemReadingRune(err) + return rfc8259errors.ErrProblemReadingRune(err) } if expected != r { @@ -86,13 +88,13 @@ func ParseTrue(rr io.RuneReader) error { { const expected rune = 'e' - r, _, err := rr.ReadRune() + r, _, err := runescanner.ReadRune() if nil != err { if io.EOF == err { - return errUnexpectedEndOfFile + return rfc8259errors.ErrUnexpectedEndOfFile } - return errProblemReadingRune(err) + return rfc8259errors.ErrProblemReadingRune(err) } if expected != r { diff --git a/boolean/parsetrue_test.go b/boolean/parsetrue_test.go index 4d14956..57bacbb 100644 --- a/boolean/parsetrue_test.go +++ b/boolean/parsetrue_test.go @@ -1,4 +1,4 @@ -package rfc8259boolean_test +package rfc8259boolean import ( "testing" @@ -7,8 +7,6 @@ import ( "io" "sourcecode.social/reiver/go-utf8" - - "sourcecode.social/reiver/go-rfc8259/boolean" ) func TestParseTrue_success(t *testing.T) { @@ -16,9 +14,9 @@ func TestParseTrue_success(t *testing.T) { var p []byte = []byte("true") var reader io.Reader = bytes.NewReader(p) - var runereader io.RuneReader = utf8.NewRuneReader(reader) + var runescanner io.RuneScanner = utf8.NewRuneScanner(reader) - err := rfc8259boolean.ParseTrue(runereader) + err := parseTrue(runescanner) if nil != err { t.Errorf("Did not expect an error but actually got one.") @@ -215,9 +213,9 @@ func TestParseTrue_failure(t *testing.T) { for testNumber, test := range tests { var reader io.Reader = bytes.NewReader(test.Value) - var runereader io.RuneReader = utf8.NewRuneReader(reader) + var runescanner io.RuneScanner = utf8.NewRuneScanner(reader) - err := rfc8259boolean.ParseTrue(runereader) + err := parseTrue(runescanner) if nil == err { t.Errorf("Expected an error but did not actually get one.")