@@ -298,8 +298,8 @@ quick_error! {
298
298
PwdFileQuery {
299
299
display( "User home info missing" )
300
300
}
301
- Unsupported {
302
- display( "Not available on this platform" )
301
+ UserInterpolationUnsupported {
302
+ display( "User interpolation is not available on this platform" )
303
303
}
304
304
}
305
305
}
@@ -308,11 +308,25 @@ quick_error! {
308
308
///
309
309
/// Git represents file paths as byte arrays, modeled here as owned or borrowed byte sequences.
310
310
#[ derive( Clone , Eq , PartialEq , Ord , PartialOrd , Hash , Debug ) ]
311
- #[ allow( missing_docs) ]
312
311
pub struct Path < ' a > {
312
+ /// The interpolated path
313
313
pub value : Cow < ' a , [ u8 ] > ,
314
314
}
315
315
316
+ impl < ' a > std:: ops:: Deref for Path < ' a > {
317
+ type Target = [ u8 ] ;
318
+
319
+ fn deref ( & self ) -> & Self :: Target {
320
+ self . value . as_ref ( )
321
+ }
322
+ }
323
+
324
+ impl < ' a > AsRef < [ u8 ] > for Path < ' a > {
325
+ fn as_ref ( & self ) -> & [ u8 ] {
326
+ self . value . as_ref ( )
327
+ }
328
+ }
329
+
316
330
impl < ' a > TryFrom < Cow < ' a , [ u8 ] > > for Path < ' a > {
317
331
type Error = PathError ;
318
332
@@ -373,7 +387,7 @@ impl Path<'_> {
373
387
374
388
#[ cfg( target_os = "windows" ) ]
375
389
fn interpolate_user ( _val : Cow < [ u8 ] > , _slash : u8 ) -> Result < Path , PathError > {
376
- Err ( PathError :: Unsupported )
390
+ Err ( PathError :: UserInterpolationUnsupported )
377
391
}
378
392
379
393
#[ cfg( not( target_os = "windows" ) ) ]
@@ -1524,12 +1538,11 @@ mod path {
1524
1538
#[ test]
1525
1539
fn not_interpolated ( ) {
1526
1540
let path = & b"/foo/bar" [ ..] ;
1527
- let borrowed_path = Cow :: Borrowed ( path) ;
1528
- assert_eq ! (
1529
- Path :: try_from( borrowed_path) . unwrap( ) ,
1530
- Path {
1531
- value: Cow :: Borrowed ( path)
1532
- }
1541
+ let actual = Path :: try_from ( Cow :: Borrowed ( path) ) . unwrap ( ) ;
1542
+ assert_eq ! ( & * actual, path) ;
1543
+ assert ! (
1544
+ matches!( & actual. value, Cow :: Borrowed ( _) ) ,
1545
+ "it does not unnecessarily copy values"
1533
1546
) ;
1534
1547
}
1535
1548
@@ -1547,17 +1560,24 @@ mod path {
1547
1560
let git_install_dir = "/tmp/git" ;
1548
1561
let expected = format ! ( "{}/foo/bar" , git_install_dir) ;
1549
1562
assert_eq ! (
1550
- Path :: interpolate( val, Some ( std:: path:: Path :: new( git_install_dir) ) ) . unwrap( ) ,
1551
- Path {
1552
- value: Cow :: Borrowed ( expected. as_bytes( ) )
1553
- }
1563
+ & * Path :: interpolate( val, Some ( std:: path:: Path :: new( git_install_dir) ) ) . unwrap( ) ,
1564
+ expected. as_bytes( )
1565
+ ) ;
1566
+ }
1567
+
1568
+ #[ test]
1569
+ fn disabled_prefix_interpoldation ( ) {
1570
+ let path = & b"./%(prefix)/foo/bar" [ ..] ;
1571
+ let git_install_dir = "/tmp/git" ;
1572
+ assert_eq ! (
1573
+ & * Path :: interpolate( Cow :: Borrowed ( path) , Some ( std:: path:: Path :: new( git_install_dir) ) ) . unwrap( ) ,
1574
+ path
1554
1575
) ;
1555
1576
}
1556
1577
1557
1578
#[ test]
1558
1579
fn tilde_interpolated ( ) {
1559
1580
let path = & b"~/foo/bar" [ ..] ;
1560
- let borrowed_path = Cow :: Borrowed ( path) ;
1561
1581
let home = dirs:: home_dir ( )
1562
1582
. expect ( "empty home" )
1563
1583
. to_str ( )
@@ -1567,10 +1587,8 @@ mod path {
1567
1587
let home = home. replace ( "\\ " , "/" ) ;
1568
1588
let expected = format ! ( "{}/foo/bar" , home) ;
1569
1589
assert_eq ! (
1570
- Path :: try_from( borrowed_path) . unwrap( ) ,
1571
- Path {
1572
- value: Cow :: Borrowed ( expected. as_bytes( ) )
1573
- }
1590
+ Path :: try_from( Cow :: Borrowed ( path) ) . unwrap( ) . as_ref( ) ,
1591
+ expected. as_bytes( )
1574
1592
) ;
1575
1593
}
1576
1594
@@ -1579,7 +1597,7 @@ mod path {
1579
1597
fn user_interpolated ( ) {
1580
1598
assert ! ( matches!(
1581
1599
Path :: try_from( Cow :: Borrowed ( & b"~baz/foo/bar" [ ..] ) ) ,
1582
- Err ( PathError :: Unsupported )
1600
+ Err ( PathError :: UserInterpolationUnsupported )
1583
1601
) ) ;
1584
1602
}
1585
1603
@@ -1588,14 +1606,11 @@ mod path {
1588
1606
fn user_interpolated ( ) {
1589
1607
let user = std:: env:: var ( "USER" ) . unwrap ( ) ;
1590
1608
let path = format ! ( "~{}/foo/bar" , user) ;
1591
- let borrowed_path = Cow :: Borrowed ( path. as_bytes ( ) ) ;
1592
1609
let home = std:: env:: var ( "HOME" ) . unwrap ( ) ;
1593
1610
let expected = format ! ( "{}/foo/bar" , home) ;
1594
1611
assert_eq ! (
1595
- Path :: try_from( borrowed_path) . unwrap( ) ,
1596
- Path {
1597
- value: Cow :: Borrowed ( expected. as_bytes( ) )
1598
- }
1612
+ & * Path :: try_from( Cow :: Borrowed ( path. as_bytes( ) ) ) . unwrap( ) ,
1613
+ expected. as_bytes( )
1599
1614
) ;
1600
1615
}
1601
1616
}
0 commit comments