@@ -327,12 +327,10 @@ impl<'a> AsRef<[u8]> for Path<'a> {
327
327
}
328
328
}
329
329
330
- impl < ' a > TryFrom < Cow < ' a , [ u8 ] > > for Path < ' a > {
331
- type Error = PathError ;
332
-
330
+ impl < ' a > From < Cow < ' a , [ u8 ] > > for Path < ' a > {
333
331
#[ inline]
334
- fn try_from ( c : Cow < ' a , [ u8 ] > ) -> Result < Self , Self :: Error > {
335
- Self :: interpolate ( c , None )
332
+ fn from ( value : Cow < ' a , [ u8 ] > ) -> Self {
333
+ Path { value }
336
334
}
337
335
}
338
336
@@ -348,51 +346,48 @@ impl Path<'_> {
348
346
/// optionally provided by the caller through `git_install_dir`.
349
347
///
350
348
/// Any other, non-empty path value is returned unchanged and error is returned in case of an empty path value.
351
- pub fn interpolate < ' a > (
352
- path : Cow < ' a , [ u8 ] > ,
353
- git_install_dir : Option < & ' a std:: path:: Path > ,
354
- ) -> Result < Path < ' a > , PathError > {
355
- if path. is_empty ( ) {
349
+ pub fn interpolate ( self , git_install_dir : Option < & std:: path:: Path > ) -> Result < Self , PathError > {
350
+ if self . is_empty ( ) {
356
351
return Err ( PathError :: Missing { what : "path" } ) ;
357
352
}
358
353
359
354
const PREFIX : & [ u8 ] = b"%(prefix)/" ;
360
355
const SLASH : u8 = b'/' ;
361
- if path . starts_with ( PREFIX ) {
356
+ if self . starts_with ( PREFIX ) {
362
357
let mut expanded = git_features:: path:: into_bytes ( git_install_dir. ok_or ( PathError :: Missing {
363
358
what : "git install dir" ,
364
359
} ) ?)
365
360
. context ( "git install dir" ) ?
366
361
. into_owned ( ) ;
367
- let ( _prefix, val) = path . split_at ( PREFIX . len ( ) - 1 ) ;
362
+ let ( _prefix, val) = self . split_at ( PREFIX . len ( ) - 1 ) ;
368
363
expanded. extend ( val) ;
369
364
Ok ( Path {
370
365
value : Cow :: Owned ( expanded) ,
371
366
} )
372
- } else if path . starts_with ( b"~/" ) {
367
+ } else if self . starts_with ( b"~/" ) {
373
368
let home_path = dirs:: home_dir ( ) . ok_or ( PathError :: Missing { what : "home dir" } ) ?;
374
369
let mut expanded = git_features:: path:: into_bytes ( home_path)
375
370
. context ( "home dir" ) ?
376
371
. into_owned ( ) ;
377
- let ( _prefix, val) = path . split_at ( SLASH . len ( ) ) ;
372
+ let ( _prefix, val) = self . split_at ( SLASH . len ( ) ) ;
378
373
expanded. extend ( val) ;
379
374
let expanded = git_features:: path:: convert:: to_unix_separators ( expanded) ;
380
375
Ok ( Path { value : expanded } )
381
- } else if path . starts_with ( b"~" ) && path . contains ( & SLASH ) {
382
- Self :: interpolate_user ( path , SLASH )
376
+ } else if self . starts_with ( b"~" ) && self . contains ( & SLASH ) {
377
+ self . interpolate_user ( SLASH )
383
378
} else {
384
- Ok ( Path { value : path } )
379
+ Ok ( self )
385
380
}
386
381
}
387
382
388
383
#[ cfg( target_os = "windows" ) ]
389
- fn interpolate_user ( _val : Cow < [ u8 ] > , _slash : u8 ) -> Result < Path , PathError > {
384
+ fn interpolate_user ( self , _slash : u8 ) -> Result < Self , PathError > {
390
385
Err ( PathError :: UserInterpolationUnsupported )
391
386
}
392
387
393
388
#[ cfg( not( target_os = "windows" ) ) ]
394
- fn interpolate_user ( val : Cow < [ u8 ] > , slash : u8 ) -> Result < Path , PathError > {
395
- let ( _prefix, val) = val . split_at ( slash. len ( ) ) ;
389
+ fn interpolate_user ( self , slash : u8 ) -> Result < Self , PathError > {
390
+ let ( _prefix, val) = self . split_at ( slash. len ( ) ) ;
396
391
let i = val
397
392
. iter ( )
398
393
. position ( |& e| e == slash)
@@ -1549,7 +1544,7 @@ mod path {
1549
1544
#[ test]
1550
1545
fn empty_is_error ( ) {
1551
1546
assert ! ( matches!(
1552
- Path :: try_from ( Cow :: Borrowed ( "" . as_bytes( ) ) ) ,
1547
+ Path :: from ( Cow :: Borrowed ( "" . as_bytes( ) ) ) . interpolate ( None ) ,
1553
1548
Err ( PathError :: Missing { what: "path" } )
1554
1549
) ) ;
1555
1550
}
@@ -1560,7 +1555,9 @@ mod path {
1560
1555
let git_install_dir = "/tmp/git" ;
1561
1556
let expected = format ! ( "{}/foo/bar" , git_install_dir) ;
1562
1557
assert_eq ! (
1563
- & * Path :: interpolate( val, Some ( std:: path:: Path :: new( git_install_dir) ) ) . unwrap( ) ,
1558
+ & * Path :: from( val)
1559
+ . interpolate( Some ( std:: path:: Path :: new( git_install_dir) ) )
1560
+ . unwrap( ) ,
1564
1561
expected. as_bytes( )
1565
1562
) ;
1566
1563
}
@@ -1570,7 +1567,9 @@ mod path {
1570
1567
let path = & b"./%(prefix)/foo/bar" [ ..] ;
1571
1568
let git_install_dir = "/tmp/git" ;
1572
1569
assert_eq ! (
1573
- & * Path :: interpolate( Cow :: Borrowed ( path) , Some ( std:: path:: Path :: new( git_install_dir) ) ) . unwrap( ) ,
1570
+ & * Path :: from( Cow :: Borrowed ( path) )
1571
+ . interpolate( Some ( std:: path:: Path :: new( git_install_dir) ) )
1572
+ . unwrap( ) ,
1574
1573
path
1575
1574
) ;
1576
1575
}
@@ -1587,7 +1586,7 @@ mod path {
1587
1586
let home = home. replace ( "\\ " , "/" ) ;
1588
1587
let expected = format ! ( "{}/foo/bar" , home) ;
1589
1588
assert_eq ! (
1590
- Path :: try_from ( Cow :: Borrowed ( path) ) . unwrap( ) . as_ref( ) ,
1589
+ Path :: from ( Cow :: Borrowed ( path) ) . interpolate ( None ) . unwrap( ) . as_ref( ) ,
1591
1590
expected. as_bytes( )
1592
1591
) ;
1593
1592
}
@@ -1596,7 +1595,7 @@ mod path {
1596
1595
#[ test]
1597
1596
fn user_interpolated ( ) {
1598
1597
assert ! ( matches!(
1599
- Path :: try_from ( Cow :: Borrowed ( & b"~baz/foo/bar" [ ..] ) ) ,
1598
+ Path :: from ( Cow :: Borrowed ( & b"~baz/foo/bar" [ ..] ) ) . interpolate ( None ) ,
1600
1599
Err ( PathError :: UserInterpolationUnsupported )
1601
1600
) ) ;
1602
1601
}
@@ -1609,7 +1608,7 @@ mod path {
1609
1608
let home = std:: env:: var ( "HOME" ) . unwrap ( ) ;
1610
1609
let expected = format ! ( "{}/foo/bar" , home) ;
1611
1610
assert_eq ! (
1612
- & * Path :: try_from ( Cow :: Borrowed ( path. as_bytes( ) ) ) . unwrap( ) ,
1611
+ & * Path :: from ( Cow :: Borrowed ( path. as_bytes( ) ) ) . interpolate ( None ) . unwrap( ) ,
1613
1612
expected. as_bytes( )
1614
1613
) ;
1615
1614
}
0 commit comments