Skip to content

Commit

Permalink
added constraints.Bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Code-Hex committed Sep 25, 2023
1 parent e7f7aec commit dc54a17
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 6 deletions.
6 changes: 6 additions & 0 deletions internal/constraints/constraints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package constraints

// Bytes is an interface that represents a byte slice.
type Bytes interface {
[]byte | ~string
}
4 changes: 3 additions & 1 deletion iso8601/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"math"
"strconv"
"time"

"github.com/Code-Hex/synchro/internal/constraints"
)

// NOTE(codehex): "math.MaxInt == 9223372036854775807" has 19 digits.
Expand Down Expand Up @@ -42,7 +44,7 @@ func parseNumber(b []byte, start, width int) (v int) {
// 2012Q485 2012-Q4-85 Quarter date
//
// The function returns an implementation of DateLike or an error if the parsing fails.
func ParseDate[bytes []byte | ~string](b bytes) (DateLike, error) {
func ParseDate[bytes constraints.Bytes](b bytes) (DateLike, error) {
n, d, err := parseDate([]byte(b))
if err != nil {
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion iso8601/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"strings"
"time"

"github.com/Code-Hex/synchro/internal/constraints"
)

var defaultParseDateTimeOptions = parseDateTimeOptions{
Expand Down Expand Up @@ -65,7 +67,7 @@ func WithInLocation(loc *time.Location) ParseDateTimeOptions {
// being in a fabricated location with time fixed at the given zone offset.
//
// If parsing fails, an error is returned.
func ParseDateTime[bytes []byte | ~string](b bytes, opts ...ParseDateTimeOptions) (time.Time, error) {
func ParseDateTime[bytes constraints.Bytes](b bytes, opts ...ParseDateTimeOptions) (time.Time, error) {
return parseDateTime([]byte(b), opts...)
}

Expand Down
4 changes: 3 additions & 1 deletion iso8601/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strconv"
"strings"
"time"

"github.com/Code-Hex/synchro/internal/constraints"
)

const (
Expand Down Expand Up @@ -33,7 +35,7 @@ const (
// or -P1M for interoperability, be aware that other programs may not recognize it.
//
// The function returns a Duration structure or an error if the parsing fails.
func ParseDuration[bytes []byte | ~string](b bytes) (Duration, error) {
func ParseDuration[bytes constraints.Bytes](b bytes) (Duration, error) {
if len(b) == len(durationBasicFormat) || len(b) == len(durationExtendedFormat) {
d, err := parseAlternativeDuration([]byte(b))
if err == nil {
Expand Down
4 changes: 3 additions & 1 deletion iso8601/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"fmt"
"time"

"github.com/Code-Hex/synchro/internal/constraints"
)

// Interval represents an ISO8601 time interval.
Expand Down Expand Up @@ -75,7 +77,7 @@ func (i Interval) Contains(t time.Time) bool {

// ParseInterval parses an ISO8601 time interval from a byte slice or string.
// It returns the parsed Interval and any error encountered.
func ParseInterval[bytes []byte | ~string](b bytes) (Interval, error) {
func ParseInterval[bytes constraints.Bytes](b bytes) (Interval, error) {
return parseInterval([]byte(b))
}

Expand Down
4 changes: 3 additions & 1 deletion iso8601/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package iso8601
import (
"fmt"
"math"

"github.com/Code-Hex/synchro/internal/constraints"
)

// Time represents an ISO8601-compliant time without a date, specified by its hour, minute, second, and nanosecond.
Expand Down Expand Up @@ -90,7 +92,7 @@ func (e *TimeRangeError) Error() string {
// 123045,123456789 12:30:45,123456789
//
// The function returns a Time structure or an error if the parsing fails.
func ParseTime[bytes []byte | ~string](b bytes) (Time, error) {
func ParseTime[bytes constraints.Bytes](b bytes) (Time, error) {
n, t, err := parseTime([]byte(b))
if err != nil {
return Time{}, err
Expand Down
4 changes: 3 additions & 1 deletion iso8601/zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package iso8601

import (
"fmt"

"github.com/Code-Hex/synchro/internal/constraints"
)

// Zone represents an ISO8601-compliant timezone offset, specified by its hour, minute, and second components.
Expand Down Expand Up @@ -84,7 +86,7 @@ func (e *TimeZoneRangeError) Error() string {
// ±hhmmss ±hh:mm:ss
//
// The function returns a Zone structure or an error if the parsing fails.
func ParseZone[bytes []byte | ~string](b bytes) (Zone, error) {
func ParseZone[bytes constraints.Bytes](b bytes) (Zone, error) {
if len(b) > 3 && b[3] == ':' {
return parseExtendedZone([]byte(b))
}
Expand Down

0 comments on commit dc54a17

Please sign in to comment.