Browse Source

utils: Make data_dir_path a method of DataType

It reduces the necessary imports.
merge-requests/2003/head
Kévin Commaille 11 months ago
parent
commit
72c44056b2
No known key found for this signature in database
GPG Key ID: C971D9DBC9D678D
  1. 8
      src/secret/mod.rs
  2. 8
      src/session_list/mod.rs
  3. 26
      src/utils/mod.rs

8
src/secret/mod.rs

@ -23,7 +23,7 @@ use self::file::SecretFile;
use crate::{
prelude::*,
spawn_tokio,
utils::{data_dir_path, matrix::ClientSetupError, DataType},
utils::{matrix::ClientSetupError, DataType},
};
/// The length of a session ID, in chars or bytes as the string is ASCII.
@ -132,7 +132,7 @@ impl StoredSession {
pub(crate) async fn new(client: &Client) -> Result<Self, ClientSetupError> {
// Generate a unique random session ID.
let mut id = None;
let data_path = data_dir_path(DataType::Persistent);
let data_path = DataType::Persistent.dir_path();
// Try 10 times, so we do not have an infinite loop.
for _ in 0..10 {
@ -180,7 +180,7 @@ impl StoredSession {
/// The path where the persistent data of this session lives.
pub(crate) fn data_path(&self) -> PathBuf {
let mut path = data_dir_path(DataType::Persistent);
let mut path = DataType::Persistent.dir_path();
path.push(&self.id);
path
}
@ -201,7 +201,7 @@ impl StoredSession {
/// The path where the cached data of this session lives.
pub(crate) fn cache_path(&self) -> PathBuf {
let mut path = data_dir_path(DataType::Cache);
let mut path = DataType::Cache.dir_path();
path.push(&self.id);
path
}

8
src/session_list/mod.rs

@ -10,13 +10,15 @@ mod new_session;
mod session_info;
mod session_list_settings;
pub use self::{failed_session::*, new_session::*, session_info::*, session_list_settings::*};
pub(crate) use self::{
failed_session::*, new_session::*, session_info::*, session_list_settings::*,
};
use crate::{
prelude::*,
secret::{Secret, StoredSession},
session::model::{Session, SessionState},
spawn, spawn_tokio,
utils::{data_dir_path, DataType, LoadingState},
utils::{DataType, LoadingState},
};
mod imp {
@ -238,7 +240,7 @@ mod imp {
/// The list of directories in the data directory.
async fn data_directories(&self, capacity: usize) -> std::io::Result<Vec<OsString>> {
let data_path = data_dir_path(DataType::Persistent);
let data_path = DataType::Persistent.dir_path();
if !data_path.try_exists()? {
return Ok(Vec::new());

26
src/utils/mod.rs

@ -42,18 +42,6 @@ pub(crate) use self::{
};
use crate::{PROFILE, RUNTIME};
/// The path of the directory where data should be stored, depending on its
/// type.
pub(crate) fn data_dir_path(data_type: DataType) -> PathBuf {
let mut path = match data_type {
DataType::Persistent => glib::user_data_dir(),
DataType::Cache => glib::user_cache_dir(),
};
path.push(PROFILE.dir_name().as_ref());
path
}
/// The type of data.
#[derive(Debug, Clone, Copy)]
pub(crate) enum DataType {
@ -63,6 +51,20 @@ pub(crate) enum DataType {
Cache,
}
impl DataType {
/// The path of the directory where data should be stored, depending on this
/// type.
pub(crate) fn dir_path(self) -> PathBuf {
let mut path = match self {
DataType::Persistent => glib::user_data_dir(),
DataType::Cache => glib::user_cache_dir(),
};
path.push(PROFILE.dir_name().as_ref());
path
}
}
/// Replace variables in the given string with the given dictionary.
///
/// The expected format to replace is `{name}`, where `name` is the first string

Loading…
Cancel
Save