From 72c44056b2c7c7d7fbc966d7fc3aebc612559ad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Fri, 25 Apr 2025 09:20:44 +0200 Subject: [PATCH] utils: Make data_dir_path a method of DataType It reduces the necessary imports. --- src/secret/mod.rs | 8 ++++---- src/session_list/mod.rs | 8 +++++--- src/utils/mod.rs | 26 ++++++++++++++------------ 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/secret/mod.rs b/src/secret/mod.rs index 3db0b021..8d2c3b6c 100644 --- a/src/secret/mod.rs +++ b/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 { // 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 } diff --git a/src/session_list/mod.rs b/src/session_list/mod.rs index 10e29afa..fbbcffdc 100644 --- a/src/session_list/mod.rs +++ b/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> { - let data_path = data_dir_path(DataType::Persistent); + let data_path = DataType::Persistent.dir_path(); if !data_path.try_exists()? { return Ok(Vec::new()); diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 4392389a..d1f01b78 100644 --- a/src/utils/mod.rs +++ b/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