fork of indigo with slightly nicer lexgen

switch from fmt.Errorf to errors.New for static strings

+4 -4
atproto/syntax/atidentifier.go
··· 1 1 package syntax 2 2 3 3 import ( 4 - "fmt" 4 + "errors" 5 5 "strings" 6 6 ) 7 7 ··· 11 11 12 12 func ParseAtIdentifier(raw string) (*AtIdentifier, error) { 13 13 if raw == "" { 14 - return nil, fmt.Errorf("expected AT account identifier, got empty string") 14 + return nil, errors.New("expected AT account identifier, got empty string") 15 15 } 16 16 if strings.HasPrefix(raw, "did:") { 17 17 did, err := ParseDID(raw) ··· 37 37 if ok { 38 38 return handle, nil 39 39 } 40 - return "", fmt.Errorf("AT Identifier is not a Handle") 40 + return "", errors.New("AT Identifier is not a Handle") 41 41 } 42 42 43 43 func (n AtIdentifier) IsDID() bool { ··· 50 50 if ok { 51 51 return did, nil 52 52 } 53 - return "", fmt.Errorf("AT Identifier is not a DID") 53 + return "", errors.New("AT Identifier is not a DID") 54 54 } 55 55 56 56 func (n AtIdentifier) Normalize() AtIdentifier {
+3 -2
atproto/syntax/aturi.go
··· 1 1 package syntax 2 2 3 3 import ( 4 + "errors" 4 5 "fmt" 5 6 "regexp" 6 7 "strings" ··· 17 18 18 19 func ParseATURI(raw string) (ATURI, error) { 19 20 if len(raw) > 8192 { 20 - return "", fmt.Errorf("ATURI is too long (8192 chars max)") 21 + return "", errors.New("ATURI is too long (8192 chars max)") 21 22 } 22 23 parts := aturiRegex.FindStringSubmatch(raw) 23 24 if parts == nil || len(parts) < 2 || parts[0] == "" { 24 - return "", fmt.Errorf("AT-URI syntax didn't validate via regex") 25 + return "", errors.New("AT-URI syntax didn't validate via regex") 25 26 } 26 27 // verify authority as either a DID or NSID 27 28 _, err := ParseAtIdentifier(parts[1])
+6 -6
atproto/syntax/cid.go
··· 1 1 package syntax 2 2 3 3 import ( 4 - "fmt" 4 + "errors" 5 5 "regexp" 6 6 "strings" 7 7 ) ··· 17 17 18 18 func ParseCID(raw string) (CID, error) { 19 19 if raw == "" { 20 - return "", fmt.Errorf("expected CID, got empty string") 20 + return "", errors.New("expected CID, got empty string") 21 21 } 22 22 if len(raw) > 256 { 23 - return "", fmt.Errorf("CID is too long (256 chars max)") 23 + return "", errors.New("CID is too long (256 chars max)") 24 24 } 25 25 if len(raw) < 8 { 26 - return "", fmt.Errorf("CID is too short (8 chars min)") 26 + return "", errors.New("CID is too short (8 chars min)") 27 27 } 28 28 29 29 if !cidRegex.MatchString(raw) { 30 - return "", fmt.Errorf("CID syntax didn't validate via regex") 30 + return "", errors.New("CID syntax didn't validate via regex") 31 31 } 32 32 if strings.HasPrefix(raw, "Qmb") { 33 - return "", fmt.Errorf("CIDv0 not allowed in this version of atproto") 33 + return "", errors.New("CIDv0 not allowed in this version of atproto") 34 34 } 35 35 return CID(raw), nil 36 36 }
+5 -4
atproto/syntax/datetime.go
··· 1 1 package syntax 2 2 3 3 import ( 4 + "errors" 4 5 "fmt" 5 6 "regexp" 6 7 "strings" ··· 25 26 26 27 func ParseDatetime(raw string) (Datetime, error) { 27 28 if raw == "" { 28 - return "", fmt.Errorf("expected datetime, got empty string") 29 + return "", errors.New("expected datetime, got empty string") 29 30 } 30 31 if len(raw) > 64 { 31 - return "", fmt.Errorf("Datetime too long (max 64 chars)") 32 + return "", errors.New("Datetime too long (max 64 chars)") 32 33 } 33 34 34 35 if !datetimeRegex.MatchString(raw) { 35 - return "", fmt.Errorf("Datetime syntax didn't validate via regex") 36 + return "", errors.New("Datetime syntax didn't validate via regex") 36 37 } 37 38 if strings.HasSuffix(raw, "-00:00") { 38 - return "", fmt.Errorf("Datetime can't use '-00:00' for UTC timezone, must use '+00:00', per ISO-8601") 39 + return "", errors.New("Datetime can't use '-00:00' for UTC timezone, must use '+00:00', per ISO-8601") 39 40 } 40 41 // ensure that the datetime actually parses using golang time lib 41 42 _, err := time.Parse(time.RFC3339Nano, raw)
+4 -4
atproto/syntax/did.go
··· 1 1 package syntax 2 2 3 3 import ( 4 - "fmt" 4 + "errors" 5 5 "regexp" 6 6 "strings" 7 7 ) ··· 17 17 18 18 func ParseDID(raw string) (DID, error) { 19 19 if raw == "" { 20 - return "", fmt.Errorf("expected DID, got empty string") 20 + return "", errors.New("expected DID, got empty string") 21 21 } 22 22 if len(raw) > 2*1024 { 23 - return "", fmt.Errorf("DID is too long (2048 chars max)") 23 + return "", errors.New("DID is too long (2048 chars max)") 24 24 } 25 25 if !didRegex.MatchString(raw) { 26 - return "", fmt.Errorf("DID syntax didn't validate via regex") 26 + return "", errors.New("DID syntax didn't validate via regex") 27 27 } 28 28 return DID(raw), nil 29 29 }
+3 -2
atproto/syntax/handle.go
··· 1 1 package syntax 2 2 3 3 import ( 4 + "errors" 4 5 "fmt" 5 6 "regexp" 6 7 "strings" ··· 22 23 23 24 func ParseHandle(raw string) (Handle, error) { 24 25 if raw == "" { 25 - return "", fmt.Errorf("expected handle, got empty string") 26 + return "", errors.New("expected handle, got empty string") 26 27 } 27 28 if len(raw) > 253 { 28 - return "", fmt.Errorf("handle is too long (253 chars max)") 29 + return "", errors.New("handle is too long (253 chars max)") 29 30 } 30 31 if !handleRegex.MatchString(raw) { 31 32 return "", fmt.Errorf("handle syntax didn't validate via regex: %s", raw)
+4 -4
atproto/syntax/language.go
··· 1 1 package syntax 2 2 3 3 import ( 4 - "fmt" 4 + "errors" 5 5 "regexp" 6 6 ) 7 7 ··· 16 16 17 17 func ParseLanguage(raw string) (Language, error) { 18 18 if raw == "" { 19 - return "", fmt.Errorf("expected language code, got empty string") 19 + return "", errors.New("expected language code, got empty string") 20 20 } 21 21 if len(raw) > 128 { 22 - return "", fmt.Errorf("Language is too long (128 chars max)") 22 + return "", errors.New("Language is too long (128 chars max)") 23 23 } 24 24 if !langRegex.MatchString(raw) { 25 - return "", fmt.Errorf("Language syntax didn't validate via regex") 25 + return "", errors.New("Language syntax didn't validate via regex") 26 26 } 27 27 return Language(raw), nil 28 28 }
+4 -4
atproto/syntax/nsid.go
··· 1 1 package syntax 2 2 3 3 import ( 4 - "fmt" 4 + "errors" 5 5 "regexp" 6 6 "strings" 7 7 ) ··· 17 17 18 18 func ParseNSID(raw string) (NSID, error) { 19 19 if raw == "" { 20 - return "", fmt.Errorf("expected NSID, got empty string") 20 + return "", errors.New("expected NSID, got empty string") 21 21 } 22 22 if len(raw) > 317 { 23 - return "", fmt.Errorf("NSID is too long (317 chars max)") 23 + return "", errors.New("NSID is too long (317 chars max)") 24 24 } 25 25 if !nsidRegex.MatchString(raw) { 26 - return "", fmt.Errorf("NSID syntax didn't validate via regex") 26 + return "", errors.New("NSID syntax didn't validate via regex") 27 27 } 28 28 return NSID(raw), nil 29 29 }
+5 -5
atproto/syntax/recordkey.go
··· 1 1 package syntax 2 2 3 3 import ( 4 - "fmt" 4 + "errors" 5 5 "regexp" 6 6 ) 7 7 ··· 16 16 17 17 func ParseRecordKey(raw string) (RecordKey, error) { 18 18 if raw == "" { 19 - return "", fmt.Errorf("expected record key, got empty string") 19 + return "", errors.New("expected record key, got empty string") 20 20 } 21 21 if len(raw) > 512 { 22 - return "", fmt.Errorf("recordkey is too long (512 chars max)") 22 + return "", errors.New("recordkey is too long (512 chars max)") 23 23 } 24 24 if raw == "" || raw == "." || raw == ".." { 25 - return "", fmt.Errorf("recordkey can not be empty, '.', or '..'") 25 + return "", errors.New("recordkey can not be empty, '.', or '..'") 26 26 } 27 27 if !recordKeyRegex.MatchString(raw) { 28 - return "", fmt.Errorf("recordkey syntax didn't validate via regex") 28 + return "", errors.New("recordkey syntax didn't validate via regex") 29 29 } 30 30 return RecordKey(raw), nil 31 31 }
+4 -4
atproto/syntax/tid.go
··· 2 2 3 3 import ( 4 4 "encoding/base32" 5 - "fmt" 5 + "errors" 6 6 "regexp" 7 7 "strings" 8 8 "sync" ··· 28 28 29 29 func ParseTID(raw string) (TID, error) { 30 30 if raw == "" { 31 - return "", fmt.Errorf("expected TID, got empty string") 31 + return "", errors.New("expected TID, got empty string") 32 32 } 33 33 if len(raw) != 13 { 34 - return "", fmt.Errorf("TID is wrong length (expected 13 chars)") 34 + return "", errors.New("TID is wrong length (expected 13 chars)") 35 35 } 36 36 if !tidRegex.MatchString(raw) { 37 - return "", fmt.Errorf("TID syntax didn't validate via regex") 37 + return "", errors.New("TID syntax didn't validate via regex") 38 38 } 39 39 return TID(raw), nil 40 40 }
+4 -4
atproto/syntax/uri.go
··· 1 1 package syntax 2 2 3 3 import ( 4 - "fmt" 4 + "errors" 5 5 "regexp" 6 6 ) 7 7 ··· 14 14 15 15 func ParseURI(raw string) (URI, error) { 16 16 if raw == "" { 17 - return "", fmt.Errorf("expected URI, got empty string") 17 + return "", errors.New("expected URI, got empty string") 18 18 } 19 19 if len(raw) > 8192 { 20 - return "", fmt.Errorf("URI is too long (8192 chars max)") 20 + return "", errors.New("URI is too long (8192 chars max)") 21 21 } 22 22 var uriRegex = regexp.MustCompile(`^[a-z][a-z.-]{0,80}:[[:graph:]]+$`) 23 23 if !uriRegex.MatchString(raw) { 24 - return "", fmt.Errorf("URI syntax didn't validate via regex") 24 + return "", errors.New("URI syntax didn't validate via regex") 25 25 } 26 26 return URI(raw), nil 27 27 }