willbe/entity/files/
manifest_file.rs1#![ allow( clippy::std_instead_of_alloc, clippy::std_instead_of_core ) ]
2
3
4use crate::*;
5
6use entity::
7{
8 PathError,
9 CrateDir,
10};
11use core::
12{
13 fmt,
14 ops::
15 {
16 Deref,
17 DerefMut,
18 },
19};
20use std::
21{
22 path::{ Path, PathBuf },
23 io,
24};
25
26use pth::{ AbsolutePath, Utf8Path, Utf8PathBuf };
27
28#[ derive( Clone, Ord, PartialOrd, Eq, PartialEq, Hash ) ]
35pub struct ManifestFile( AbsolutePath );
36
37impl ManifestFile
38{
39 #[ inline( always ) ]
48 #[ must_use ]
49 pub fn inner( self ) -> AbsolutePath
50 {
51 self.0
52 }
53
54 #[ inline( always ) ]
56 #[ must_use ]
57 pub fn crate_dir( self ) -> CrateDir
58 {
59 self.into()
60 }
61
62}
63
64impl fmt::Display for ManifestFile
65{
66 fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
67 {
68 write!( f, "{}", self.0.display() )
69 }
70}
71
72impl fmt::Debug for ManifestFile
73{
74 fn fmt( &self, f : &mut fmt::Formatter< '_ > ) -> fmt::Result
75 {
76 write!( f, "manifest file :: {}", self.0.display() )
77 }
78}
79
80impl From< CrateDir > for ManifestFile
89{
90 fn from( src : CrateDir ) -> Self
91 {
92 Self( src.absolute_path().join( "Cargo.toml" ) )
93 }
94}
95
96impl From< ManifestFile > for AbsolutePath
97{
98 fn from( src : ManifestFile ) -> Self
99 {
100 src.inner()
101 }
102}
103
104impl From< ManifestFile > for PathBuf
105{
106 fn from( src : ManifestFile ) -> Self
107 {
108 src.inner().inner()
109 }
110}
111
112impl< 'a > TryFrom< &'a ManifestFile > for &'a str
121{
122 type Error = std::io::Error;
123 fn try_from( src : &'a ManifestFile ) -> Result< &'a str, Self::Error >
124 {
125 ( &src.0 ).try_into()
126 }
127}
128
129impl TryFrom< &ManifestFile > for String
130{
131 type Error = std::io::Error;
132 fn try_from( src : &ManifestFile ) -> Result< String, Self::Error >
133 {
134 let src2 : &str = src.try_into()?;
135 Result::Ok( src2.into() )
136 }
137}
138
139impl TryFrom< &AbsolutePath > for ManifestFile
140{
141 type Error = PathError;
142
143 #[ inline( always ) ]
144 fn try_from( manifest_file : &AbsolutePath ) -> Result< Self, Self::Error >
145 {
146 manifest_file.clone().try_into()
147 }
148}
149
150impl TryFrom< AbsolutePath > for ManifestFile
151{
152 type Error = PathError;
153
154 #[ inline( always ) ]
155 fn try_from( manifest_file : AbsolutePath ) -> Result< Self, Self::Error >
156 {
157
158 if !manifest_file.as_ref().ends_with( "Cargo.toml" )
159 {
160 let err = io::Error::other( format!( "File path does not end with Cargo.toml as it should {}", manifest_file.display() ) );
161 return Err( PathError::Io( err ) );
162 }
163
164 if !manifest_file.as_ref().is_file()
165 {
166 let err = io::Error::new( io::ErrorKind::InvalidData, format!( "Cannot find crate dir at {}", manifest_file.display() ) );
167 return Err( PathError::Io( err ) );
168 }
169 Result::Ok( Self( manifest_file ) )
170 }
171}
172
173impl TryFrom< PathBuf > for ManifestFile
174{
175 type Error = PathError;
176
177 #[ inline( always ) ]
178 fn try_from( manifest_file : PathBuf ) -> Result< Self, Self::Error >
179 {
180 Self::try_from( AbsolutePath::try_from( manifest_file )? )
181 }
182}
183
184impl TryFrom< &PathBuf > for ManifestFile
185{
186 type Error = PathError;
187
188 #[ inline( always ) ]
189 fn try_from( manifest_file : &PathBuf ) -> Result< Self, Self::Error >
190 {
191 Self::try_from( AbsolutePath::try_from( manifest_file )? )
192 }
193}
194
195impl TryFrom< &Path > for ManifestFile
196{
197 type Error = PathError;
198
199 #[ inline( always ) ]
200 fn try_from( manifest_file : &Path ) -> Result< Self, Self::Error >
201 {
202 Self::try_from( AbsolutePath::try_from( manifest_file )? )
203 }
204}
205
206impl TryFrom< &str > for ManifestFile
207{
208 type Error = PathError;
209
210 #[ inline( always ) ]
211 fn try_from( crate_dir_path : &str ) -> Result< Self, Self::Error >
212 {
213 Self::try_from( AbsolutePath::try_from( crate_dir_path )? )
214 }
215}
216
217impl TryFrom< Utf8PathBuf > for ManifestFile
218{
219 type Error = PathError;
220
221 #[ inline( always ) ]
222 fn try_from( manifest_file : Utf8PathBuf ) -> Result< Self, Self::Error >
223 {
224 Self::try_from( AbsolutePath::try_from( manifest_file )? )
225 }
226}
227
228impl TryFrom< &Utf8PathBuf > for ManifestFile
229{
230 type Error = PathError;
231
232 #[ inline( always ) ]
233 fn try_from( manifest_file : &Utf8PathBuf ) -> Result< Self, Self::Error >
234 {
235 Self::try_from( AbsolutePath::try_from( manifest_file )? )
236 }
237}
238
239impl TryFrom< &Utf8Path > for ManifestFile
240{
241 type Error = PathError;
242
243 #[ inline( always ) ]
244 fn try_from( manifest_file : &Utf8Path ) -> Result< Self, Self::Error >
245 {
246 Self::try_from( AbsolutePath::try_from( manifest_file )? )
247 }
248}
249
250impl AsRef< Path > for ManifestFile
251{
252 fn as_ref( &self ) -> &Path
253 {
254 self.0.as_ref()
255 }
256}
257
258impl AsMut< Path > for ManifestFile
259{
260 fn as_mut( &mut self ) -> &mut Path
261 {
262 self.0.as_mut()
263 }
264}
265
266impl Deref for ManifestFile
267{
268 type Target = AbsolutePath;
269 fn deref( &self ) -> &Self::Target
270 {
271 &self.0
272 }
273}
274
275impl DerefMut for ManifestFile
276{
277 fn deref_mut( &mut self ) -> &mut Self::Target
278 {
279 &mut self.0
280 }
281}