-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
158 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package hashtree | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// pathToString converts a path to a string, by joining the (string) labels with a slash. | ||
func pathToString(path []Label) string { | ||
var sb strings.Builder | ||
for i, p := range path { | ||
if i > 0 { | ||
sb.WriteByte('/') | ||
} | ||
sb.WriteString(string(p)) | ||
} | ||
return sb.String() | ||
} | ||
|
||
type LookupError struct { | ||
Type LookupResultType | ||
Path string | ||
} | ||
|
||
// NewLookupAbsentError returns a new LookupError with type LookupResultAbsent. | ||
func NewLookupAbsentError(path ...Label) LookupError { | ||
return LookupError{ | ||
Type: LookupResultAbsent, | ||
Path: pathToString(path), | ||
} | ||
} | ||
|
||
// NewLookupError returns a new LookupError with type LookupResultError. | ||
func NewLookupError(path ...Label) LookupError { | ||
return LookupError{ | ||
Type: LookupResultError, | ||
Path: pathToString(path), | ||
} | ||
} | ||
|
||
// NewLookupUnknownError returns a new LookupError with type LookupResultUnknown. | ||
func NewLookupUnknownError(path ...Label) LookupError { | ||
return LookupError{ | ||
Type: LookupResultUnknown, | ||
Path: pathToString(path), | ||
} | ||
} | ||
|
||
func (l LookupError) Error() string { | ||
return fmt.Sprintf("lookup error (path: %q): %s", l.Path, l.error()) | ||
} | ||
|
||
func (l LookupError) error() string { | ||
switch l.Type { | ||
case LookupResultAbsent: | ||
return "not found, not present in the tree" | ||
case LookupResultUnknown: | ||
return "not found, could be pruned" | ||
case LookupResultError: | ||
return "error, can not exist in the tree" | ||
default: | ||
return "unknown lookup error" | ||
} | ||
} | ||
|
||
// LookupResultType is the type of the lookup result. | ||
// It indicates whether the result is guaranteed to be absent, unknown or is an invalid tree. | ||
type LookupResultType int | ||
|
||
const ( | ||
// LookupResultAbsent means that the result is guaranteed to be absent. | ||
LookupResultAbsent LookupResultType = iota | ||
// LookupResultUnknown means that the result is unknown, some leaves were pruned. | ||
LookupResultUnknown | ||
// LookupResultError means that the result is an error, the path is not valid in this context. | ||
LookupResultError | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.