1
1
use std:: borrow:: Borrow ;
2
- use std:: fmt;
3
2
use std:: path:: Path ;
4
3
use std:: str:: FromStr ;
5
4
6
5
use anyhow:: Result ;
6
+ use derive_more:: derive:: { Display , Into } ;
7
+ use derive_more:: Deref ;
7
8
use lazy_static:: lazy_static;
8
9
use regex:: Regex ;
9
10
@@ -27,20 +28,12 @@ lazy_static! {
27
28
/// let result = ".foo.bar".parse::<Pypath>();
28
29
/// assert!(result.is_err());
29
30
/// ```
30
- #[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
31
- pub struct Pypath {
32
- pub ( crate ) s : String ,
33
- }
31
+ #[ derive( Debug , Clone , PartialEq , Eq , Hash , Deref , Display , Into ) ]
32
+ pub struct Pypath ( String ) ;
34
33
35
34
impl Pypath {
36
35
pub ( crate ) fn new ( s : & str ) -> Pypath {
37
- Pypath { s : s. to_string ( ) }
38
- }
39
- }
40
-
41
- impl fmt:: Display for Pypath {
42
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
43
- write ! ( f, "{}" , self . s)
36
+ Pypath ( s. to_string ( ) )
44
37
}
45
38
}
46
39
@@ -56,24 +49,6 @@ impl FromStr for Pypath {
56
49
}
57
50
}
58
51
59
- impl AsRef < str > for Pypath {
60
- fn as_ref ( & self ) -> & str {
61
- & self . s
62
- }
63
- }
64
-
65
- impl From < Pypath > for String {
66
- fn from ( value : Pypath ) -> Self {
67
- value. s
68
- }
69
- }
70
-
71
- impl < ' a > From < & ' a Pypath > for & ' a str {
72
- fn from ( value : & ' a Pypath ) -> Self {
73
- & value. s
74
- }
75
- }
76
-
77
52
impl Pypath {
78
53
pub ( crate ) fn from_path ( path : & Path , root_path : & Path ) -> Result < Self > {
79
54
let path = path. strip_prefix ( root_path. parent ( ) . unwrap ( ) ) ?;
@@ -85,7 +60,7 @@ impl Pypath {
85
60
Ok ( Pypath :: new ( & s) )
86
61
}
87
62
88
- /// Returns true if the passed pypath is contained by this pypath.
63
+ /// Returns true if this pypath is equal to or an ancestor of the passed pypath.
89
64
///
90
65
/// # Example
91
66
///
@@ -95,14 +70,14 @@ impl Pypath {
95
70
/// let foo_bar: Pypath = "foo.bar".parse().unwrap();
96
71
/// let foo_bar_baz: Pypath = "foo.bar.baz".parse().unwrap();
97
72
///
98
- /// assert!(foo_bar.contains (&foo_bar_baz));
99
- /// assert!(!foo_bar_baz.contains (&foo_bar));
73
+ /// assert!(foo_bar.is_equal_to_or_ancestor_of (&foo_bar_baz));
74
+ /// assert!(!foo_bar_baz.is_equal_to_or_ancestor_of (&foo_bar));
100
75
/// ```
101
- pub fn contains ( & self , other : & Pypath ) -> bool {
102
- self == other || other. s . starts_with ( & ( self . s . clone ( ) + "." ) )
76
+ pub fn is_equal_to_or_ancestor_of ( & self , other : & Pypath ) -> bool {
77
+ self == other || other. 0 . starts_with ( & ( self . 0 . clone ( ) + "." ) )
103
78
}
104
79
105
- /// Returns true if this pypath is contained by the passed pypath.
80
+ /// Returns true if this pypath is equal to or a descendant of the passed pypath.
106
81
///
107
82
/// # Example
108
83
///
@@ -112,11 +87,11 @@ impl Pypath {
112
87
/// let foo_bar: Pypath = "foo.bar".parse().unwrap();
113
88
/// let foo_bar_baz: Pypath = "foo.bar.baz".parse().unwrap();
114
89
///
115
- /// assert!(!foo_bar.is_contained_by (&foo_bar_baz));
116
- /// assert!(foo_bar_baz.is_contained_by (&foo_bar));
90
+ /// assert!(!foo_bar.is_equal_to_or_descendant_of (&foo_bar_baz));
91
+ /// assert!(foo_bar_baz.is_equal_to_or_descendant_of (&foo_bar));
117
92
/// ```
118
- pub fn is_contained_by ( & self , other : & Pypath ) -> bool {
119
- other. contains ( self )
93
+ pub fn is_equal_to_or_descendant_of ( & self , other : & Pypath ) -> bool {
94
+ other. is_equal_to_or_ancestor_of ( self )
120
95
}
121
96
122
97
/// Returns the parent of this pypath.
@@ -132,9 +107,9 @@ impl Pypath {
132
107
///assert!(foo_bar_baz.parent() == foo_bar);
133
108
/// ```
134
109
pub fn parent ( & self ) -> Self {
135
- let mut v = self . s . split ( "." ) . collect :: < Vec < _ > > ( ) ;
110
+ let mut v = self . 0 . split ( "." ) . collect :: < Vec < _ > > ( ) ;
136
111
v. pop ( ) ;
137
- Pypath { s : v. join ( "." ) }
112
+ Pypath ( v. join ( "." ) )
138
113
}
139
114
}
140
115
@@ -208,19 +183,19 @@ mod tests {
208
183
}
209
184
210
185
#[ test]
211
- fn test_contains ( ) -> Result < ( ) > {
212
- assert ! ( Pypath :: new( "foo.bar" ) . contains ( & Pypath :: new( "foo.bar" ) ) ) ;
213
- assert ! ( Pypath :: new( "foo.bar" ) . contains ( & Pypath :: new( "foo.bar.baz" ) ) ) ;
214
- assert ! ( !Pypath :: new( "foo.bar" ) . contains ( & Pypath :: new( "foo" ) ) ) ;
186
+ fn test_is_equal_or_ancestor ( ) -> Result < ( ) > {
187
+ assert ! ( Pypath :: new( "foo.bar" ) . is_equal_to_or_ancestor_of ( & Pypath :: new( "foo.bar" ) ) ) ;
188
+ assert ! ( Pypath :: new( "foo.bar" ) . is_equal_to_or_ancestor_of ( & Pypath :: new( "foo.bar.baz" ) ) ) ;
189
+ assert ! ( !Pypath :: new( "foo.bar" ) . is_equal_to_or_ancestor_of ( & Pypath :: new( "foo" ) ) ) ;
215
190
216
191
Ok ( ( ) )
217
192
}
218
193
219
194
#[ test]
220
- fn test_contained_by ( ) -> Result < ( ) > {
221
- assert ! ( Pypath :: new( "foo.bar" ) . is_contained_by ( & Pypath :: new( "foo.bar" ) ) ) ;
222
- assert ! ( !Pypath :: new( "foo.bar" ) . is_contained_by ( & Pypath :: new( "foo.bar.baz" ) ) ) ;
223
- assert ! ( Pypath :: new( "foo.bar" ) . is_contained_by ( & Pypath :: new( "foo" ) ) ) ;
195
+ fn test_is_equal_or_descendant ( ) -> Result < ( ) > {
196
+ assert ! ( Pypath :: new( "foo.bar" ) . is_equal_to_or_descendant_of ( & Pypath :: new( "foo.bar" ) ) ) ;
197
+ assert ! ( !Pypath :: new( "foo.bar" ) . is_equal_to_or_descendant_of ( & Pypath :: new( "foo.bar.baz" ) ) ) ;
198
+ assert ! ( Pypath :: new( "foo.bar" ) . is_equal_to_or_descendant_of ( & Pypath :: new( "foo" ) ) ) ;
224
199
225
200
Ok ( ( ) )
226
201
}
0 commit comments