diff options
Diffstat (limited to 'path.go')
| -rw-r--r-- | path.go | 75 |
1 files changed, 0 insertions, 75 deletions
diff --git a/path.go b/path.go deleted file mode 100644 index bb81229..0000000 --- a/path.go +++ /dev/null | |||
| @@ -1,75 +0,0 @@ | |||
| 1 | package main | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | "strings" | ||
| 6 | ) | ||
| 7 | |||
| 8 | type PathComponent string | ||
| 9 | |||
| 10 | type Path []PathComponent | ||
| 11 | |||
| 12 | func NewPathComponent(s string) (PathComponent, error) { | ||
| 13 | if s != strings.TrimSpace(s) { | ||
| 14 | return PathComponent(""), fmt.Errorf("path component cannot contain leading or trailing spaces") | ||
| 15 | } | ||
| 16 | if strings.Contains(s, "/") { | ||
| 17 | return PathComponent(""), fmt.Errorf("path component cannot contain '/'") | ||
| 18 | } | ||
| 19 | if len(s) == 0 { | ||
| 20 | return PathComponent(""), fmt.Errorf("path component cannot be empty") | ||
| 21 | } | ||
| 22 | return PathComponent(s), nil | ||
| 23 | } | ||
| 24 | |||
| 25 | func (c PathComponent) String() string { | ||
| 26 | return string(c) | ||
| 27 | } | ||
| 28 | |||
| 29 | func (p Path) Components() []PathComponent { | ||
| 30 | return []PathComponent(p) | ||
| 31 | } | ||
| 32 | |||
| 33 | func (p Path) Parent() Path { | ||
| 34 | components := []PathComponent(p) | ||
| 35 | if len(components) == 0 { | ||
| 36 | return Path(components) | ||
| 37 | } else { | ||
| 38 | return Path(components[:len(components)-1]) | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | func (p Path) IsRoot() bool { | ||
| 43 | return len(p.Components()) == 0 | ||
| 44 | } | ||
| 45 | |||
| 46 | func ParsePath(pathStr string) (Path, error) { | ||
| 47 | if pathStr == "" || pathStr == "/" { | ||
| 48 | return Path{}, nil | ||
| 49 | } | ||
| 50 | |||
| 51 | if !strings.HasPrefix(pathStr, "/") { | ||
| 52 | return nil, fmt.Errorf("path must be absolute, got: %s", pathStr) | ||
| 53 | } | ||
| 54 | |||
| 55 | pathStr = strings.TrimPrefix(pathStr, "/") | ||
| 56 | if len(pathStr) == 0 { | ||
| 57 | return Path{}, nil | ||
| 58 | } | ||
| 59 | |||
| 60 | parts := strings.Split(pathStr, "/") | ||
| 61 | components := make([]PathComponent, 0, len(parts)) | ||
| 62 | |||
| 63 | for _, part := range parts { | ||
| 64 | if len(part) == 0 { | ||
| 65 | return nil, fmt.Errorf("empty path component in: %s", pathStr) | ||
| 66 | } | ||
| 67 | component, err := NewPathComponent(part) | ||
| 68 | if err != nil { | ||
| 69 | return nil, fmt.Errorf("invalid path component '%s': %w", part, err) | ||
| 70 | } | ||
| 71 | components = append(components, component) | ||
| 72 | } | ||
| 73 | |||
| 74 | return Path(components), nil | ||
| 75 | } | ||
