Skip to content

Commit b1776c9

Browse files
authored
Merge pull request #1193 from JakeStanger/refactor/serde-default
refactor(config): prefer `serde(default)` attr at struct-level
2 parents 99babdb + 3faff44 commit b1776c9

File tree

25 files changed

+448
-621
lines changed

25 files changed

+448
-621
lines changed

src/clients/bluetooth.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub struct Client {
6060

6161
impl Client {
6262
pub(crate) async fn new() -> Result<Self> {
63-
let (tx, _rx) = watch::channel(BluetoothState::NotFound);
63+
let (tx, rx) = watch::channel(BluetoothState::NotFound);
6464
let session = bluer::Session::new().await?;
6565
{
6666
let tx = tx.clone();
@@ -102,7 +102,11 @@ impl Client {
102102
});
103103
}
104104

105-
Ok(Self { session, tx, _rx })
105+
Ok(Self {
106+
session,
107+
tx,
108+
_rx: rx,
109+
})
106110
}
107111

108112
pub(crate) fn subscribe(&self) -> watch::Receiver<BluetoothState> {

src/config/default.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// Command for launching external applications.
2+
///
3+
/// `gtk-launch {app_name}`
4+
pub fn launch_command() -> String {
5+
String::from("gtk-launch {app_name}")
6+
}
7+
8+
/// Image icon sizes.
9+
#[repr(i32)]
10+
pub enum IconSize {
11+
/// 32
12+
Normal = 32,
13+
/// 24
14+
Small = 24,
15+
/// 16
16+
Tiny = 16,
17+
}

src/config/mod.rs

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod common;
2+
pub mod default;
23
mod r#impl;
34
mod layout;
45
mod truncate;
@@ -232,14 +233,11 @@ impl Default for BarPosition {
232233

233234
#[derive(Debug, Default, Deserialize, Copy, Clone, PartialEq, Eq)]
234235
#[cfg_attr(feature = "schema", derive(JsonSchema))]
236+
#[serde(default)]
235237
pub struct MarginConfig {
236-
#[serde(default)]
237238
pub bottom: i32,
238-
#[serde(default)]
239239
pub left: i32,
240-
#[serde(default)]
241240
pub right: i32,
242-
#[serde(default)]
243241
pub top: i32,
244242
}
245243

@@ -251,6 +249,7 @@ pub struct MarginConfig {
251249
///
252250
#[derive(Debug, Deserialize, Clone)]
253251
#[cfg_attr(feature = "schema", derive(JsonSchema))]
252+
#[serde(default)]
254253
pub struct BarConfig {
255254
/// A unique identifier for the bar, used for controlling it over IPC.
256255
/// If not set, uses a generated integer suffix.
@@ -263,14 +262,12 @@ pub struct BarConfig {
263262
/// **Valid options**: `top`, `bottom`, `left`, `right`
264263
/// <br>
265264
/// **Default**: `bottom`
266-
#[serde(default)]
267265
pub position: BarPosition,
268266

269267
/// Whether to anchor the bar to the edges of the screen.
270268
/// Setting to false centers the bar.
271269
///
272270
/// **Default**: `true`
273-
#[serde(default = "default_true")]
274271
pub anchor_to_edges: bool,
275272

276273
/// The bar's height in pixels.
@@ -280,7 +277,6 @@ pub struct BarConfig {
280277
/// it will automatically expand to fit.
281278
///
282279
/// **Default**: `42`
283-
#[serde(default = "default_bar_height")]
284280
pub height: i32,
285281

286282
/// The margin to use on each side of the bar, in pixels.
@@ -300,7 +296,6 @@ pub struct BarConfig {
300296
/// margin.right = 10
301297
/// }
302298
/// ```
303-
#[serde(default)]
304299
pub margin: MarginConfig,
305300

306301
/// The layer-shell layer to place the bar on.
@@ -317,10 +312,7 @@ pub struct BarConfig {
317312
/// **Valid options**: `background`, `bottom`, `top`, `overlay`
318313
/// <br>
319314
/// **Default**: `top`
320-
#[serde(
321-
default = "default_layer",
322-
deserialize_with = "r#impl::deserialize_layer"
323-
)]
315+
#[serde(deserialize_with = "r#impl::deserialize_layer")]
324316
#[cfg_attr(feature = "schema", schemars(schema_with = "r#impl::schema_layer"))]
325317
pub layer: gtk_layer_shell::Layer,
326318

@@ -330,14 +322,12 @@ pub struct BarConfig {
330322
/// as the bar, causing them to shift.
331323
///
332324
/// **Default**: `true` unless `start_hidden` is set.
333-
#[serde(default)]
334325
pub exclusive_zone: Option<bool>,
335326

336327
/// The size of the gap in pixels
337328
/// between the bar and the popup window.
338329
///
339330
/// **Default**: `5`
340-
#[serde(default = "default_popup_gap")]
341331
pub popup_gap: i32,
342332

343333
/// Whether to enable autohide behaviour on the popup.
@@ -346,20 +336,17 @@ pub struct BarConfig {
346336
/// On some compositors, this may also aggressively steal mouse/keyboard focus.
347337
///
348338
/// **Default**: `false`
349-
#[serde(default)]
350339
pub popup_autohide: bool,
351340

352341
/// Whether the bar should be hidden when Ironbar starts.
353342
///
354343
/// **Default**: `false`, unless `autohide` is set.
355-
#[serde(default)]
356344
pub start_hidden: Option<bool>,
357345

358346
/// The duration in milliseconds before the bar is hidden after the cursor leaves.
359347
/// Leave unset to disable auto-hide behaviour.
360348
///
361349
/// **Default**: `null`
362-
#[serde(default)]
363350
pub autohide: Option<u64>,
364351

365352
/// An array of modules to append to the start of the bar.
@@ -404,9 +391,9 @@ impl Default for BarConfig {
404391
position: BarPosition::default(),
405392
margin: MarginConfig::default(),
406393
name: None,
407-
layer: default_layer(),
394+
layer: gtk_layer_shell::Layer::Top,
408395
exclusive_zone: None,
409-
height: default_bar_height(),
396+
height: 42,
410397
start_hidden: None,
411398
autohide: None,
412399
#[cfg(feature = "label")]
@@ -417,15 +404,16 @@ impl Default for BarConfig {
417404
start: None,
418405
center,
419406
end,
420-
anchor_to_edges: default_true(),
421-
popup_gap: default_popup_gap(),
407+
anchor_to_edges: true,
408+
popup_gap: 5,
422409
popup_autohide: false,
423410
}
424411
}
425412
}
426413

427414
#[derive(Debug, Deserialize, Clone, Default)]
428415
#[cfg_attr(feature = "schema", derive(JsonSchema))]
416+
#[serde(default)]
429417
pub struct Config {
430418
/// A map of [ironvar](ironvar) keys and values
431419
/// to initialize Ironbar with on startup.
@@ -479,30 +467,5 @@ pub struct Config {
479467
/// overriding the app's default icon.
480468
///
481469
/// **Default**: `{}`
482-
#[serde(default)]
483470
pub icon_overrides: HashMap<String, String>,
484471
}
485-
486-
const fn default_layer() -> gtk_layer_shell::Layer {
487-
gtk_layer_shell::Layer::Top
488-
}
489-
490-
const fn default_bar_height() -> i32 {
491-
42
492-
}
493-
494-
const fn default_popup_gap() -> i32 {
495-
5
496-
}
497-
498-
pub const fn default_false() -> bool {
499-
false
500-
}
501-
502-
pub const fn default_true() -> bool {
503-
true
504-
}
505-
506-
pub fn default_launch_command() -> String {
507-
String::from("gtk-launch {app_name}")
508-
}

src/modules/battery.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::channels::{AsyncSenderExt, BroadcastReceiverExt};
22
use crate::clients::upower;
33
use crate::clients::upower::BatteryState;
4-
use crate::config::{CommonConfig, LayoutConfig};
4+
use crate::config::{CommonConfig, LayoutConfig, default};
55
use crate::gtk_helpers::IronbarLabelExt;
66
use crate::image::IconLabel;
77
use crate::modules::PopupButton;
@@ -26,23 +26,22 @@ const MINUTE: i64 = 60;
2626

2727
#[derive(Debug, Deserialize, Clone)]
2828
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
29+
#[serde(default)]
2930
pub struct BatteryModule {
3031
/// The format string to use for the widget button label.
3132
/// For available tokens, see [below](#formatting-tokens).
3233
///
3334
/// **Default**: `{percentage}%`
34-
#[serde(default = "default_format")]
3535
format: String,
3636

3737
/// The size to render the icon at, in pixels.
3838
///
3939
/// **Default**: `24`
40-
#[serde(default = "default_icon_size")]
4140
icon_size: i32,
4241

4342
// -- Common --
4443
/// See [layout options](module-level-options#layout)
45-
#[serde(default, flatten)]
44+
#[serde(flatten)]
4645
layout: LayoutConfig,
4746

4847
/// A map of threshold names to apply as classes,
@@ -70,20 +69,23 @@ pub struct BatteryModule {
7069
/// Above 20%, no class applies.
7170
///
7271
/// **Default**: `{}`
73-
#[serde(default)]
7472
thresholds: HashMap<Box<str>, f64>,
7573

7674
/// See [common options](module-level-options#common-options).
7775
#[serde(flatten)]
7876
pub common: Option<CommonConfig>,
7977
}
8078

81-
fn default_format() -> String {
82-
String::from("{percentage}%")
83-
}
84-
85-
const fn default_icon_size() -> i32 {
86-
24
79+
impl Default for BatteryModule {
80+
fn default() -> Self {
81+
Self {
82+
format: "{percentage}%".to_string(),
83+
icon_size: default::IconSize::Small as i32,
84+
layout: LayoutConfig::default(),
85+
thresholds: HashMap::new(),
86+
common: Some(CommonConfig::default()),
87+
}
88+
}
8789
}
8890

8991
#[derive(Clone, Debug, Default)]

0 commit comments

Comments
 (0)