SongRec/Internal formats/preferences.toml: Difference between revisions
Appearance
No edit summary |
|||
| (7 intermediate revisions by the same user not shown) | |||
| Line 3: | Line 3: | ||
It uses the [https://toml.io/fr/ TOML] format. | It uses the [https://toml.io/fr/ TOML] format. | ||
It is written through by the <code>[https://github.com/marin-m/SongRec/blob/main/src/core/preferences.rs src/core/preferences.rs]</code> utility file. | It is written through by the <code>[https://github.com/marin-m/SongRec/blob/main/src/core/preferences.rs src/core/preferences.rs]</code> utility file, which calculates the path of the preferences file through <code>[https://github.com/marin-m/SongRec/blob/main/src/utils/filesystem_operations.rs src/utils/filesystem_operations.rs]</code>. | ||
== Save location == | == Save location == | ||
( | The location for the <code>preferences.toml</code> file is calculated through the <code>[https://github.com/marin-m/SongRec/blob/7ab23d0/src/utils/filesystem_operations.rs#L31 obtain_preferences_file_path]</code> function of <code>[https://github.com/marin-m/SongRec/blob/7ab23d0/src/utils/filesystem_operations.rs src/utils/filesystem_operations.rs]</code>. | ||
Relevant code: | |||
<syntaxhighlight lang="rust"> | |||
use directories::ProjectDirs; | |||
use std::error::Error; | |||
use std::fs::create_dir_all; | |||
const QUALIFIER: &str = ""; | |||
const ORGANIZATION: &str = "SongRec"; | |||
const APPLICATION: &str = "SongRec"; | |||
static PROJECT_DIRS: LazyLock<ProjectDirs> = | |||
LazyLock::new(|| ProjectDirs::from(QUALIFIER, ORGANIZATION, APPLICATION).unwrap()); | |||
pub fn obtain_preferences_file_path() -> Result<PathBuf, Box<dyn Error>> { | |||
let mut preferences_file_path = obtain_preferences_directory()?; | |||
preferences_file_path.push("preferences.toml"); | |||
Ok(preferences_file_path) | |||
} | |||
fn obtain_preferences_directory() -> Result<PathBuf, Box<dyn Error>> { | |||
let preferences_dir = PROJECT_DIRS.preference_dir(); | |||
if !preferences_dir.exists() { | |||
create_dir_all(preferences_dir)?; | |||
} | |||
Ok(preferences_dir.to_path_buf()) | |||
} | |||
</syntaxhighlight> | |||
See: | |||
* https://docs.rs/directories/latest/directories/struct.ProjectDirs.html | |||
* https://docs.rs/directories/latest/directories/struct.ProjectDirs.html#method.from | |||
* https://docs.rs/directories/latest/directories/struct.ProjectDirs.html#method.preference_dir | |||
== Variables == | == Variables == | ||
( | The [https://github.com/marin-m/SongRec/blob/b738934/src/core/preferences.rs#L10 <code>Preferences</code> structure defined in <code>src/core/preferences.rs</code>] holds a list of the variables that can be serialized through the serde TOML plug-in: | ||
<syntaxhighlight lang="rust"> | |||
#[derive(Serialize, Deserialize, Clone, Debug)] | |||
#[serde(default)] | |||
pub struct Preferences { | |||
pub enable_notifications: Option<bool>, | |||
pub enable_systray: Option<bool>, | |||
pub enable_mpris: Option<bool>, // Legacy, before setting default to true | |||
pub enable_mpris_v2: Option<bool>, | |||
pub no_duplicates: Option<bool>, | |||
pub buffer_size_secs: Option<u64>, | |||
pub request_interval_secs: Option<u64>, // Legacy, before increasing default from 4 to 10 | |||
pub request_interval_secs_v2: Option<u64>, // before decreasing from 10 to 8 | |||
pub request_interval_secs_v3: Option<u64>, | |||
pub current_device_name: Option<String>, | |||
pub website_search_url: Option<String>, | |||
pub website_search_text: Option<String>, | |||
} | |||
</syntaxhighlight> | |||
With respective default values: | |||
<syntaxhighlight lang="rust"> | |||
impl Default for Preferences { | |||
fn default() -> Self { | |||
Preferences { | |||
enable_notifications: Some(true), | |||
enable_systray: Some(false), | |||
enable_mpris: None, | |||
enable_mpris_v2: Some(true), | |||
no_duplicates: Some(false), | |||
buffer_size_secs: Some(12), | |||
request_interval_secs: None, | |||
request_interval_secs_v2: None, | |||
request_interval_secs_v3: Some(8), | |||
current_device_name: None, | |||
website_search_url: Some("https://www.youtube.com/results?search_query=".to_string()), | |||
website_search_text: Some(gettext("Search on YouTube".to_string())), | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
Latest revision as of 04:23, 6 May 2026
The preferences.toml file of SongRec is saved, under Linux, at the ~/.config/songrec/preferences.toml path by default, when using an unsandboxed environment.
It uses the TOML format.
It is written through by the src/core/preferences.rs utility file, which calculates the path of the preferences file through src/utils/filesystem_operations.rs.
Save location
The location for the preferences.toml file is calculated through the obtain_preferences_file_path function of src/utils/filesystem_operations.rs.
Relevant code:
use directories::ProjectDirs;
use std::error::Error;
use std::fs::create_dir_all;
const QUALIFIER: &str = "";
const ORGANIZATION: &str = "SongRec";
const APPLICATION: &str = "SongRec";
static PROJECT_DIRS: LazyLock<ProjectDirs> =
LazyLock::new(|| ProjectDirs::from(QUALIFIER, ORGANIZATION, APPLICATION).unwrap());
pub fn obtain_preferences_file_path() -> Result<PathBuf, Box<dyn Error>> {
let mut preferences_file_path = obtain_preferences_directory()?;
preferences_file_path.push("preferences.toml");
Ok(preferences_file_path)
}
fn obtain_preferences_directory() -> Result<PathBuf, Box<dyn Error>> {
let preferences_dir = PROJECT_DIRS.preference_dir();
if !preferences_dir.exists() {
create_dir_all(preferences_dir)?;
}
Ok(preferences_dir.to_path_buf())
}
See:
- https://docs.rs/directories/latest/directories/struct.ProjectDirs.html
- https://docs.rs/directories/latest/directories/struct.ProjectDirs.html#method.from
- https://docs.rs/directories/latest/directories/struct.ProjectDirs.html#method.preference_dir
Variables
The Preferences structure defined in src/core/preferences.rs holds a list of the variables that can be serialized through the serde TOML plug-in:
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(default)]
pub struct Preferences {
pub enable_notifications: Option<bool>,
pub enable_systray: Option<bool>,
pub enable_mpris: Option<bool>, // Legacy, before setting default to true
pub enable_mpris_v2: Option<bool>,
pub no_duplicates: Option<bool>,
pub buffer_size_secs: Option<u64>,
pub request_interval_secs: Option<u64>, // Legacy, before increasing default from 4 to 10
pub request_interval_secs_v2: Option<u64>, // before decreasing from 10 to 8
pub request_interval_secs_v3: Option<u64>,
pub current_device_name: Option<String>,
pub website_search_url: Option<String>,
pub website_search_text: Option<String>,
}
With respective default values:
impl Default for Preferences {
fn default() -> Self {
Preferences {
enable_notifications: Some(true),
enable_systray: Some(false),
enable_mpris: None,
enable_mpris_v2: Some(true),
no_duplicates: Some(false),
buffer_size_secs: Some(12),
request_interval_secs: None,
request_interval_secs_v2: None,
request_interval_secs_v3: Some(8),
current_device_name: None,
website_search_url: Some("https://www.youtube.com/results?search_query=".to_string()),
website_search_text: Some(gettext("Search on YouTube".to_string())),
}
}
}