summaryrefslogtreecommitdiff
path: root/path.go
diff options
context:
space:
mode:
authordiogo464 <[email protected]>2025-08-08 17:40:19 +0100
committerdiogo464 <[email protected]>2025-08-08 17:40:44 +0100
commitbebf1cd5fc4668b7970e3e2e426ad103ecbc670c (patch)
treeafee1e59890836e367a075749745066450d63f44 /path.go
parent9a25abd1d6ef6f5b0e2c08751183f63db43c73a5 (diff)
rust cli init
Diffstat (limited to 'path.go')
-rw-r--r--path.go75
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 @@
1package main
2
3import (
4 "fmt"
5 "strings"
6)
7
8type PathComponent string
9
10type Path []PathComponent
11
12func 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
25func (c PathComponent) String() string {
26 return string(c)
27}
28
29func (p Path) Components() []PathComponent {
30 return []PathComponent(p)
31}
32
33func (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
42func (p Path) IsRoot() bool {
43 return len(p.Components()) == 0
44}
45
46func 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}