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
+2 -8
View File
@@ -17,10 +17,6 @@ use std::process::Command;
use std::time::Duration;
use std::time::Instant;
mod defaults {
pub const DATA_REFRESH_INTERVAL: u64 = 10;
}
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
/// Battery widget configuration
@@ -30,7 +26,7 @@ pub struct BatteryConfig {
/// Hide the widget if the battery is at full charge
pub hide_on_full_charge: Option<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>,
@@ -40,9 +36,7 @@ pub struct BatteryConfig {
impl From<BatteryConfig> for Battery {
fn from(value: BatteryConfig) -> Self {
let data_refresh_interval = value
.data_refresh_interval
.unwrap_or(defaults::DATA_REFRESH_INTERVAL);
let data_refresh_interval = value.data_refresh_interval.unwrap_or(10);
Self {
enable: value.enable,
+2 -8
View File
@@ -16,10 +16,6 @@ use std::time::Instant;
use sysinfo::RefreshKind;
use sysinfo::System;
mod defaults {
pub const DATA_REFRESH_INTERVAL: u64 = 10;
}
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
/// CPU widget configuration
@@ -27,7 +23,7 @@ pub struct CpuConfig {
/// Enable the Cpu 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>,
@@ -37,9 +33,7 @@ pub struct CpuConfig {
impl From<CpuConfig> for Cpu {
fn from(value: CpuConfig) -> Self {
let data_refresh_interval = value
.data_refresh_interval
.unwrap_or(defaults::DATA_REFRESH_INTERVAL);
let data_refresh_interval = value.data_refresh_interval.unwrap_or(10);
Self {
enable: value.enable,
+4 -9
View File
@@ -21,10 +21,7 @@ use windows::Win32::UI::Input::KeyboardAndMouse::GetKeyboardLayout;
use windows::Win32::UI::WindowsAndMessaging::GetForegroundWindow;
use windows::Win32::UI::WindowsAndMessaging::GetWindowThreadProcessId;
mod defaults {
pub const DATA_REFRESH_INTERVAL: u64 = 10;
pub const ERROR_TEXT: &str = "Error";
}
const ERROR_TEXT: &str = "Error";
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
@@ -33,7 +30,7 @@ pub struct KeyboardConfig {
/// Enable the Input widget
pub enable: bool,
/// Data refresh interval
#[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>,
@@ -41,9 +38,7 @@ pub struct KeyboardConfig {
impl From<KeyboardConfig> for Keyboard {
fn from(value: KeyboardConfig) -> Self {
let data_refresh_interval = value
.data_refresh_interval
.unwrap_or(defaults::DATA_REFRESH_INTERVAL);
let data_refresh_interval = value.data_refresh_interval.unwrap_or(10);
Self {
enable: value.enable,
@@ -128,7 +123,7 @@ fn get_active_keyboard_layout() -> eyre::Result<String, ()> {
fn get_lang() -> String {
get_active_keyboard_layout()
.map(|l| l.trim_end_matches('\0').to_string())
.unwrap_or_else(|_| defaults::ERROR_TEXT.to_string())
.unwrap_or_else(|_| ERROR_TEXT.to_string())
}
impl Keyboard {
+2 -8
View File
@@ -16,10 +16,6 @@ use std::time::Instant;
use sysinfo::RefreshKind;
use sysinfo::System;
mod defaults {
pub const DATA_REFRESH_INTERVAL: u64 = 10;
}
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
/// Memory widget configuration
@@ -27,7 +23,7 @@ pub struct MemoryConfig {
/// Enable the Memory 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>,
@@ -37,9 +33,7 @@ pub struct MemoryConfig {
impl From<MemoryConfig> for Memory {
fn from(value: MemoryConfig) -> Self {
let data_refresh_interval = value
.data_refresh_interval
.unwrap_or(defaults::DATA_REFRESH_INTERVAL);
let data_refresh_interval = value.data_refresh_interval.unwrap_or(10);
Self {
enable: value.enable,
+2 -6
View File
@@ -24,10 +24,6 @@ use std::time::Duration;
use std::time::Instant;
use sysinfo::Networks;
mod defaults {
pub const DATA_REFRESH_INTERVAL: u64 = 10;
}
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
/// Network widget configuration
@@ -46,7 +42,7 @@ pub struct NetworkConfig {
#[serde(alias = "network_activity_fill_characters")]
pub activity_left_padding: Option<usize>,
/// 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>,
@@ -73,7 +69,7 @@ impl From<NetworkConfig> for Network {
let default_refresh_interval = 10;
let data_refresh_interval = value
.data_refresh_interval
.unwrap_or(defaults::DATA_REFRESH_INTERVAL);
.unwrap_or(default_refresh_interval);
Self {
enable: value.enable,
+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(),
+2 -8
View File
@@ -14,10 +14,6 @@ use std::process::Command;
use std::time::Duration;
use std::time::Instant;
mod defaults {
pub const DATA_REFRESH_INTERVAL: u64 = 12;
}
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
/// Update widget configuration
@@ -25,7 +21,7 @@ pub struct UpdateConfig {
/// Enable the Update widget
pub enable: bool,
/// Data refresh interval in hours
#[cfg_attr(feature = "schemars", schemars(extend("default" = defaults::DATA_REFRESH_INTERVAL)))]
#[cfg_attr(feature = "schemars", schemars(extend("default" = 12)))]
pub data_refresh_interval: Option<u64>,
/// Display label prefix
pub label_prefix: Option<LabelPrefix>,
@@ -33,9 +29,7 @@ pub struct UpdateConfig {
impl From<UpdateConfig> for Update {
fn from(value: UpdateConfig) -> Self {
let data_refresh_interval = value
.data_refresh_interval
.unwrap_or(defaults::DATA_REFRESH_INTERVAL);
let data_refresh_interval = value.data_refresh_interval.unwrap_or(12);
let mut latest_version = String::new();