|
| 1 | +package isoduration |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | +) |
| 8 | + |
| 9 | +// is checks type matching |
| 10 | +var is = func(err, target error) bool { |
| 11 | + if err == nil { |
| 12 | + return false |
| 13 | + } |
| 14 | + |
| 15 | + if reflect.TypeOf(err) != reflect.TypeOf(target) { |
| 16 | + return false |
| 17 | + } |
| 18 | + |
| 19 | + return reflect.DeepEqual(err, target) |
| 20 | +} |
| 21 | + |
| 22 | +// IsNotIsoFormatError occurs when parsing a string in ISO 8601 duration format, the error cannot be clearly identified |
| 23 | +var IsNotIsoFormatError = errors.New("incorrect ISO 8601 duration format") |
| 24 | + |
| 25 | +// TimeIsEmptyError occurs when a time designator is found in a line, but its value is not found |
| 26 | +// For example: P10YT |
| 27 | +var TimeIsEmptyError = errors.New("incorrect ISO 8601 T duration format, designator T found, but value is empty") |
| 28 | + |
| 29 | +// PeriodIsEmptyError occurs when a period designator is found in a line, but its value is not found |
| 30 | +// For example: P |
| 31 | +var PeriodIsEmptyError = errors.New("incorrect ISO 8601 P duration format, designator P found, but value is empty") |
| 32 | + |
| 33 | +// IncorrectIsoFormatError occurs when a token is found in a string that cannot be converted to the float64 type |
| 34 | +// For example: P10,5Y |
| 35 | +type IncorrectIsoFormatError struct { |
| 36 | + text string |
| 37 | + in string |
| 38 | +} |
| 39 | + |
| 40 | +// Error defines error output |
| 41 | +func (i *IncorrectIsoFormatError) Error() string { |
| 42 | + return fmt.Sprintf(i.text, i.in) |
| 43 | +} |
| 44 | + |
| 45 | +// Is checks for object matching |
| 46 | +func (i *IncorrectIsoFormatError) Is(err error) bool { |
| 47 | + return is(i, err) |
| 48 | +} |
| 49 | + |
| 50 | +// NewIncorrectIsoFormatError creates new IncorrectIsoFormatError |
| 51 | +func NewIncorrectIsoFormatError(in string) *IncorrectIsoFormatError { |
| 52 | + return &IncorrectIsoFormatError{"incorrect ISO 8601 duration format, invalid tokens %s", in} |
| 53 | +} |
| 54 | + |
| 55 | +// IncorrectDesignatorError occurs when an unknown designator is encountered in the line. |
| 56 | +// For example: PT10Y or P1V |
| 57 | +type IncorrectDesignatorError struct { |
| 58 | + text string |
| 59 | + designator rune |
| 60 | + state rune |
| 61 | +} |
| 62 | + |
| 63 | +// Error defines error output |
| 64 | +func (i *IncorrectDesignatorError) Error() string { |
| 65 | + return fmt.Sprintf(i.text, i.state, i.designator) |
| 66 | +} |
| 67 | + |
| 68 | +// Is checks for object matching |
| 69 | +func (i *IncorrectDesignatorError) Is(err error) bool { |
| 70 | + return is(i, err) |
| 71 | +} |
| 72 | + |
| 73 | +// NewIncorrectDesignatorError creates new IncorrectDesignatorError |
| 74 | +func NewIncorrectDesignatorError(state, designator rune) *IncorrectDesignatorError { |
| 75 | + return &IncorrectDesignatorError{"incorrect ISO 8601 duration %c format, invalid designator %c", designator, state} |
| 76 | +} |
| 77 | + |
| 78 | +// DesignatorNotFoundError occurs when there is no designator in the line after the token |
| 79 | +// For example P10T10H. |
| 80 | +type DesignatorNotFoundError struct { |
| 81 | + text string |
| 82 | + state rune |
| 83 | + after string |
| 84 | +} |
| 85 | + |
| 86 | +// Error defines error output |
| 87 | +func (i *DesignatorNotFoundError) Error() string { |
| 88 | + return fmt.Sprintf(i.text, i.state, i.after) |
| 89 | +} |
| 90 | + |
| 91 | +// Is checks for object matching |
| 92 | +func (i *DesignatorNotFoundError) Is(err error) bool { |
| 93 | + return is(i, err) |
| 94 | +} |
| 95 | + |
| 96 | +// NewDesignatorNotFoundError creates new DesignatorNotFoundError |
| 97 | +func NewDesignatorNotFoundError(state rune, after string) *DesignatorNotFoundError { |
| 98 | + return &DesignatorNotFoundError{"incorrect ISO 8601 duration %c format, designator not found after token %s", state, after} |
| 99 | +} |
| 100 | + |
| 101 | +// DesignatorValueNotFoundError occurs when no value is found for the designator |
| 102 | +// For example: PT1HM |
| 103 | +type DesignatorValueNotFoundError struct { |
| 104 | + text string |
| 105 | + state rune |
| 106 | + designator rune |
| 107 | +} |
| 108 | + |
| 109 | +// Error defines error output |
| 110 | +func (i *DesignatorValueNotFoundError) Error() string { |
| 111 | + return fmt.Sprintf(i.text, i.state, i.designator) |
| 112 | +} |
| 113 | + |
| 114 | +// Is checks for object matching |
| 115 | +func (i *DesignatorValueNotFoundError) Is(err error) bool { |
| 116 | + return is(i, err) |
| 117 | +} |
| 118 | + |
| 119 | +// NewDesignatorValueNotFoundError creates new DesignatorValueNotFoundError |
| 120 | +func NewDesignatorValueNotFoundError(state, designator rune) *DesignatorValueNotFoundError { |
| 121 | + return &DesignatorValueNotFoundError{"incorrect ISO 8601 duration %c format, %c designator's value not found", state, designator} |
| 122 | +} |
| 123 | + |
| 124 | +// DesignatorMetError occurs when this designator has already been processed previously. |
| 125 | +// Affect: if you write string like PT10H0M10M it defines value as 10 |
| 126 | +// For example: P1Y5Y |
| 127 | +type DesignatorMetError struct { |
| 128 | + text string |
| 129 | + designator rune |
| 130 | +} |
| 131 | + |
| 132 | +// Error defines error output |
| 133 | +func (i *DesignatorMetError) Error() string { |
| 134 | + return fmt.Sprintf(i.text, i.designator) |
| 135 | +} |
| 136 | + |
| 137 | +// Is checks for object matching |
| 138 | +func (i *DesignatorMetError) Is(err error) bool { |
| 139 | + return is(i, err) |
| 140 | +} |
| 141 | + |
| 142 | +// NewDesignatorMetError creates new DesignatorMetError |
| 143 | +func NewDesignatorMetError(designator rune) *DesignatorMetError { |
| 144 | + return &DesignatorMetError{"incorrect ISO 8601 duration format, the designator %c has already been processed", designator} |
| 145 | +} |
0 commit comments