egui_file_dialog/data/
user_directories.rs

1use std::path::{Path, PathBuf};
2
3/// Wrapper above `directories::UserDirs`.
4/// Currently only used to canonicalize the paths.
5#[derive(Default, Clone, Debug)]
6pub struct UserDirectories {
7    home_dir: Option<PathBuf>,
8
9    audio_dir: Option<PathBuf>,
10    desktop_dir: Option<PathBuf>,
11    document_dir: Option<PathBuf>,
12    download_dir: Option<PathBuf>,
13    picture_dir: Option<PathBuf>,
14    video_dir: Option<PathBuf>,
15}
16
17impl UserDirectories {
18    /// Creates a new custom `UserDirectories` object
19    pub const fn new(
20        home_dir: Option<PathBuf>,
21        audio_dir: Option<PathBuf>,
22        desktop_dir: Option<PathBuf>,
23        document_dir: Option<PathBuf>,
24        download_dir: Option<PathBuf>,
25        picture_dir: Option<PathBuf>,
26        video_dir: Option<PathBuf>,
27    ) -> Self {
28        Self {
29            home_dir,
30            audio_dir,
31            desktop_dir,
32            document_dir,
33            download_dir,
34            picture_dir,
35            video_dir,
36        }
37    }
38
39    pub(crate) fn home_dir(&self) -> Option<&Path> {
40        self.home_dir.as_deref()
41    }
42
43    pub(crate) fn audio_dir(&self) -> Option<&Path> {
44        self.audio_dir.as_deref()
45    }
46
47    pub(crate) fn desktop_dir(&self) -> Option<&Path> {
48        self.desktop_dir.as_deref()
49    }
50
51    pub(crate) fn document_dir(&self) -> Option<&Path> {
52        self.document_dir.as_deref()
53    }
54
55    pub(crate) fn download_dir(&self) -> Option<&Path> {
56        self.download_dir.as_deref()
57    }
58
59    pub(crate) fn picture_dir(&self) -> Option<&Path> {
60        self.picture_dir.as_deref()
61    }
62
63    pub(crate) fn video_dir(&self) -> Option<&Path> {
64        self.video_dir.as_deref()
65    }
66
67    /// Canonicalizes the given paths. Returns None if an error occurred.
68    pub(crate) fn canonicalize(path: Option<&Path>, canonicalize: bool) -> Option<PathBuf> {
69        if !canonicalize {
70            return path.map(PathBuf::from);
71        }
72
73        if let Some(path) = path {
74            return dunce::canonicalize(path).ok();
75        }
76
77        None
78    }
79}