initial commits

master
Charles Iliya Krempeaux 2023-11-05 00:16:31 -07:00
parent d40fa44b46
commit bb64ec8664
6 changed files with 61 additions and 79 deletions

View File

@ -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)
}

View File

@ -4,6 +4,8 @@ import (
"io" "io"
"sourcecode.social/reiver/go-erorr" "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'. // Parse tries to parse a JSON boolean literal — i.e., either 'false' or 'true'.
@ -17,7 +19,7 @@ import (
// // ... // // ...
// //
// var value rfc8259boolean.Boolean // var value rfc8259boolean.Boolean
// err := rfc8259.ParseTrue(rs, &value) // err := rfc8259.Parse(rs, &value)
// //
// if nil != err { // if nil != err {
// return err // return err
@ -26,10 +28,10 @@ import (
// fmt.Printf("value = %#v\n", value) // fmt.Printf("value = %#v\n", value)
func Parse(runescanner io.RuneScanner, dst *Boolean) error { func Parse(runescanner io.RuneScanner, dst *Boolean) error {
if nil == runescanner { if nil == runescanner {
return errNilRuneScanner return rfc8259errors.ErrNilRuneScanner
} }
if nil == dst { if nil == dst {
return errNilDestination return rfc8259errors.ErrNilDestination
} }
var r rune var r rune
@ -39,26 +41,26 @@ func Parse(runescanner io.RuneScanner, dst *Boolean) error {
r, _, err = runescanner.ReadRune() r, _, err = runescanner.ReadRune()
if nil != err { if nil != err {
if io.EOF == err { if io.EOF == err {
return errUnexpectedEndOfFile return rfc8259errors.ErrUnexpectedEndOfFile
} }
return errProblemReadingRune(err) return rfc8259errors.ErrProblemReadingRune(err)
} }
if err := runescanner.UnreadRune(); nil != err { if err := runescanner.UnreadRune(); nil != err {
return errProblemUnreadingRune(err, r) return rfc8259errors.ErrProblemUnreadingRune(err, r)
} }
} }
switch r { switch r {
case 'f': case 'f':
if err := ParseFalse(runescanner); nil != err { if err := parseFalse(runescanner); nil != err {
return err return err
} }
*dst = False() *dst = False()
return nil return nil
case 't': case 't':
if err := ParseTrue(runescanner); nil != err { if err := parseTrue(runescanner); nil != err {
return err return err
} }
*dst = True() *dst = True()

View File

@ -4,6 +4,8 @@ import (
"io" "io"
"sourcecode.social/reiver/go-erorr" "sourcecode.social/reiver/go-erorr"
"sourcecode.social/reiver/go-rfc8259/errors"
) )
func errProblemParsingBooleanFalse(expected rune, actual rune) error { func errProblemParsingBooleanFalse(expected rune, actual rune) error {
@ -16,32 +18,32 @@ func errProblemParsingBooleanFalse(expected rune, actual rune) error {
// //
// Example usage: // Example usage:
// //
// var rr io.RuneReader // var runescanner io.RuneScanner
// //
// // ... // // ...
// //
// err := rfc8259.ParseFalse(rr) // err := rfc8259.ParseFalse(runescanner)
// //
// if nil != err { // if nil != err {
// fmt.Printf("We did NOT have a 'false', but instead got the error: %s\n", err) // fmt.Printf("We did NOT have a 'false', but instead got the error: %s\n", err)
// } else { // } else {
// fmt.Print("We had a 'false'\n") // fmt.Print("We had a 'false'\n")
// } // }
func ParseFalse(rr io.RuneReader) error { func parseFalse(runescanner io.RuneScanner) error {
if nil == rr { if nil == runescanner {
return errNilRuneReader return rfc8259errors.ErrNilRuneScanner
} }
{ {
const expected rune = 'f' const expected rune = 'f'
r, _, err := rr.ReadRune() r, _, err := runescanner.ReadRune()
if nil != err { if nil != err {
if io.EOF == err { if io.EOF == err {
return errUnexpectedEndOfFile return rfc8259errors.ErrUnexpectedEndOfFile
} }
return errProblemReadingRune(err) return rfc8259errors.ErrProblemReadingRune(err)
} }
if expected != r { if expected != r {
@ -52,13 +54,13 @@ func ParseFalse(rr io.RuneReader) error {
{ {
const expected rune = 'a' const expected rune = 'a'
r, _, err := rr.ReadRune() r, _, err := runescanner.ReadRune()
if nil != err { if nil != err {
if io.EOF == err { if io.EOF == err {
return errUnexpectedEndOfFile return rfc8259errors.ErrUnexpectedEndOfFile
} }
return errProblemReadingRune(err) return rfc8259errors.ErrProblemReadingRune(err)
} }
if expected != r { if expected != r {
@ -69,13 +71,13 @@ func ParseFalse(rr io.RuneReader) error {
{ {
const expected rune = 'l' const expected rune = 'l'
r, _, err := rr.ReadRune() r, _, err := runescanner.ReadRune()
if nil != err { if nil != err {
if io.EOF == err { if io.EOF == err {
return errUnexpectedEndOfFile return rfc8259errors.ErrUnexpectedEndOfFile
} }
return errProblemReadingRune(err) return rfc8259errors.ErrProblemReadingRune(err)
} }
if expected != r { if expected != r {
@ -86,13 +88,13 @@ func ParseFalse(rr io.RuneReader) error {
{ {
const expected rune = 's' const expected rune = 's'
r, _, err := rr.ReadRune() r, _, err := runescanner.ReadRune()
if nil != err { if nil != err {
if io.EOF == err { if io.EOF == err {
return errUnexpectedEndOfFile return rfc8259errors.ErrUnexpectedEndOfFile
} }
return errProblemReadingRune(err) return rfc8259errors.ErrProblemReadingRune(err)
} }
if expected != r { if expected != r {
@ -103,13 +105,13 @@ func ParseFalse(rr io.RuneReader) error {
{ {
const expected rune = 'e' const expected rune = 'e'
r, _, err := rr.ReadRune() r, _, err := runescanner.ReadRune()
if nil != err { if nil != err {
if io.EOF == err { if io.EOF == err {
return errUnexpectedEndOfFile return rfc8259errors.ErrUnexpectedEndOfFile
} }
return errProblemReadingRune(err) return rfc8259errors.ErrProblemReadingRune(err)
} }
if expected != r { if expected != r {

View File

@ -1,4 +1,4 @@
package rfc8259boolean_test package rfc8259boolean
import ( import (
"testing" "testing"
@ -7,8 +7,6 @@ import (
"io" "io"
"sourcecode.social/reiver/go-utf8" "sourcecode.social/reiver/go-utf8"
"sourcecode.social/reiver/go-rfc8259/boolean"
) )
func TestParseFalse_success(t *testing.T) { func TestParseFalse_success(t *testing.T) {
@ -16,9 +14,9 @@ func TestParseFalse_success(t *testing.T) {
var p []byte = []byte("false") var p []byte = []byte("false")
var reader io.Reader = bytes.NewReader(p) 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 { if nil != err {
t.Errorf("Did not expect an error but actually got one.") 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 { for testNumber, test := range tests {
var reader io.Reader = bytes.NewReader(test.Value) 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 { if nil == err {
t.Errorf("Expected an error but did not actually get one.") t.Errorf("Expected an error but did not actually get one.")

View File

@ -4,6 +4,8 @@ import (
"io" "io"
"sourcecode.social/reiver/go-erorr" "sourcecode.social/reiver/go-erorr"
"sourcecode.social/reiver/go-rfc8259/errors"
) )
func errProblemParsingBooleanTrue(expected rune, actual rune) error { func errProblemParsingBooleanTrue(expected rune, actual rune) error {
@ -16,32 +18,32 @@ func errProblemParsingBooleanTrue(expected rune, actual rune) error {
// //
// Example usage: // Example usage:
// //
// var rr io.RuneReader // var runescanner io.RuneScanner
// //
// // ... // // ...
// //
// err := rfc8259.ParseTrue(rr) // err := rfc8259.ParseTrue(runescanner)
// //
// if nil != err { // if nil != err {
// fmt.Printf("We did NOT have a 'true', but instead got the error: %s\n", err) // fmt.Printf("We did NOT have a 'true', but instead got the error: %s\n", err)
// } else { // } else {
// fmt.Print("We had a 'true'\n") // fmt.Print("We had a 'true'\n")
// } // }
func ParseTrue(rr io.RuneReader) error { func parseTrue(runescanner io.RuneScanner) error {
if nil == rr { if nil == runescanner {
return errNilRuneReader return rfc8259errors.ErrNilRuneScanner
} }
{ {
const expected rune = 't' const expected rune = 't'
r, _, err := rr.ReadRune() r, _, err := runescanner.ReadRune()
if nil != err { if nil != err {
if io.EOF == err { if io.EOF == err {
return errUnexpectedEndOfFile return rfc8259errors.ErrUnexpectedEndOfFile
} }
return errProblemReadingRune(err) return rfc8259errors.ErrProblemReadingRune(err)
} }
if expected != r { if expected != r {
@ -52,13 +54,13 @@ func ParseTrue(rr io.RuneReader) error {
{ {
const expected rune = 'r' const expected rune = 'r'
r, _, err := rr.ReadRune() r, _, err := runescanner.ReadRune()
if nil != err { if nil != err {
if io.EOF == err { if io.EOF == err {
return errUnexpectedEndOfFile return rfc8259errors.ErrUnexpectedEndOfFile
} }
return errProblemReadingRune(err) return rfc8259errors.ErrProblemReadingRune(err)
} }
if expected != r { if expected != r {
@ -69,13 +71,13 @@ func ParseTrue(rr io.RuneReader) error {
{ {
const expected rune = 'u' const expected rune = 'u'
r, _, err := rr.ReadRune() r, _, err := runescanner.ReadRune()
if nil != err { if nil != err {
if io.EOF == err { if io.EOF == err {
return errUnexpectedEndOfFile return rfc8259errors.ErrUnexpectedEndOfFile
} }
return errProblemReadingRune(err) return rfc8259errors.ErrProblemReadingRune(err)
} }
if expected != r { if expected != r {
@ -86,13 +88,13 @@ func ParseTrue(rr io.RuneReader) error {
{ {
const expected rune = 'e' const expected rune = 'e'
r, _, err := rr.ReadRune() r, _, err := runescanner.ReadRune()
if nil != err { if nil != err {
if io.EOF == err { if io.EOF == err {
return errUnexpectedEndOfFile return rfc8259errors.ErrUnexpectedEndOfFile
} }
return errProblemReadingRune(err) return rfc8259errors.ErrProblemReadingRune(err)
} }
if expected != r { if expected != r {

View File

@ -1,4 +1,4 @@
package rfc8259boolean_test package rfc8259boolean
import ( import (
"testing" "testing"
@ -7,8 +7,6 @@ import (
"io" "io"
"sourcecode.social/reiver/go-utf8" "sourcecode.social/reiver/go-utf8"
"sourcecode.social/reiver/go-rfc8259/boolean"
) )
func TestParseTrue_success(t *testing.T) { func TestParseTrue_success(t *testing.T) {
@ -16,9 +14,9 @@ func TestParseTrue_success(t *testing.T) {
var p []byte = []byte("true") var p []byte = []byte("true")
var reader io.Reader = bytes.NewReader(p) 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 { if nil != err {
t.Errorf("Did not expect an error but actually got one.") 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 { for testNumber, test := range tests {
var reader io.Reader = bytes.NewReader(test.Value) 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 { if nil == err {
t.Errorf("Expected an error but did not actually get one.") t.Errorf("Expected an error but did not actually get one.")