Skip to content

Commit

Permalink
Merge pull request #224 from pcasotti/fix/tap-hold-defaults
Browse files Browse the repository at this point in the history
fix(macro): use defaults defined in `TapHoldConfig`
  • Loading branch information
HaoboGu authored Dec 29, 2024
2 parents 8e22492 + d234078 commit bf778b2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
35 changes: 24 additions & 11 deletions rmk-macro/src/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,39 @@ fn expand_tap_hold(tap_hold: &Option<TapHoldConfig>) -> proc_macro2::TokenStream
let default = quote! {::rmk::config::TapHoldConfig::default()};
match tap_hold {
Some(tap_hold) => {
let enable_hrm = tap_hold.enable_hrm.unwrap_or_default();
let enable_hrm = match tap_hold.enable_hrm {
Some(enable) => quote! { enable_hrm: #enable, },
None => quote! {},
};
let prior_idle_time = match &tap_hold.prior_idle_time {
Some(t) => t.0,
None => 120,
Some(t) => {
let timeout = t.0;
quote! { prior_idle_time: ::embassy_time::Duration::from_millis(#timeout), }
}
None => quote! {},
};
let post_wait_time = match &tap_hold.post_wait_time {
Some(t) => t.0,
None => 50,
Some(t) => {
let timeout = t.0;
quote! { post_wait_time: ::embassy_time::Duration::from_millis(#timeout), }
}
None => quote! {},
};
let hold_timeout = match &tap_hold.hold_timeout {
Some(t) => t.0,
None => 250,
Some(t) => {
let timeout = t.0;
quote! { hold_timeout: ::embassy_time::Duration::from_millis(#timeout), }
}
None => quote! {},
};

quote! {
::rmk::config::TapHoldConfig {
enable_hrm: #enable_hrm,
prior_idle_time: ::embassy_time::Duration::from_millis(#prior_idle_time),
post_wait_time: ::embassy_time::Duration::from_millis(#post_wait_time),
hold_timeout: ::embassy_time::Duration::from_millis(#hold_timeout),
#enable_hrm
#prior_idle_time
#post_wait_time
#hold_timeout
..Default::default()
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions rmk-macro/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,10 @@ fn parse_duration_millis<'de, D: de::Deserializer<'de>>(deserializer: D) -> Resu
})?;

match unit {
"s" => Ok(num*1000),
"s" => Ok(num * 1000),
"ms" => Ok(num),
other => Err(de::Error::custom(format!("Invalid unit \"{other}\" in [one_shot.timeout]: unit part must be either \"s\" or \"ms\""))),
other => Err(de::Error::custom(format!(
"Invalid duration unit \"{other}\": unit part must be either \"s\" or \"ms\""
))),
}
}

0 comments on commit bf778b2

Please sign in to comment.