@@ -111,13 +111,13 @@ type immutablePath struct {
111
111
112
112
func NewImmutablePath (p Path ) (ImmutablePath , error ) {
113
113
if p .Namespace ().Mutable () {
114
- return nil , fmt . Errorf ( "path was expected to be immutable: %s" , p .String ())
114
+ return nil , ErrInvalidPath { err : ErrExpectedImmutable , path : p .String ()}
115
115
}
116
116
117
117
segments := p .Segments ()
118
118
cid , err := cid .Decode (segments [1 ])
119
119
if err != nil {
120
- return nil , & ErrInvalidPath {error : fmt . Errorf ( "invalid CID: %w" , err ) , path : p .String ()}
120
+ return nil , & ErrInvalidPath {err : err , path : p .String ()}
121
121
}
122
122
123
123
return immutablePath {path : p , cid : cid }, nil
@@ -149,7 +149,7 @@ func (ip immutablePath) Remainder() string {
149
149
150
150
// NewIPFSPath returns a new "/ipfs" path with the provided CID.
151
151
func NewIPFSPath (cid cid.Cid ) ImmutablePath {
152
- return & immutablePath {
152
+ return immutablePath {
153
153
path : path {
154
154
str : fmt .Sprintf ("/%s/%s" , IPFSNamespace , cid .String ()),
155
155
namespace : IPFSNamespace ,
@@ -160,7 +160,7 @@ func NewIPFSPath(cid cid.Cid) ImmutablePath {
160
160
161
161
// NewIPLDPath returns a new "/ipld" path with the provided CID.
162
162
func NewIPLDPath (cid cid.Cid ) ImmutablePath {
163
- return & immutablePath {
163
+ return immutablePath {
164
164
path : path {
165
165
str : fmt .Sprintf ("/%s/%s" , IPLDNamespace , cid .String ()),
166
166
namespace : IPLDNamespace ,
@@ -169,10 +169,10 @@ func NewIPLDPath(cid cid.Cid) ImmutablePath {
169
169
}
170
170
}
171
171
172
- // NewPath takes the given string and returns a well-forme and sanitized [Path].
172
+ // NewPath takes the given string and returns a well-formed and sanitized [Path].
173
173
// The given string is cleaned through [gopath.Clean], but preserving the final
174
174
// trailing slash. This function returns an error when the given string is not
175
- // a valid path.
175
+ // a valid content path.
176
176
func NewPath (str string ) (Path , error ) {
177
177
cleaned := gopath .Clean (str )
178
178
components := strings .Split (cleaned , "/" )
@@ -186,18 +186,18 @@ func NewPath(str string) (Path, error) {
186
186
// components: [" " "{namespace}" "{element}"]. The first component must therefore
187
187
// be empty.
188
188
if len (components ) < 3 || components [0 ] != "" {
189
- return nil , & ErrInvalidPath {error : fmt . Errorf ( "not enough path components" ) , path : str }
189
+ return nil , & ErrInvalidPath {err : ErrInsufficientComponents , path : str }
190
190
}
191
191
192
192
switch components [1 ] {
193
193
case "ipfs" , "ipld" :
194
194
if components [2 ] == "" {
195
- return nil , & ErrInvalidPath {error : fmt . Errorf ( "not enough path components" ) , path : str }
195
+ return nil , & ErrInvalidPath {err : ErrInsufficientComponents , path : str }
196
196
}
197
197
198
198
cid , err := cid .Decode (components [2 ])
199
199
if err != nil {
200
- return nil , & ErrInvalidPath {error : fmt . Errorf ( "invalid CID: %w" , err ) , path : str }
200
+ return nil , & ErrInvalidPath {err : err , path : str }
201
201
}
202
202
203
203
ns := IPFSNamespace
@@ -214,15 +214,15 @@ func NewPath(str string) (Path, error) {
214
214
}, nil
215
215
case "ipns" :
216
216
if components [2 ] == "" {
217
- return nil , & ErrInvalidPath {error : fmt . Errorf ( "not enough path components" ) , path : str }
217
+ return nil , & ErrInvalidPath {err : ErrInsufficientComponents , path : str }
218
218
}
219
219
220
220
return path {
221
221
str : cleaned ,
222
222
namespace : IPNSNamespace ,
223
223
}, nil
224
224
default :
225
- return nil , & ErrInvalidPath {error : fmt .Errorf ("unknown namespace %q" , components [1 ]), path : str }
225
+ return nil , & ErrInvalidPath {err : fmt .Errorf ("%w: %q" , ErrUnknownNamespace , components [1 ]), path : str }
226
226
}
227
227
}
228
228
0 commit comments