Skip to content

Commit

Permalink
chore: refactor code
Browse files Browse the repository at this point in the history
- remove assertNotNegative
- use checkLowerBoundary
  • Loading branch information
ccoVeille committed Nov 13, 2024
1 parent 6f47578 commit a2fcd25
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
11 changes: 0 additions & 11 deletions asserters.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
package safecast

func assertNotNegative[T Type, T2 Type](i T, zero T2) error {
if i < 0 {
return Error{
err: ErrExceedMinimumValue,
value: i,
boundary: zero,
}
}
return nil
}

func checkUpperBoundary[T Type, T2 Type](value T, boundary T2) error {
if value <= 0 {
return nil
Expand Down
10 changes: 5 additions & 5 deletions conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func ToInt[T Type](i T) (int, error) {
// If the conversion results in a value outside the range of an uint,
// an [ErrConversionIssue] error is returned.
func ToUint[T Type](i T) (uint, error) {
if err := assertNotNegative(i, uint(0)); err != nil {
if err := checkLowerBoundary(i, uint(0)); err != nil {
return 0, err
}

Expand Down Expand Up @@ -56,7 +56,7 @@ func ToInt8[T Type](i T) (int8, error) {
// If the conversion results in a value outside the range of an uint8,
// an [ErrConversionIssue] error is returned.
func ToUint8[T Type](i T) (uint8, error) {
if err := assertNotNegative(i, uint8(0)); err != nil {
if err := checkLowerBoundary(i, uint8(0)); err != nil {
return 0, err
}

Expand Down Expand Up @@ -86,7 +86,7 @@ func ToInt16[T Type](i T) (int16, error) {
// If the conversion results in a value outside the range of an uint16,
// an [ErrConversionIssue] error is returned.
func ToUint16[T Type](i T) (uint16, error) {
if err := assertNotNegative(i, uint16(0)); err != nil {
if err := checkLowerBoundary(i, uint16(0)); err != nil {
return 0, err
}

Expand Down Expand Up @@ -116,7 +116,7 @@ func ToInt32[T Type](i T) (int32, error) {
// If the conversion results in a value outside the range of an uint32,
// an [ErrConversionIssue] error is returned.
func ToUint32[T Type](i T) (uint32, error) {
if err := assertNotNegative(i, uint32(0)); err != nil {
if err := checkLowerBoundary(i, uint32(0)); err != nil {
return 0, err
}

Expand All @@ -142,7 +142,7 @@ func ToInt64[T Type](i T) (int64, error) {
// If the conversion results in a value outside the range of an uint64,
// an [ErrConversionIssue] error is returned.
func ToUint64[T Type](i T) (uint64, error) {
if err := assertNotNegative(i, uint64(0)); err != nil {
if err := checkLowerBoundary(i, uint64(0)); err != nil {
return 0, err
}

Expand Down

0 comments on commit a2fcd25

Please sign in to comment.