crossplatform_path 4.1.3

Crossplatform Path - Rust library
Documentation
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
// crossplatform_path/src/lib.rs

// region: auto_md_to_doc_comments include README.md A //!
//! # crossplatform_path
//!
//! **Crossplatform Path - Rust library**  
//! ***version: 4.1.3 date: 2026-04-12 author: [bestia.dev](https://bestia.dev) repository: [codeberg.org](https://codeberg.org/bestia-dev-ready-for-use/crossplatform_path)***
//!
//!  ![maintained](https://img.shields.io/badge/maintained-green)
//!  ![ready-for-use](https://img.shields.io/badge/ready_for_use-green)
//!  ![rustlang](https://img.shields.io/badge/rustlang-orange)
//!  ![crossplatform](https://img.shields.io/badge/crossplatform-orange)
//!
//!   [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://codeberg.org/bestia-dev-ready-for-use/crossplatform_path/raw/branch/main/LICENSE)
//!   [![Repository](https://img.shields.io/badge/repo-Codeberg-blue.svg)](https://codeberg.org/bestia-dev-ready-for-use/crossplatform_path/)
//!   [![DocsDevelopment](https://img.shields.io/badge/docs-development-orange.svg)](https://bestia.dev/docs/bestia-dev-ready-for-use/crossplatform_path/)
//!   [![crates.io](https://img.shields.io/crates/v/crossplatform_path.svg)](https://crates.io/crates/crossplatform_path)
//!   [![DocsStable](https://img.shields.io/badge/docs-stable-green.svg)](https://docs.rs/crossplatform_path)
//!   ![crossplatform_path](https://bestia.dev/webpage_hit_counter/get_svg_image/1320456497.svg)
//!
//!  ![Lines in Rust code](https://img.shields.io/badge/Lines_in_Rust-98-green.svg)
//!  ![Lines in Doc comments](https://img.shields.io/badge/Lines_in_Doc_comments-261-blue.svg)
//!  ![Lines in Comments](https://img.shields.io/badge/Lines_in_comments-34-purple.svg)
//!  ![Lines in examples](https://img.shields.io/badge/Lines_in_examples-38-yellow.svg)
//!  ![Lines in tests](https://img.shields.io/badge/Lines_in_tests-422-orange.svg)
//!
//! Hashtags: #maintained #ready-for-use #rustlang #crossplatform  
//! My projects on Codeberg.org are more like a tutorial than a finished product: [bestia-dev tutorials](https://codeberg.org/bestia-dev-ready-for-use/tutorials_rust_wasm).  
//!
//! ## Motivation
//!
//! I have a few Rust projects that are compiled for Linux and Windows. I need to save some paths inside a config file. Windows have a strange way to work with file/folder paths. I need a library to work in a neutral crossplatform way. Only at the last line of code I transform the neutral path into something that the current OS recognizes.
//!
//! There exist already some libraries for that: relative-path, typed-path, x-path, camino,...
//!
//! But it is fun to make something new and simple and very very opinionated.
//!
//! ## Opinionated to the max
//!
//! My opinions are probably not useful for all developers, but they work for me and probably for most.
//!
//! 1. The path will be strictly utf8. I know that there exists a rare possibility of the path being in some other strange format, but I will never encounter that with this library. Or at least, I will always avoid that.
//! 2. Unix and Linux have paths that look nice. Windows is the problem here. The crossplatform format will be very much like the Linux paths.
//! 3. The only path separator will be the universal '/'. If some '\\' exists, it will be replaced by '/'. Linux allows the use of '\\' inside a filename, but my opinion is that this is bad and should be avoided.
//! 4. Linux is very permissive. Only NULL and '/' are forbidden in path components. But it is a good idea to not allow special characters forbidden on Windows:  
//!
//!     ```text
//!     < > : " / \\ | ? *
//!     0 (NULL byte)
//!     0-31 (ASCII control characters)  
//!     ```
//!   
//! 5. Filenames cannot end in a space or dot.
//! 6. Not allow reserved filenames even with extensions and foldernames:  
//!    CON, PRN, AUX, NUL  
//!    COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9  
//!    LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9  
//!    These names are not really needed and will not be allowed:  
//!    .  (special name referring to current directory)  
//!    This have to be avoided because of traversal attacks:  
//!    .. (special name referring to parent directory)  
//!
//! 7. Instead of the problematic Windows 'c:' or 'd:' drives,  
//!    the neutral crossplatform format will be '/mnt/c' or '/mnt/d'  
//!    From Windows:  
//!    c:\\ will be transformed into /mnt/c/  
//!    d:\\ will be transformed into /mnt/d/  
//! 8. This special symbols and root folders are allowed and will be transformed for Windows:  
//!    '~'    will be transformed into %UserProfile%  
//!    /tmp   will be transformed into %TEMP%  
//! 9. Definitely some paths in one OS have absolutely no meaning in other OS, but these have to be avoided manually.
//!
//! ## Usage
//!
//! ```rust
//! // cargo add crossplatform_path
//!
//! let cross_path = crossplatform_path::CrossPathBuf::new(r#"tmp\folder_for_testing"#)?.join_relative(r#"file_1.txt"#)?;
//! println!("{cross_path}");
//!
//! let linux_path_buf = cross_path.to_path_buf_nix();
//! println!("linux: {:?}", linux_path_buf);
//!
//! let win_path_buf = cross_path.to_path_buf_win();
//! println!("windows: {:?}", win_path_buf);
//!
//! let current_os_path_buf = cross_path.to_path_buf_current_os();
//! println!("current_os: {:?}", current_os_path_buf);
//!
//! println!("exists: {}", cross_path.exists());
//! println!("is_dir: {}", cross_path.is_dir());
//! println!("is_file: {}", cross_path.is_file());
//!
//! println!("parent: {}", cross_path.parent()?);
//! println!("file_name: {}", cross_path.file_name()?);
//! println!("file_stem: {}", cross_path.file_stem()?);
//! println!("extension: {}", cross_path.extension()?);
//!
//! println!("write_str_to_file");
//! cross_path.write_str_to_file("content for testing")?;
//!
//! let content = cross_path.read_to_string()?;
//! println!("read_to_string: {content}");
//!
//!
//! let cross_path = cross_path.add_start_slash()?.add_end_slash()?;
//! println!("add slashes {}", cross_path);
//!
//! let cross_path = cross_path.trim_start_slash()?.trim_end_slash()?;
//! println!("trim slashes {}", cross_path);
//!    
//! # Ok::<(), crossplatform_path::Error>(())
//! ```
//!
//! ## Development details
//!
//! Read the development details in a separate md file:
//! [DEVELOPMENT.md](DEVELOPMENT.md)
//!
//! ## Releases changelog
//!
//! Read the releases changelog in a separate md file:
//! [RELEASES.md](RELEASES.md)
//!
//! ## TODO
//!
//! - remove does not need to error on NotFound. It is ok, just as the remove was a success.
//!
//! And code happily ever after...
//!
//! ## Open-source and free as a beer
//!
//! My open-source projects are free as a beer (MIT license).  
//! I just love programming.  
//! But I need also to drink. If you find my projects and tutorials helpful, please buy me a beer by donating to my [PayPal](https://www.paypal.me/LucianoBestia).  
//! You know the price of a beer in your local bar ;-)  
//! So I can drink a free beer for your health :-)  
//! [Na zdravje!](https://translate.google.com/?hl=en&sl=sl&tl=en&text=Na%20zdravje&op=translate) [Alla salute!](https://dictionary.cambridge.org/dictionary/italian-english/alla-salute) [Prost!](https://dictionary.cambridge.org/dictionary/german-english/prost) [Nazdravlje!](https://matadornetwork.com/nights/how-to-say-cheers-in-50-languages/) 🍻
//!
//! [//bestia.dev](https://bestia.dev)  
//! [//codeberg.org/bestia-dev](https://codeberg.org/bestia-dev)  
//! [//bestiadev.substack.com](https://bestiadev.substack.com)  
//! [//youtube.com/@bestia-dev-tutorials](https://www.youtube.com/@bestia-dev-tutorials)  
//!
// endregion: auto_md_to_doc_comments include README.md A //!

use std::ffi::OsStr;

#[cfg(test)]
mod tests;

/// All possible library errors for `thiserror`.
#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error(r#"The string {0} contains an invalid windows path character < > : " | ? * "#)]
    InvalidCharacter(String),
    #[error(r#"The string {0} contains forbidden ascii control character for windows path 0-31 "#)]
    ForbiddenAscii(String),
    #[error(r#"The path string {0} must not end with space or dot "#)]
    MustNotEndWith(String),
    #[error(r#"The path string {0} must not contain reserved words con, prn, aux, nul, com1-com9, lpt1-lpt9, . and .."#)]
    ReservedWords(String),
    #[error(r#"The parent of {0} does not exist."#)]
    NoParent(String),
    #[error(r#"The file_name of {0} does not exist."#)]
    NoFileName(String),
    #[error(r#"char_indices().nth error {0}"#)]
    CharIndicesNthError(String),
    #[error("I/O error: {path} {source}")]
    IoError {
        #[source]
        source: std::io::Error,
        path: String,
    },
    #[error("Unknown error.")]
    Unknown,
}

/// crossplatform_path::Result
///
/// `crossplatform_path::Result` is used with just one parameter.
/// Instead of the regular Result with second parameter,
/// that is always crossplatform_path::Error in this library.
pub type Result<T, E = Error> = core::result::Result<T, E>;

/// CrossPathBuf stores Path in a Neutral Crossplatform format.
///
/// The neutral path is limited to valid utf8 strings.  \
/// This format can be stored in config files. It is "similar" to the Linux format, but not exactly equal.  \
/// When used for file operations, this Neutral format is converted into Linux or Windows format accordingly.  \
/// Some limitations exist for paths mostly because of Windows limitations:  \
/// forbidden characters < > : " / \\ | ? *  0 (NULL byte)  0-31 (ASCII control characters)  \
/// Filenames cannot end in a space or dot.  \
/// Separator is always slash. Backslash is replaced. Backslash must never be a part of a name or path component.  \
/// Must not contain reserved words con, prn, aux, nul, com1-com9, lpt1-lpt9, . and ..  \
/// If starts with windows c: or d: it is converted to /mnt/c or /mnt/d lowercase  
#[derive(Clone, Debug, PartialEq)]
pub struct CrossPathBuf {
    /// Path stored in a Neutral Crossplatform format.
    cross_path: String,
}

impl CrossPathBuf {
    /// Creates a new CrossPathBuf from &str.
    ///
    /// Path must be always utf8. Rust strings are guaranteed to be utf8.  \
    /// The input path will be tested that is somehow correct.  \
    /// It will be transformed from Windows into the crossplatform format. Linux format will stay mostly the same.  \
    /// The neutral path is limited to valid utf8 strings.  \
    /// This format can be stored in config files. It is "similar" to the Linux format, but not exactly equal.  \
    /// When used for file operations, this Neutral format is converted into Linux or Windows format accordingly.  \
    /// Some limitations exist for paths mostly because of Windows limitations:  \
    /// forbidden characters < > : " / \\ | ? *  0 (NULL byte)  0-31 (ASCII control characters)  \
    /// Filenames cannot end in a space or dot.  \
    /// Separator is always slash. Backslash is replaced. Backslash must never be a part of a name or path component.  \
    /// Must not contain reserved words con, prn, aux, nul, com1-com9, lpt1-lpt9, . and ..  \
    /// If start with windows c: or d: convert to /mnt/c or /mnt/d lowercase  
    pub fn new(str_path: &str) -> Result<Self> {
        // forbidden: < > : " / \\ | ? *  0 (NULL byte)  0-31 (ASCII control characters)
        // but : / and \\ are delimiters and can be used in a path fragment with multiple components.
        if str_path.contains("<")
            || str_path.contains(">")
            || str_path.contains(r#"""#)
            || str_path.contains("|")
            || str_path.contains("?")
            || str_path.contains("*")
        {
            return Err(Error::InvalidCharacter(str_path.to_string()));
        }
        // 0 (NULL byte) and  0-31 (ASCII control characters) 127 is DEL
        // Important: utf8 is always on a byte level compatible with ASCII7, under 127.
        for byte in str_path.bytes() {
            match byte {
                0x00..=0x1F | 0x7F => return Err(Error::ForbiddenAscii(str_path.to_string())),
                _ => (),
            }
        }
        //Filenames cannot end in a space or dot.
        if str_path.ends_with(" ") || str_path.ends_with(".") {
            return Err(Error::MustNotEndWith(str_path.to_string()));
        }

        // Separator is always slash. Backslash is replaced. Backslash must never be a part of a name or path component.
        // trim: leading and trailing whitespace removed
        let mut cross_path = str_path.trim().replace(r#"\"#, "/");

        // 6. Not allow reserved filenames even with extensions and foldernames:
        // Windows path is case insensitive, so I must check insensitive. I will use to_lowercase.
        // CON, PRN, AUX, NUL
        // COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9
        // LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9
        // These names are not really needed and will not be allowed:
        // .  (special name referring to current directory)
        // This have to be avoided because of traversal attacks:
        // .. (special name referring to parent directory)
        // I need to delimit start and end with / to use contains.
        let delimited_str_path = format!("/{}/", cross_path.trim_start_matches("/").trim_end_matches("/").to_lowercase());
        if delimited_str_path.contains("/con/")
            || delimited_str_path.contains("/prn/")
            || delimited_str_path.contains("/aux/")
            || delimited_str_path.contains("/nul/")
            || delimited_str_path.contains("/com1/")
            || delimited_str_path.contains("/com2/")
            || delimited_str_path.contains("/com3/")
            || delimited_str_path.contains("/com4/")
            || delimited_str_path.contains("/com5/")
            || delimited_str_path.contains("/com6/")
            || delimited_str_path.contains("/com7/")
            || delimited_str_path.contains("/com8/")
            || delimited_str_path.contains("/com9/")
            || delimited_str_path.contains("/lpt1/")
            || delimited_str_path.contains("/lpt2/")
            || delimited_str_path.contains("/lpt3/")
            || delimited_str_path.contains("/lpt4/")
            || delimited_str_path.contains("/lpt5/")
            || delimited_str_path.contains("/lpt6/")
            || delimited_str_path.contains("/lpt7/")
            || delimited_str_path.contains("/lpt8/")
            || delimited_str_path.contains("/lpt9/")
            || delimited_str_path.contains("/./")
            || delimited_str_path.contains("/../")
        {
            return Err(Error::ReservedWords(str_path.to_string()));
        }

        // If start with windows c: or d: convert to /mnt/c or /mnt/d lowercase
        let mut iter = cross_path.chars();
        if let Some(first) = iter.next()
            && let Some(second) = iter.next()
            && second == ':'
        {
            cross_path = format!("/mnt/{}{}", first.to_lowercase(), iter.as_str());
        }

        // Forbidden character, except for windows drive
        if cross_path.contains(":") {
            return Err(Error::InvalidCharacter(cross_path));
        }

        // Forbidden double slash
        if cross_path.contains("//") {
            return Err(Error::InvalidCharacter(cross_path));
        }

        Ok(CrossPathBuf { cross_path })
    }

    /// Creates a new CrossPathBuf from &Path.
    pub fn from_path(path: &std::path::Path) -> Result<Self> {
        let str_path = path
            .to_str()
            .ok_or_else(|| Error::InvalidCharacter(path.to_string_lossy().to_string()))?;
        Self::new(str_path)
    }

    /// Converts crossplatform path into Windows path.
    ///
    /// '~'     will be transformed into home  \
    /// /mnt/c/ will be transformed into c:\\  \
    /// /mnt/d/ will be transformed into d:\\  \
    /// /tmp    will be transformed into %TEMP%  
    pub fn to_path_buf_win(&self) -> std::path::PathBuf {
        let mut win_path = self.cross_path.clone();
        // '~'    will be transformed into home
        if win_path.starts_with("~")
            && let Some(home) = std::env::home_dir()
        {
            win_path = format!("{}{}", home.to_string_lossy(), win_path.trim_start_matches("~"));
        }
        // /mnt/c/ will be transformed into c:\\
        // /mnt/d/ will be transformed into d:\\
        if win_path.starts_with("/mnt/") {
            win_path = win_path.trim_start_matches("/mnt/").to_string();
            win_path.insert(1, ':');
        }
        // /tmp   will be transformed into %TEMP%
        if win_path.starts_with("/tmp") {
            let tmp_dir = std::env::temp_dir();
            win_path = format!("{}{}", tmp_dir.to_string_lossy(), win_path.trim_start_matches("/tmp"));
        }
        use std::str::FromStr;
        std::path::PathBuf::from_str(&win_path).expect("PathBuf::from_str() returns Infallible error. Therefore the error cannot occur.")
    }

    /// Converts crossplatform path into Linux path.
    ///
    /// '~'    will be transformed into home  
    pub fn to_path_buf_nix(&self) -> std::path::PathBuf {
        let mut nix_path = self.cross_path.clone();
        // '~'    will be transformed into home
        if nix_path.starts_with("~")
            && let Some(home) = std::env::home_dir()
        {
            nix_path = format!("{}{}", home.to_string_lossy(), nix_path.trim_start_matches("~"));
        }
        use std::str::FromStr;
        std::path::PathBuf::from_str(&nix_path).expect("PathBuf::from_str() returns Infallible error. Therefore the error cannot occur.")
    }

    /// Converts crossplatform path into current OS path.
    ///
    /// '~'     will be transformed into home  \
    /// /mnt/c/ will be transformed into c:\\  \
    /// /mnt/d/ will be transformed into d:\\  \
    /// /tmp    will be transformed into %TEMP%  
    pub fn to_path_buf_current_os(&self) -> std::path::PathBuf {
        if cfg!(windows) {
            self.to_path_buf_win()
        } else {
            self.to_path_buf_nix()
        }
    }

    /// Returns the crossplatform str for use in Display and store into config files.
    pub fn as_str(&self) -> &str {
        &self.cross_path
    }

    /// Returns `true` if the path points at an existing entity.
    pub fn exists(&self) -> bool {
        if cfg!(windows) {
            self.to_path_buf_win().exists()
        } else {
            self.to_path_buf_nix().exists()
        }
    }

    /// Returns `true` if the path exists on disk and is pointing at a regular file.
    pub fn is_file(&self) -> bool {
        if cfg!(windows) {
            self.to_path_buf_win().is_file()
        } else {
            self.to_path_buf_nix().is_file()
        }
    }

    /// Returns `true` if the path exists on disk and is pointing at a directory.
    pub fn is_dir(&self) -> bool {
        if cfg!(windows) {
            self.to_path_buf_win().is_dir()
        } else {
            self.to_path_buf_nix().is_dir()
        }
    }

    /// Joins two paths and returns a new CrossPathBuf to allow function chaining.
    ///
    /// It works differently from the original Rust join() where if the second path is absolute, it overwrites the first path.  \
    /// Here the second path is always relative and is added to the first path.
    pub fn join_relative(&self, str_path: &str) -> Result<Self> {
        let second_path = CrossPathBuf::new(str_path)?;
        let cross_path = format!(
            "{}/{}",
            self.cross_path.trim_end_matches("/"),
            second_path.as_str().trim_start_matches("/")
        );
        Ok(CrossPathBuf { cross_path })
    }

    /// Reads the entire contents of a file into a string.
    ///
    /// This is a convenience function based on std::fs::read_to_string  
    pub fn read_to_string(&self) -> Result<String> {
        let content = std::fs::read_to_string(self.to_path_buf_current_os()).map_err(|err| Error::IoError {
            source: (err),
            path: (self.cross_path.clone()),
        })?;
        Ok(content)
    }

    /// Writes a string slice as the entire contents of a file.
    ///
    /// This function will create a file if it does not exist, and will entirely replace its contents if it does.  \
    /// It creates the full path directory, if path does not exist.  
    pub fn write_str_to_file(&self, content: &str) -> Result<()> {
        self.create_dir_all_for_file()?;
        std::fs::write(self.to_path_buf_current_os(), content).map_err(|err| Error::IoError {
            source: (err),
            path: (self.cross_path.clone()),
        })?;
        Ok(())
    }

    /// Writes a byte slice as the entire contents of a file.
    ///
    /// This function will create a file if it does not exist, and will entirely replace its contents if it does.  \
    /// It creates the full path directory, if path does not exist.  
    pub fn write_bytes_to_file(&self, content: &[u8]) -> Result<()> {
        self.create_dir_all_for_file()?;
        std::fs::write(self.to_path_buf_current_os(), content).map_err(|err| Error::IoError {
            source: (err),
            path: (self.cross_path.clone()),
        })?;
        Ok(())
    }

    /// Recursively create this path as directory and all of its parent components if they are missing.
    ///
    /// The cross_path must represent a directory and not a file for this command. \
    /// This function is not atomic. If it returns an error, any parent components it was able to create will remain.   
    pub fn create_dir_all(&self) -> Result<()> {
        std::fs::create_dir_all(self.to_path_buf_current_os()).map_err(|err| Error::IoError {
            source: (err),
            path: (self.cross_path.clone()),
        })?;
        Ok(())
    }

    /// Recursively create the parent directory of a file and all of its parent components if they are missing.
    ///
    /// The cross_path must represent a file. The parent directory will be created. \
    /// This function is not atomic. If it returns an error, any parent components it was able to create will remain.   
    pub fn create_dir_all_for_file(&self) -> Result<()> {
        let path = self.to_path_buf_current_os();
        let parent = path.parent().ok_or_else(|| Error::NoParent(self.cross_path.clone()))?;
        std::fs::create_dir_all(parent).map_err(|err| Error::IoError {
            source: (err),
            path: (self.cross_path.clone()),
        })?;
        Ok(())
    }

    /// Returns a CrossPathBuf without leading start slash (repeatedly removed).  
    pub fn trim_start_slash(&self) -> Result<Self> {
        let cross_path = self.cross_path.trim_start_matches('/').trim().to_string();
        Ok(CrossPathBuf { cross_path })
    }

    /// Returns a CrossPathBuf without trailing end slash (repeatedly removed).  
    pub fn trim_end_slash(&self) -> Result<Self> {
        let cross_path = self.cross_path.trim_end_matches('/').trim().to_string();
        Ok(CrossPathBuf { cross_path })
    }

    /// Returns a CrossPathBuf with one leading start slash.  
    pub fn add_start_slash(&self) -> Result<Self> {
        let cross_path = format!("/{}", self.cross_path.trim_start_matches('/').trim());
        Ok(CrossPathBuf { cross_path })
    }

    /// Returns a CrossPathBuf with one trailing end slash.  
    pub fn add_end_slash(&self) -> Result<Self> {
        let cross_path = format!("{}/", self.cross_path.trim_end_matches('/').trim());
        Ok(CrossPathBuf { cross_path })
    }

    /// Returns the final component of the Path, if there is one.
    ///
    /// If the path is a normal file, this is the file name.
    /// If it's the path of a directory, this is the directory name.
    pub fn file_name(&self) -> Result<String> {
        let file_name = self
            .to_path_buf_current_os()
            .file_name()
            .ok_or_else(|| Error::NoFileName(self.cross_path.clone()))?
            .to_string_lossy()
            .to_string();
        Ok(file_name)
    }

    /// Extracts the extension (without the leading dot), if possible.
    ///
    /// It is different from the std::fs extension() because  \
    /// it returns an empty string if there is no extension.  \
    /// It returns Error only if there is no file_name.  
    pub fn extension(&self) -> Result<String> {
        // return error if there is no file_name
        let _file_name = self
            .to_path_buf_current_os()
            .file_name()
            .ok_or_else(|| Error::NoFileName(self.cross_path.clone()))?;

        let file_extension = self
            .to_path_buf_current_os()
            .extension()
            // return empty string if there is no extension
            .unwrap_or_else(|| OsStr::new(""))
            .to_string_lossy()
            .to_string();
        Ok(file_extension)
    }

    /// Extracts the stem (non-extension) portion of file_name (the final component of the Path).
    pub fn file_stem(&self) -> Result<String> {
        let file_stem = self
            .to_path_buf_current_os()
            .file_stem()
            .ok_or_else(|| Error::NoFileName(self.cross_path.clone()))?
            .to_string_lossy()
            .to_string();
        Ok(file_stem)
    }

    /// Returns the Path without its final component, if there is one.
    pub fn parent(&self) -> Result<Self> {
        CrossPathBuf::new(
            &self
                .to_path_buf_current_os()
                .parent()
                .ok_or_else(|| Error::NoParent(self.cross_path.clone()))?
                .to_string_lossy(),
        )
    }

    /// Returns new object where the extension is replaced.
    ///
    /// If the extension did not exist, it is added.
    pub fn replace_extension(&self, extension: &str) -> Result<Self> {
        let old_extension = self.extension()?;
        let dot_separator = if extension.is_empty() { "" } else { "." };
        let cross_path = format!(
            "{}{dot_separator}{extension}",
            self.cross_path.trim_end_matches(&old_extension).trim_end_matches(".")
        );
        CrossPathBuf::new(&cross_path)
    }

    /// Returns new object with added extension.
    pub fn add_extension(&self, extension: &str) -> Result<Self> {
        let cross_path = format!("{}.{extension}", self.cross_path);
        CrossPathBuf::new(&cross_path)
    }

    /// Shorten the crossplatform path to avoid word-wrap for longer paths.
    pub fn short_string(&self, max_char: u16) -> Result<String> {
        /// It is used for substring, because string slice are counted in bytes and not chars.
        ///
        /// If we have multi-byte unicode characters we can get an error if the boundary is not on char boundary.  
        fn byte_pos_from_chars(text: &str, char_pos: usize) -> Result<usize> {
            Ok(text
                .char_indices()
                .nth(char_pos)
                .ok_or_else(|| Error::NoFileName(text.to_string()))?
                .0)
        }
        if self.cross_path.chars().count() > max_char as usize {
            let half_in_char = (max_char / 2 - 2) as usize;
            let pos1_in_bytes = byte_pos_from_chars(&self.cross_path, half_in_char)?;
            let pos2_in_bytes = byte_pos_from_chars(&self.cross_path, self.cross_path.chars().count() - half_in_char)?;
            Ok(format!(
                "{}...{}",
                &self.cross_path[..pos1_in_bytes],
                &self.cross_path[pos2_in_bytes..]
            ))
        } else {
            Ok(self.cross_path.to_string())
        }
    }

    /// Decompress tar.gz into destination folder.
    ///
    /// It creates the full path destination folder, if path does not exist.  
    pub fn decompress_tar_gz(&self, destination_folder: &CrossPathBuf) -> Result<()> {
        destination_folder.create_dir_all()?;

        let tar_gz = std::fs::File::open(self.to_path_buf_current_os()).map_err(|err| Error::IoError {
            source: (err),
            path: (self.cross_path.clone()),
        })?;
        let tar = flate2::read::GzDecoder::new(tar_gz);
        let mut archive = tar::Archive::new(tar);
        archive
            .unpack(destination_folder.to_path_buf_current_os())
            .map_err(|err| Error::IoError {
                source: (err),
                path: (self.cross_path.clone()),
            })?;

        Ok(())
    }

    /// Removes a file from the filesystem.
    ///
    /// DIFFERENCE from std::fs::remove_file: The file you are deleting does not need to exist.
    /// Note that there is no guarantee that the file is immediately deleted (e.g., depending on platform, other open file descriptors may prevent immediate removal).  
    pub fn remove_file(&self) -> Result<()> {
        if std::fs::exists(self.to_path_buf_current_os()).map_err(|err| Error::IoError {
            source: (err),
            path: (self.cross_path.clone()),
        })? {
            std::fs::remove_file(self.to_path_buf_current_os()).map_err(|err| Error::IoError {
                source: (err),
                path: (self.cross_path.clone()),
            })?;
        }
        Ok(())
    }

    /// Removes a directory at this path, after removing all its contents. Use carefully!
    ///
    /// DIFFERENCE from std::fs::remove_dir_all: The directory you are deleting does not need to exist.
    /// This function does not follow symbolic links and it will simply remove the symbolic link itself. \
    pub fn remove_dir_all(&self) -> Result<()> {
        if std::fs::exists(self.to_path_buf_current_os()).map_err(|err| Error::IoError {
            source: (err),
            path: (self.cross_path.clone()),
        })? {
            std::fs::remove_dir_all(self.to_path_buf_current_os()).map_err(|err| Error::IoError {
                source: (err),
                path: (self.cross_path.clone()),
            })?;
        }
        Ok(())
    }

    /// Copies the contents of one file to another file.
    ///
    /// This function will also copy the permission bits of the original file to the destination file. \
    /// It creates the full path destination folder, if path does not exist. \
    /// DIFFERENCE from std::fs::copy If the source and destination is the same nothing happens.
    pub fn copy_file_to_file(&self, destination_file: &CrossPathBuf) -> Result<()> {
        if self.to_path_buf_current_os() != destination_file.to_path_buf_current_os() {
            destination_file.create_dir_all_for_file()?;
            std::fs::copy(self.to_path_buf_current_os(), destination_file.to_path_buf_current_os()).map_err(|err| Error::IoError {
                source: (err),
                path: (self.cross_path.clone()),
            })?;
        }
        Ok(())
    }

    /// Renames a file or directory to a new name, replacing the original file if to already exists.
    pub fn rename_or_move(&self, destination_file: &CrossPathBuf) -> Result<()> {
        if self.to_path_buf_current_os() != destination_file.to_path_buf_current_os() {
            destination_file.create_dir_all_for_file()?;
            std::fs::rename(self.to_path_buf_current_os(), destination_file.to_path_buf_current_os()).map_err(|err| Error::IoError {
                source: (err),
                path: (self.cross_path.clone()),
            })?;
        }
        Ok(())
    }
}

/// Method display() is used in format!("{}").
impl std::fmt::Display for CrossPathBuf {
    /// Method display() is used in format!("{}").
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        std::fmt::Display::fmt(self.as_str(), f)
    }
}

/// CrossPathBuf from() and into() are useful in places where PathBuf is needed.
impl From<CrossPathBuf> for std::path::PathBuf {
    /// CrossPathBuf from() and into() are useful in places where PathBuf is needed.
    fn from(cross_path: CrossPathBuf) -> Self {
        cross_path.to_path_buf_current_os()
    }
}

// TODO: is it possible to impl AsRef<Path>?
// many functions accept AsRef<Path>
// problem: I cannot create a PathBuf inside as_ref() because
// then cannot return value referencing temporary value
/* impl AsRef<std::path::Path> for CrossPathBuf {
    #[inline]
    fn as_ref(&self) -> &std::path::Path {
        self.to_current_os_path_buf().as_path()
    }
}
 */