Skip to content

Commit e5dd049

Browse files
refactor: rework choice_enum macro
Signed-off-by: Maika Namuo <httpworldview@gmail.com>
1 parent b5fdfbe commit e5dd049

5 files changed

Lines changed: 30 additions & 24 deletions

File tree

‎src/infra/pipewire/registry/types.rs‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ pub(super) fn dict_to_map(dict: Option<&DictRef>) -> HashMap<String, String> {
1616
}
1717

1818
crate::macros::choice_enum!(no_default all
19-
#[derive(Hash)]
2019
pub enum AudioChannel {
2120
FrontLeft => "FL", FrontRight => "FR", FrontCenter => "FC", LowFrequency => "LFE",
2221
RearLeft => "RL", RearRight => "RR", SideLeft => "SL", SideRight => "SR", Mono => "MONO",
@@ -32,11 +31,13 @@ impl AudioChannel {
3231
}
3332
}
3433

35-
crate::macros::choice_enum!(pub enum PortDirection {
36-
Input => "Input",
37-
Output => "Output",
38-
#[default] Unknown => "Unknown",
39-
});
34+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
35+
pub enum PortDirection {
36+
Input,
37+
Output,
38+
#[default]
39+
Unknown,
40+
}
4041

4142
#[derive(Clone, Debug, Default, PartialEq, Eq)]
4243
pub struct GraphPort {
@@ -70,11 +71,13 @@ impl GraphPort {
7071
}
7172
}
7273

73-
crate::macros::choice_enum!(pub enum NodeDirection {
74-
Input => "Input",
75-
Output => "Output",
76-
#[default] Unknown => "Unknown",
77-
});
74+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
75+
pub enum NodeDirection {
76+
Input,
77+
Output,
78+
#[default]
79+
Unknown,
80+
}
7881

7982
fn derive_node_direction(
8083
media_class: Option<&str>,

‎src/macros.rs‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
// Copyright (C) 2026 Maika Namuo
33

44
macro_rules! choice_enum {
5-
(@build [$($default:ident)?] $(#[$attr:meta])* $vis:vis enum $name:ident { $($(#[$var_attr:meta])* $variant:ident => $label:expr),+ $(,)? }) => {
5+
(@parse [$($default:ident)?] [$($all:ident)?] no_default $($rest:tt)+) => {
6+
$crate::macros::choice_enum!(@parse [] [$($all)?] $($rest)+);
7+
};
8+
(@parse [$($default:ident)?] [$($all:ident)?] all $($rest:tt)+) => {
9+
$crate::macros::choice_enum!(@parse [$($default)?] [all] $($rest)+);
10+
};
11+
(@parse [$($default:ident)?] [$($all:ident)?] $(#[$attr:meta])* $vis:vis enum $name:ident { $($(#[$var_attr:meta])* $variant:ident => $label:expr),+ $(,)? }) => {
612
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize, PartialEq, Eq $(, $default)?)]
713
#[serde(rename_all = "snake_case")]
814
$(#[$attr])*
@@ -15,15 +21,14 @@ macro_rules! choice_enum {
1521
impl std::fmt::Display for $name {
1622
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(self.label()) }
1723
}
24+
25+
$crate::macros::choice_enum!(@all [$($all)?] $name { $($variant,)+ });
1826
};
19-
(@build_all [$($default:ident)?] $(#[$attr:meta])* $vis:vis enum $name:ident { $($(#[$var_attr:meta])* $variant:ident => $label:expr),+ $(,)? }) => {
20-
$crate::macros::choice_enum!(@build [$($default)?] $(#[$attr])* $vis enum $name { $($(#[$var_attr])* $variant => $label,)+ });
27+
(@all [] $($rest:tt)*) => {};
28+
(@all [all] $name:ident { $($variant:ident,)+ }) => {
2129
impl $name { pub const ALL: &'static [Self] = &[$(Self::$variant,)+]; }
2230
};
23-
(no_default all $($rest:tt)+) => { $crate::macros::choice_enum!(@build_all [] $($rest)+); };
24-
(all $($rest:tt)+) => { $crate::macros::choice_enum!(@build_all [Default] $($rest)+); };
25-
(no_default $($rest:tt)+) => { $crate::macros::choice_enum!(@build [] $($rest)+); };
26-
($($rest:tt)+) => { $crate::macros::choice_enum!(@build [Default] $($rest)+); };
31+
($($rest:tt)+) => { $crate::macros::choice_enum!(@parse [Default] [] $($rest)+); };
2732
}
2833

2934
pub(super) use choice_enum;

‎src/ui/settings/spectrum.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ const BARS_R: SliderRange = SliderRange::new(8.0, 128.0, 1.0);
2424
const GAP_R: SliderRange = SliderRange::new(0.0, 0.8, 0.05);
2525
const HIGH_R: SliderRange = SliderRange::new(0.0, 0.9, 0.01);
2626
const FLOOR_R: SliderRange = SliderRange::new(MIN_SPECTRUM_DB_FLOOR, MAX_SPECTRUM_DB_FLOOR, 1.0);
27-
crate::macros::choice_enum!(all pub(in crate::ui) enum AvgMode {
27+
crate::macros::choice_enum!(no_default all pub(in crate::ui) enum AvgMode {
2828
None => "None",
29-
#[default] Exponential => "Exponential",
29+
Exponential => "Exponential",
3030
PeakHold => "Peak hold",
3131
});
3232

‎src/util/audio/channel.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
use std::collections::VecDeque;
55

6-
crate::macros::choice_enum!(all pub enum Channel {
7-
#[default]
6+
crate::macros::choice_enum!(no_default all pub enum Channel {
87
Left => "Left",
98
Right => "Right",
109
Mid => "Mid",

‎src/visuals.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ pub mod options {
103103
crate::macros::choice_enum!(all pub enum CorrelationMeterSide { Left => "Left", #[default] Right => "Right" });
104104
crate::macros::choice_enum!(all pub enum PianoRollOverlay { #[default] Off => "Off", Right => "Right", Left => "Left" });
105105

106-
crate::macros::choice_enum!(all pub enum MeterMode {
107-
#[default]
106+
crate::macros::choice_enum!(no_default all pub enum MeterMode {
108107
LufsShortTerm => "LUFS Short-term",
109108
LufsMomentary => "LUFS Momentary",
110109
RmsFast => "RMS Fast",

0 commit comments

Comments
 (0)