refactor(bar): inline defaults in schemars attrs

This commit is contained in:
LGUG2Z
2026-01-04 19:08:34 -08:00
parent b792328676
commit 1219c3d118
8 changed files with 30 additions and 87 deletions
+6 -18
View File
@@ -16,12 +16,6 @@ use std::time::Duration;
use std::time::Instant;
use sysinfo::Disks;
mod defaults {
pub const DATA_REFRESH_INTERVAL: u64 = 10;
pub const SHOW_READ_ONLY_DISKS: bool = false;
pub const SHOW_REMOVABLE_DISKS: bool = true;
}
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
/// Storage widget configuration
@@ -29,15 +23,15 @@ pub struct StorageConfig {
/// Enable the Storage widget
pub enable: bool,
/// Data refresh interval in seconds
#[cfg_attr(feature = "schemars", schemars(extend("default" = defaults::DATA_REFRESH_INTERVAL)))]
#[cfg_attr(feature = "schemars", schemars(extend("default" = 10)))]
pub data_refresh_interval: Option<u64>,
/// Display label prefix
pub label_prefix: Option<LabelPrefix>,
/// Show disks that are read only
#[cfg_attr(feature = "schemars", schemars(extend("default" = defaults::SHOW_READ_ONLY_DISKS)))]
#[cfg_attr(feature = "schemars", schemars(extend("default" = false)))]
pub show_read_only_disks: Option<bool>,
/// Show removable disks
#[cfg_attr(feature = "schemars", schemars(extend("default" = defaults::SHOW_REMOVABLE_DISKS)))]
#[cfg_attr(feature = "schemars", schemars(extend("default" = true)))]
pub show_removable_disks: Option<bool>,
/// Select when the current percentage is over this value [[1-100]]
pub auto_select_over: Option<u8>,
@@ -50,16 +44,10 @@ impl From<StorageConfig> for Storage {
Self {
enable: value.enable,
disks: Disks::new_with_refreshed_list(),
data_refresh_interval: value
.data_refresh_interval
.unwrap_or(defaults::DATA_REFRESH_INTERVAL),
data_refresh_interval: value.data_refresh_interval.unwrap_or(10),
label_prefix: value.label_prefix.unwrap_or(LabelPrefix::IconAndText),
show_read_only_disks: value
.show_read_only_disks
.unwrap_or(defaults::SHOW_READ_ONLY_DISKS),
show_removable_disks: value
.show_removable_disks
.unwrap_or(defaults::SHOW_REMOVABLE_DISKS),
show_read_only_disks: value.show_read_only_disks.unwrap_or(false),
show_removable_disks: value.show_removable_disks.unwrap_or(true),
auto_select_over: value.auto_select_over.map(|o| o.clamp(1, 100)),
auto_hide_under: value.auto_hide_under.map(|o| o.clamp(1, 100)),
last_updated: Instant::now(),