1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/// Internal namespace.
mod private
{

  use crate::*;

  use std::
  {
    // borrow::Cow,
    path::{ Path, PathBuf },
    io,
  };

  use core::
  {
    fmt,
    ops::
    {
      Deref,
      DerefMut,
    },
  };

  #[ cfg( feature="no_std" ) ]
  extern crate std;
  
  #[ cfg( feature="no_std" ) ]
  use alloc::string::String;

  #[ cfg( feature = "derive_serde" ) ]
  use serde::{ Serialize, Deserialize };

  #[ cfg( feature = "path_utf8" ) ]
  use camino::{ Utf8Path, Utf8PathBuf };

  /// Absolute path.
  #[ cfg_attr( feature = "derive_serde", derive( Serialize, Deserialize ) ) ]
  #[ derive( Debug, Default, Clone, Ord, PartialOrd, Eq, PartialEq, Hash ) ]
  pub struct AbsolutePath( PathBuf );

  impl AbsolutePath
  {

    /// Returns the Path without its final component, if there is one.
    /// Returns None if the path terminates in a root or prefix, or if it's the empty string.
    #[ inline ]
    pub fn parent( &self ) -> Option< AbsolutePath >
    {
      self.0.parent().map( PathBuf::from ).map( AbsolutePath )
    }

    /// Creates an owned `AbsolutePath` with path adjoined to self.
    #[ inline ]
    pub fn join< P >( &self, path : P ) -> AbsolutePath
    where
      P : AsRef< Path >,
    {
      Self::try_from( self.0.join( path ) ).unwrap()
    }

    // /// Converts a `AbsolutePath` to a `Cow<str>`
    // pub fn to_string_lossy( &self ) -> Cow< '_, str >
    // {
    //   self.0.to_string_lossy()
    // }

    /// Determines whether base is a prefix of self.
    ///
    /// Only considers whole path components to match.
    #[ inline ]
    pub fn starts_with< P : AsRef< Path > >( &self, base : P ) -> bool
    {
      self.0.starts_with( base )
    }

    /// Returns inner type which is PathBuf.
    #[ inline( always ) ]
    pub fn inner( self ) -> PathBuf
    {
      self.0
    }

  }

  impl fmt::Display for AbsolutePath
  {
    #[ inline ]
    fn fmt( &self, f : &mut fmt::Formatter<'_> ) -> fmt::Result
    {
      write!( f, "{}", self.0.display() )
    }
  }

  #[ inline ]
  fn is_absolute( path : &Path ) -> bool
  {
    // None - not absolute
    // with `.` or `..` at the first component - not absolute
    !path.components().next().is_some_and( | c | c.as_os_str() == "." || c.as_os_str() == ".." )
  }

  impl TryFrom< PathBuf > for AbsolutePath
  {
    type Error = std::io::Error;

    #[ inline ]
    fn try_from( src : PathBuf ) -> Result< Self, Self::Error >
    {
      < Self as TryFrom< &Path > >::try_from( &src.as_path() )
    }
  }

  impl TryFrom< &PathBuf > for AbsolutePath
  {
    type Error = std::io::Error;

    #[ inline ]
    fn try_from( src : &PathBuf ) -> Result< Self, Self::Error >
    {
      < Self as TryFrom< &Path > >::try_from( &src.as_path() )
    }
  }

  // xxx : qqq : use Into< Path >
  impl TryFrom< &Path > for AbsolutePath
  {
    type Error = std::io::Error;

    #[ inline ]
    fn try_from( src : &Path ) -> Result< Self, Self::Error >
    {
      // < Self as TryFrom< &str > >::try_from( src.to_string_lossy() )
      let path = path::canonicalize( src )?;

      // xxx
      if !is_absolute( &path )
      {
        return Err( io::Error::new( io::ErrorKind::InvalidData, "Path expected to be absolute, but it's not {path}" ) )
      }

      Ok( Self( path ) )
    }
  }

  impl< 'a > TryFrom< &'a str > for AbsolutePath
  {
    type Error = std::io::Error;

    #[ inline ]
    fn try_from( src : &'a str ) -> Result< Self, Self::Error >
    {
      < Self as TryFrom< &Path > >::try_from( src.as_ref() )
    }
  }

//   impl TryFrom< &str > for AbsolutePath
//   {
//     type Error = std::io::Error;
//     // type Error = PathError;
//
//     #[ inline( always ) ]
//     fn try_from( src : &str ) -> Result< Self, Self::Error >
//     {
//       Self::try_from( AbsolutePath::try_from( src )? )
//     }
//   }

  #[ cfg( feature = "path_utf8" ) ]
  impl TryFrom< Utf8PathBuf > for AbsolutePath
  {
    type Error = std::io::Error;

    #[ inline ]
    fn try_from( src : Utf8PathBuf ) -> Result< Self, Self::Error >
    {
      AbsolutePath::try_from( src.as_std_path() )
    }
  }

  #[ cfg( feature = "path_utf8" ) ]
  impl TryFrom< &Utf8PathBuf > for AbsolutePath
  {
    type Error = std::io::Error;

    #[ inline ]
    fn try_from( src : &Utf8PathBuf ) -> Result< Self, Self::Error >
    {
      AbsolutePath::try_from( src.as_std_path() )
    }
  }

  #[ cfg( feature = "path_utf8" ) ]
  impl TryFrom< &Utf8Path > for AbsolutePath
  {
    type Error = std::io::Error;

    #[ inline ]
    fn try_from( src : &Utf8Path ) -> Result< Self, Self::Error >
    {
      AbsolutePath::try_from( src.as_std_path() )
    }
  }

  impl From< AbsolutePath > for PathBuf
  {
    #[ inline ]
    fn from( src : AbsolutePath ) -> Self
    {
      src.0
    }
  }

  impl< 'a > TryFrom< &'a AbsolutePath > for &'a str
  {
    type Error = std::io::Error;
    #[ inline ]
    fn try_from( src : &'a AbsolutePath ) -> Result< &'a str, Self::Error >
    {
      src
      .to_str()
      .ok_or_else
      (
        move || io::Error::new( io::ErrorKind::Other, format!( "Can't convert &PathBuf into &str {src}" ) )
      )
    }
  }

  impl TryFrom< &AbsolutePath > for String
  {
    type Error = std::io::Error;
    #[ inline ]
    fn try_from( src : &AbsolutePath ) -> Result< String, Self::Error >
    {
      let src2 : &str = src.try_into()?;
      Ok( src2.into() )
    }
  }

//   impl TryFrom< Utf8PathBuf > for AbsolutePath
//   {
//     type Error = std::io::Error;
//
//     fn try_from( src : Utf8PathBuf ) -> Result< Self, Self::Error >
//     {
//       AbsolutePath::try_from( src.as_std_path() )
//     }
//   }

//   impl TryFrom< &Utf8Path > for AbsolutePath
//   {
//     type Error = std::io::Error;
//
//     fn try_from( src : &Utf8Path ) -> Result< Self, Self::Error >
//     {
//       AbsolutePath::try_from( src.as_std_path() )
//     }
//   }

  // // xxx : use derives
  // impl AsRef< Path > for AbsolutePath
  // {
  //   fn as_ref( &self ) -> &Path
  //   {
  //     self.0.as_ref()
  //   }
  // }

  impl AsRef< Path > for AbsolutePath
  {
    #[ inline ]
    fn as_ref( &self ) -> &Path
    {
      self.0.as_ref()
    }
  }

  impl AsMut< Path > for AbsolutePath
  {
    #[ inline ]
    fn as_mut( &mut self ) -> &mut Path
    {
      &mut self.0
    }
  }

  impl Deref for AbsolutePath
  {
    type Target = Path;
    #[ inline ]
    fn deref( &self ) -> &Self::Target
    {
      &self.0
    }
  }

  impl DerefMut for AbsolutePath
  {
    #[ inline ]
    fn deref_mut( &mut self ) -> &mut Self::Target
    {
      &mut self.0
    }
  }

//   /// Convertable into absolute path entity should implement the trait.
//   pub trait TryIntoAbsolutePath
//   {
//     /// Error returned if conversion is failed.
//     type Error;
//     /// Method to convert the type into absolute path.
//     fn into_absolute_path( self ) -> Result< AbsolutePath, Self::Error >;
//   }
//
//   // impl TryIntoAbsolutePath for AbsolutePath
//   // {
//   //   type Error = std::io::Error;
//   //   #[ inline ]
//   //   fn into_absolute_path( self ) -> Result< AbsolutePath, Self::Error >
//   //   {
//   //     Ok( self )
//   //   }
//   // }
//
//   impl< TryIntoAbsolutePathType > TryIntoAbsolutePath for TryIntoAbsolutePathType
//   where
//     TryIntoAbsolutePathType : TryInto< AbsolutePath >,
//   {
//     type Error = < Self as TryInto< AbsolutePath > >::Error;
//     #[ inline ]
//     fn into_absolute_path( self ) -> Result< AbsolutePath, Self::Error >
//     {
//       self.try_into()
//     }
//   }

}

crate::mod_interface!
{
  exposed use AbsolutePath;
  // exposed use TryIntoAbsolutePath;
}