willbe/entity/files/
source_file.rs

1#![ allow( clippy ::std_instead_of_alloc, clippy ::std_instead_of_core ) ]
2
3
4
5use crate :: *;
6
7use entity ::
8{
9  PathError,
10  ManifestFile,
11};
12use core ::
13{
14  fmt,
15  ops ::
16  {
17  Deref,
18  DerefMut,
19 },
20};
21use std ::
22{
23  fs,
24  path :: { Path, PathBuf },
25  borrow ::Cow,
26};
27// use error ::
28// {
29//   Result,
30// };
31use pth :: { AbsolutePath, Utf8Path, Utf8PathBuf };
32
33/// Path to a source file
34#[ derive( Clone, Ord, PartialOrd, Eq, PartialEq, Hash ) ]
35pub struct SourceFile( AbsolutePath );
36
37impl SourceFile
38{
39
40  /// Returns inner type which is an absolute path.
41  #[ inline( always ) ]
42  #[ must_use ]
43  pub fn inner( self ) -> AbsolutePath
44  {
45  self.0
46 }
47
48}
49
50impl fmt ::Display for SourceFile
51{
52  fn fmt( &self, f: &mut fmt ::Formatter< '_ > ) -> fmt ::Result
53  {
54  write!( f, "{}", self.0.display() )
55 }
56}
57
58impl fmt ::Debug for SourceFile
59{
60  fn fmt( &self, f: &mut fmt ::Formatter< '_ > ) -> fmt ::Result
61  {
62  write!( f, "source file :: {}", self.0.display() )
63 }
64}
65
66impl From< ManifestFile > for SourceFile
67{
68  fn from( src: ManifestFile ) -> Self
69  {
70  Self ( src.inner().parent().unwrap() )
71 }
72}
73
74impl From< SourceFile > for AbsolutePath
75{
76  fn from( src: SourceFile ) -> Self
77  {
78  src.inner()
79 }
80}
81
82impl From< SourceFile > for PathBuf
83{
84  fn from( src: SourceFile ) -> Self
85  {
86  src.inner().inner()
87 }
88}
89
90impl< 'a > TryFrom< &'a SourceFile > for &'a str
91{
92  type Error = std ::io ::Error;
93  fn try_from( src: &'a SourceFile ) -> Result< &'a str, Self ::Error >
94  {
95  ( &src.0 ).try_into()
96 }
97}
98
99impl TryFrom< &SourceFile > for String
100{
101  type Error = std ::io ::Error;
102  fn try_from( src: &SourceFile ) -> Result< String, Self ::Error >
103  {
104  let src2: &str = src.try_into()?;
105  Result ::Ok( src2.into() )
106 }
107}
108
109impl TryFrom< &AbsolutePath > for SourceFile
110{
111  type Error = PathError;
112
113  #[ inline( always ) ]
114  fn try_from( src: &AbsolutePath ) -> Result< Self, Self ::Error >
115  {
116  src.clone().try_into()
117 }
118}
119
120impl TryFrom< AbsolutePath > for SourceFile
121{
122  type Error = PathError;
123
124  #[ inline( always ) ]
125  fn try_from( src: AbsolutePath ) -> Result< Self, Self ::Error >
126  {
127  Result ::Ok( Self( src ) )
128 }
129}
130
131impl TryFrom< PathBuf > for SourceFile
132{
133  type Error = PathError;
134
135  #[ inline( always ) ]
136  fn try_from( src: PathBuf ) -> Result< Self, Self ::Error >
137  {
138  Self ::try_from( AbsolutePath ::try_from( src )? )
139 }
140}
141
142impl TryFrom< &Path > for SourceFile
143{
144  type Error = PathError;
145
146  #[ inline( always ) ]
147  fn try_from( src: &Path ) -> Result< Self, Self ::Error >
148  {
149  Self ::try_from( AbsolutePath ::try_from( src )? )
150 }
151}
152
153impl TryFrom< &str > for SourceFile
154{
155  type Error = PathError;
156
157  #[ inline( always ) ]
158  fn try_from( src: &str ) -> Result< Self, Self ::Error >
159  {
160  Self ::try_from( AbsolutePath ::try_from( src )? )
161 }
162}
163
164impl TryFrom< Utf8PathBuf > for SourceFile
165{
166  type Error = PathError;
167
168  #[ inline( always ) ]
169  fn try_from( src: Utf8PathBuf ) -> Result< Self, Self ::Error >
170  {
171  Self ::try_from( AbsolutePath ::try_from( src )? )
172 }
173}
174
175impl TryFrom< &Utf8PathBuf > for SourceFile
176{
177  type Error = PathError;
178
179  #[ inline( always ) ]
180  fn try_from( src: &Utf8PathBuf ) -> Result< Self, Self ::Error >
181  {
182  Self ::try_from( AbsolutePath ::try_from( src )? )
183 }
184}
185
186impl TryFrom< &Utf8Path > for SourceFile
187{
188  type Error = PathError;
189
190  #[ inline( always ) ]
191  fn try_from( src: &Utf8Path ) -> Result< Self, Self ::Error >
192  {
193  Self ::try_from( AbsolutePath ::try_from( src )? )
194 }
195}
196
197impl AsRef< Path > for SourceFile
198{
199  fn as_ref( &self ) -> &Path
200  {
201  self.0.as_ref()
202 }
203}
204
205impl AsMut< Path > for SourceFile
206{
207  fn as_mut( &mut self ) -> &mut Path
208  {
209  self.0.as_mut()
210 }
211}
212
213impl Deref for SourceFile
214{
215  type Target = AbsolutePath;
216  fn deref( &self ) -> &Self ::Target
217  {
218  &self.0
219 }
220}
221
222impl DerefMut for SourceFile
223{
224  fn deref_mut( &mut self ) -> &mut Self ::Target
225  {
226  &mut self.0
227 }
228}
229
230// =
231
232impl CodeItems for SourceFile
233{
234  fn items( &self ) -> impl IterTrait< '_, syn ::Item >
235  {
236  // xxx: use closures instead of expect
237  let content = fs ::read_to_string( self.as_ref() ).unwrap_or_else( | _ | panic!( "Failed to parse file {self}" ) );
238  let parsed: syn ::File = syn ::parse_file( &content ).unwrap_or_else( | _ |  panic!( "Failed to parse file {self}" ) );
239  parsed.items.into_iter()
240 }
241}
242
243impl AsCode for SourceFile
244{
245  fn as_code( &self ) -> std ::io ::Result< Cow< '_, str > >
246  {
247  std ::io ::Result ::Ok( Cow ::Owned( std ::fs ::read_to_string( self.as_ref() )? ) )
248 }
249}
250
251// =
252
253/// A trait that defines a method for retrieving an iterator over entries.
254///
255/// The `Entries` trait is used to represent objects that can provide an iterator over their
256/// contained entries, which are represented as source files. This can be useful in scenarios
257/// where you need to access or process all entries associated with an object.
258pub trait Entries
259{
260  /// Returns an iterator over the entries.
261  fn entries( &self ) -> impl IterTrait< '_, SourceFile >;
262}
263
264/// A trait that defines a method for retrieving an iterator over source files.
265///
266/// The `Sources` trait is used to represent objects that can provide an iterator over their
267/// contained source files. This can be useful in scenarios where you need to access or process
268/// all source files associated with an object.
269pub trait Sources
270{
271  /// Returns an iterator over the source files.
272  fn sources( &self ) -> impl IterTrait< '_, SourceFile >;
273}
274
275// =