1
1
use std:: borrow:: Cow ;
2
+ use std:: convert:: TryInto ;
2
3
3
4
use crate :: {
4
- bstr:: { BStr , ByteSlice , ByteVec } ,
5
+ bstr:: { BStr , ByteSlice } ,
5
6
remote, Reference ,
6
7
} ;
7
8
@@ -14,20 +15,60 @@ pub enum Name<'repo> {
14
15
Url ( Cow < ' repo , BStr > ) ,
15
16
}
16
17
17
- impl Name < ' _ > {
18
- /// Return this instance as a symbolic name, if it is one.
19
- pub fn as_symbol ( & self ) -> Option < & str > {
20
- match self {
21
- Name :: Symbol ( n) => n. as_ref ( ) . into ( ) ,
22
- Name :: Url ( _) => None ,
18
+ mod name {
19
+ use super :: Name ;
20
+ use crate :: bstr:: { BStr , ByteSlice , ByteVec } ;
21
+ use std:: borrow:: Cow ;
22
+ use std:: convert:: TryFrom ;
23
+
24
+ impl Name < ' _ > {
25
+ /// Obtain the name as string representation.
26
+ pub fn as_bstr ( & self ) -> & BStr {
27
+ match self {
28
+ Name :: Symbol ( v) => v. as_ref ( ) . into ( ) ,
29
+ Name :: Url ( v) => v. as_ref ( ) ,
30
+ }
31
+ }
32
+
33
+ /// Return this instance as a symbolic name, if it is one.
34
+ pub fn as_symbol ( & self ) -> Option < & str > {
35
+ match self {
36
+ Name :: Symbol ( n) => n. as_ref ( ) . into ( ) ,
37
+ Name :: Url ( _) => None ,
38
+ }
39
+ }
40
+
41
+ /// Return this instance as url, if it is one.
42
+ pub fn as_url ( & self ) -> Option < & BStr > {
43
+ match self {
44
+ Name :: Url ( n) => n. as_ref ( ) . into ( ) ,
45
+ Name :: Symbol ( _) => None ,
46
+ }
47
+ }
48
+ }
49
+
50
+ impl < ' a > TryFrom < Cow < ' a , BStr > > for Name < ' a > {
51
+ type Error = Cow < ' a , BStr > ;
52
+
53
+ fn try_from ( name : Cow < ' a , BStr > ) -> Result < Self , Self :: Error > {
54
+ if name. contains ( & b'/' ) {
55
+ Ok ( Name :: Url ( name) )
56
+ } else {
57
+ match name {
58
+ Cow :: Borrowed ( n) => n. to_str ( ) . ok ( ) . map ( Cow :: Borrowed ) . ok_or ( name) ,
59
+ Cow :: Owned ( n) => Vec :: from ( n)
60
+ . into_string ( )
61
+ . map_err ( |err| Cow :: Owned ( err. into_vec ( ) . into ( ) ) )
62
+ . map ( Cow :: Owned ) ,
63
+ }
64
+ . map ( Name :: Symbol )
65
+ }
23
66
}
24
67
}
25
68
26
- /// Return this instance as url, if it is one.
27
- pub fn as_url ( & self ) -> Option < & BStr > {
28
- match self {
29
- Name :: Url ( n) => n. as_ref ( ) . into ( ) ,
30
- Name :: Symbol ( _) => None ,
69
+ impl < ' a > AsRef < BStr > for Name < ' a > {
70
+ fn as_ref ( & self ) -> & BStr {
71
+ self . as_bstr ( )
31
72
}
32
73
}
33
74
}
@@ -56,17 +97,7 @@ impl<'repo> Reference<'repo> {
56
97
} )
57
98
. flatten ( )
58
99
. or_else ( || config. string ( "branch" , Some ( name) , "remote" ) )
59
- . and_then ( |name| {
60
- if name. contains ( & b'/' ) {
61
- Some ( Name :: Url ( name) )
62
- } else {
63
- match name {
64
- Cow :: Borrowed ( n) => n. to_str ( ) . ok ( ) . map ( Cow :: Borrowed ) ,
65
- Cow :: Owned ( n) => Vec :: from ( n) . into_string ( ) . ok ( ) . map ( Cow :: Owned ) ,
66
- }
67
- . map ( Name :: Symbol )
68
- }
69
- } )
100
+ . and_then ( |name| name. try_into ( ) . ok ( ) )
70
101
}
71
102
72
103
/// Like [`remote_name(…)`][Self::remote_name()], but configures the returned `Remote` with additional information like
0 commit comments