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
use crate::not_found;
use cap_std::fs::Dir;
use cap_std::AmbientAuthority;
use std::io;
/// `UserDirs` provides paths of user-facing standard directories, following
/// the conventions of the operating system the library is running on.
///
/// This corresponds to [`directories_next::UserDirs`], except that the
/// functions open the directories and returns `Dir`s instead of returning
/// `Path`s.
///
/// Unlike `directories_next::UserDirs`, the `*_dir` functions return `Dir`s
/// rather than `Path`s, because absolute paths don't interoperate well with
/// the capability model.
#[derive(Clone)]
pub struct UserDirs {
inner: directories_next::UserDirs,
}
impl UserDirs {
/// Creates a `UserDirs` struct which holds the paths to user-facing
/// directories for audio, font, video, etc. data on the system.
///
/// This corresponds to [`directories_next::UserDirs::new`].
pub fn new() -> Option<Self> {
let inner = directories_next::UserDirs::new()?;
Some(Self { inner })
}
/// Returns the user's home directory.
///
/// This corresponds to [`directories_next::UserDirs::home_dir`].
///
/// # Ambient Authority
///
/// This function makes use of ambient authority to access the user
/// directories.
pub fn home_dir(&self, ambient_authority: AmbientAuthority) -> io::Result<Dir> {
Dir::open_ambient_dir(self.inner.home_dir(), ambient_authority)
}
/// Returns the user's audio directory.
///
/// This corresponds to [`directories_next::UserDirs::audio_dir`].
///
/// # Ambient Authority
///
/// This function makes use of ambient authority to access the user
/// directories.
pub fn audio_dir(&self, ambient_authority: AmbientAuthority) -> io::Result<Dir> {
Dir::open_ambient_dir(
self.inner.audio_dir().ok_or_else(not_found)?,
ambient_authority,
)
}
/// Returns the user's desktop directory.
///
/// This corresponds to [`directories_next::UserDirs::desktop_dir`].
///
/// # Ambient Authority
///
/// This function makes use of ambient authority to access the user
/// directories.
pub fn desktop_dir(&self, ambient_authority: AmbientAuthority) -> io::Result<Dir> {
Dir::open_ambient_dir(
self.inner.desktop_dir().ok_or_else(not_found)?,
ambient_authority,
)
}
/// Returns the user's document directory.
///
/// This corresponds to [`directories_next::UserDirs::document_dir`].
///
/// # Ambient Authority
///
/// This function makes use of ambient authority to access the user
/// directories.
pub fn document_dir(&self, ambient_authority: AmbientAuthority) -> io::Result<Dir> {
Dir::open_ambient_dir(
self.inner.document_dir().ok_or_else(not_found)?,
ambient_authority,
)
}
/// Returns the user's download directory.
///
/// This corresponds to [`directories_next::UserDirs::download_dir`].
///
/// # Ambient Authority
///
/// This function makes use of ambient authority to access the user
/// directories.
pub fn download_dir(&self, ambient_authority: AmbientAuthority) -> io::Result<Dir> {
Dir::open_ambient_dir(
self.inner.download_dir().ok_or_else(not_found)?,
ambient_authority,
)
}
/// Returns the user's font directory.
///
/// This corresponds to [`directories_next::UserDirs::font_dir`].
///
/// # Ambient Authority
///
/// This function makes use of ambient authority to access the user
/// directories.
pub fn font_dir(&self, ambient_authority: AmbientAuthority) -> io::Result<Dir> {
Dir::open_ambient_dir(
self.inner.font_dir().ok_or_else(not_found)?,
ambient_authority,
)
}
/// Returns the user's picture directory.
///
/// This corresponds to [`directories_next::UserDirs::picture_dir`].
///
/// # Ambient Authority
///
/// This function makes use of ambient authority to access the user
/// directories.
pub fn picture_dir(&self, ambient_authority: AmbientAuthority) -> io::Result<Dir> {
Dir::open_ambient_dir(
self.inner.picture_dir().ok_or_else(not_found)?,
ambient_authority,
)
}
/// Returns the user's public directory.
///
/// This corresponds to [`directories_next::UserDirs::public_dir`].
///
/// # Ambient Authority
///
/// This function makes use of ambient authority to access the user
/// directories.
pub fn public_dir(&self, ambient_authority: AmbientAuthority) -> io::Result<Dir> {
Dir::open_ambient_dir(
self.inner.public_dir().ok_or_else(not_found)?,
ambient_authority,
)
}
/// Returns the user's template directory.
///
/// This corresponds to [`directories_next::UserDirs::template_dir`].
///
/// # Ambient Authority
///
/// This function makes use of ambient authority to access the user
/// directories.
pub fn template_dir(&self, ambient_authority: AmbientAuthority) -> io::Result<Dir> {
Dir::open_ambient_dir(
self.inner.template_dir().ok_or_else(not_found)?,
ambient_authority,
)
}
/// Returns the user's video directory.
///
/// This corresponds to [`directories_next::UserDirs::video_dir`].
///
/// # Ambient Authority
///
/// This function makes use of ambient authority to access the user
/// directories.
pub fn video_dir(&self, ambient_authority: AmbientAuthority) -> io::Result<Dir> {
Dir::open_ambient_dir(
self.inner.video_dir().ok_or_else(not_found)?,
ambient_authority,
)
}
}