Skip to content

Commit

Permalink
drop serde_with dependency (#2001)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxVerevkin authored Feb 16, 2024
1 parent 88cfdbe commit f7bffc9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 160 deletions.
146 changes: 10 additions & 136 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ env_logger = "0.11"
futures = { version = "0.3", default-features = false }
glob = { version = "0.3.1", optional = true }
hyper = "0.14"
icu_datetime = { version = "1.3.0", optional = true }
icu_calendar = { version = "1.3.0", optional = true }
icu_datetime = { version = "1.3.0", optional = true }
icu_locid = { version = "1.3.0", optional = true }
indexmap = { version = "2.0", features = ["serde"] }
inotify = "0.10"
itertools = "0.12"
libc = "0.2"
Expand All @@ -63,14 +64,13 @@ reqwest = { version = "0.11", features = ["json"] }
sensors = "0.2.2"
serde = { version = "1.0", features = ["derive", "rc"] }
serde_json = "1.0"
serde_with = "3.0"
shellexpand = "3.0"
signal-hook = "0.3"
signal-hook-tokio = { version = "0.3", features = ["futures-v0_3"] }
smart-default = "0.7"
swayipc-async = "2.0"
thiserror = "1.0"
toml = "0.8"
toml = { version = "0.8", features = ["preserve_order"] }
unicode-segmentation = "1.10.1"
wayrs-client = { version = "1.0", features = ["tokio"] }
wayrs-protocols = { version = "0.13", features = ["wlr-foreign-toplevel-management-unstable-v1"] }
Expand Down
29 changes: 8 additions & 21 deletions src/blocks/sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,10 @@ mod alsa;
mod pulseaudio;

use super::prelude::*;
use crate::wrappers::SerdeRegex;
use indexmap::IndexMap;
use regex::Regex;
use serde_with::{serde_as, serde_conv, Map};

// A conversion adapter that is used to convert Regex to String and vice versa
// when serializing or deserializing using serde.
serde_conv!(
RegexAsString,
Regex,
|regex: &Regex| regex.as_str().to_string(),
|value: String| Regex::new(&value)
);

#[serde_as]
#[derive(Deserialize, Debug, SmartDefault)]
#[serde(deny_unknown_fields, default)]
pub struct Config {
Expand All @@ -121,17 +112,15 @@ pub struct Config {
pub format: FormatConfig,
pub headphones_indicator: bool,
pub show_volume_when_muted: bool,
#[serde_as(as = "Option<Map<_, _>>")]
pub mappings: Option<Vec<(String, String)>>,
pub mappings: Option<IndexMap<String, String>>,
#[default(true)]
pub mappings_use_regex: bool,
pub max_vol: Option<u32>,
#[serde_as(as = "Map<RegexAsString, _>")]
pub active_port_mappings: Vec<(Regex, String)>,
pub active_port_mappings: IndexMap<SerdeRegex, String>,
}

enum Mappings<'a> {
Exact(&'a [(String, String)]),
Exact(&'a IndexMap<String, String>),
Regex(Vec<(Regex, &'a str)>),
}

Expand Down Expand Up @@ -249,9 +238,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
}
}
Some(Mappings::Exact(m)) => {
if let Some((_, mapped)) =
m.iter().find(|&(exact, _)| output_name == exact.as_str())
{
if let Some(mapped) = m.get(&output_name) {
output_name = mapped.clone();
}
}
Expand All @@ -261,9 +248,9 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
if let Some((regex, mapped)) = config
.active_port_mappings
.iter()
.find(|(regex, _)| regex.is_match(ap))
.find(|(regex, _)| regex.0.is_match(ap))
{
let mapped = regex.replace(ap, mapped);
let mapped = regex.0.replace(ap, mapped);
if mapped.is_empty() {
active_port = None;
} else {
Expand Down
14 changes: 14 additions & 0 deletions src/wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,20 @@ where
#[derive(Debug, Clone)]
pub struct SerdeRegex(pub regex::Regex);

impl PartialEq for SerdeRegex {
fn eq(&self, other: &Self) -> bool {
self.0.as_str() == other.0.as_str()
}
}

impl Eq for SerdeRegex {}

impl std::hash::Hash for SerdeRegex {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.0.as_str().hash(state);
}
}

impl<'de> Deserialize<'de> for SerdeRegex {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down

0 comments on commit f7bffc9

Please sign in to comment.