Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map! macro: add @extend mode to extend an existing map #2040

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/blocks/privacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,34 +209,34 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
let mut values = Values::new();

if let Some(info_by_type) = info.get(&Type::Audio) {
values.extend(map! {
map! { @extend values
"icon_audio" => Value::icon("microphone"),
"info_audio" => Value::text(format!("{}", info_by_type))
});
}
}
if let Some(info_by_type) = info.get(&Type::AudioSink) {
values.extend(map! {
map! { @extend values
"icon_audio_sink" => Value::icon("volume"),
"info_audio_sink" => Value::text(format!("{}", info_by_type))
});
}
}
if let Some(info_by_type) = info.get(&Type::Video) {
values.extend(map! {
map! { @extend values
"icon_video" => Value::icon("xrandr"),
"info_video" => Value::text(format!("{}", info_by_type))
});
}
}
if let Some(info_by_type) = info.get(&Type::Webcam) {
values.extend(map! {
map! { @extend values
"icon_webcam" => Value::icon("webcam"),
"info_webcam" => Value::text(format!("{}", info_by_type))
});
}
}
if let Some(info_by_type) = info.get(&Type::Unknown) {
values.extend(map! {
map! { @extend values
"icon_unknown" => Value::icon("unknown"),
"info_unknown" => Value::text(format!("{}", info_by_type))
});
}
}

widget.set_values(values);
Expand Down
17 changes: 8 additions & 9 deletions src/blocks/weather.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,16 @@ impl WeatherResult {
if let Some(forecast) = self.forecast {
macro_rules! map_forecasts {
({$($suffix: literal => $src: expr),* $(,)?}) => {
values.extend(map!{
$(
map!{ @extend values
$(
concat!("temp_f", $suffix) => Value::degrees($src.temp),
concat!("apparent_f", $suffix) => Value::degrees($src.apparent),
concat!("humidity_f", $suffix) => Value::percents($src.humidity),
concat!("wind_f", $suffix) => Value::number($src.wind),
concat!("wind_kmh_f", $suffix) => Value::number($src.wind_kmh),
concat!("direction_f", $suffix) => Value::text(convert_wind_direction($src.wind_direction).into()),
)*
});
}
};
}
map_forecasts!({
Expand All @@ -283,12 +283,11 @@ impl WeatherResult {
"fin" => forecast.fin,
});

values.extend(map! {
"icon_ffin" => Value::icon(forecast.fin.icon.to_icon_str()),
"weather_ffin" => Value::text(forecast.fin.weather.clone()),
"weather_verbose_ffin" => Value::text(forecast.fin.weather_verbose.clone()),

});
map! { @extend values
"icon_ffin" => Value::icon(forecast.fin.icon.to_icon_str()),
"weather_ffin" => Value::text(forecast.fin.weather.clone()),
"weather_verbose_ffin" => Value::text(forecast.fin.weather_verbose.clone()),
}
}
values
}
Expand Down
26 changes: 15 additions & 11 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,29 +138,27 @@ pub async fn has_command(command: &str) -> Result<bool> {
///
/// ```ignore
/// let opt = Some(1);
/// let map: HashMap<&'static str, String> = map! {
/// let m: HashMap<&'static str, String> = map! {
/// "key" => "value",
/// [if true] "hello" => "world",
/// [if let Some(x) = opt] "opt" => x.to_string(),
/// };
/// map! { @extend m
/// "new key" => "new value",
/// "one" => "more",
/// }
/// ```
#[macro_export]
macro_rules! map {
($( $([$($cond_tokens:tt)*])? $key:literal => $value:expr ),* $(,)?) => {{
#[allow(unused_mut)]
let mut m = ::std::collections::HashMap::new();
(@extend $map:ident $( $([$($cond_tokens:tt)*])? $key:literal => $value:expr ),* $(,)?) => {{
$(
map!(@insert m, $key, $value $(,$($cond_tokens)*)?);
map!(@insert $map, $key, $value $(,$($cond_tokens)*)?);
)*
m
}};
($( $key:expr => $value:expr ),* $(,)?) => {{
#[allow(unused_mut)]
let mut m = ::std::collections::HashMap::new();
(@extend $map:ident $( $key:expr => $value:expr ),* $(,)?) => {{
$(
map!(@insert m, $key, $value);
map!(@insert $map, $key, $value);
)*
m
}};
(@insert $map:ident, $key:expr, $value:expr) => {{
$map.insert($key.into(), $value.into());
Expand All @@ -175,6 +173,12 @@ macro_rules! map {
$map.insert($key.into(), $value.into());
}
}};
($($tt:tt)*) => {{
#[allow(unused_mut)]
let mut m = ::std::collections::HashMap::new();
map!(@extend m $($tt)*);
m
}};
}

pub use map;
Expand Down