Skip to content

Commit

Permalink
Parse duration fields in JSON as an integer seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
shesek committed Oct 6, 2021
1 parent c0b733e commit 6be05b8
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub struct Config {
feature = "cli",
structopt(short = "m", long, env, hide_env_values(true), display_order(36), parse(try_from_str = parse_duration))
)]
#[serde(default, deserialize_with = "parse_duration_serde_opt")]
pub bitcoind_timeout: Option<time::Duration>,

/// Create the specified bitcoind wallet if it's missing [env: CREATE_WALLET_IF_MISSING]
Expand Down Expand Up @@ -323,7 +324,10 @@ pub struct Config {
env, hide_env_values(true),
display_order(90)
))]
#[serde(default = "default_poll_interval")]
#[serde(
default = "default_poll_interval",
deserialize_with = "parse_duration_serde"
)]
pub poll_interval: time::Duration,

/// Custom command for broadcasting transactions. {tx_hex} is replaced with the transaction.
Expand Down Expand Up @@ -695,6 +699,24 @@ fn parse_duration(s: &str) -> Result<time::Duration> {
Ok(time::Duration::from_secs(s.parse()?))
}

fn parse_duration_serde<'de, D>(deserializer: D) -> std::result::Result<time::Duration, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::Deserialize;
let secs = u64::deserialize(deserializer)?;
Ok(time::Duration::from_secs(secs))
}

fn parse_duration_serde_opt<'de, D>(
deserializer: D,
) -> std::result::Result<Option<time::Duration>, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(Some(parse_duration_serde(deserializer)?))
}

fn get_cookie(config: &Config) -> Option<path::PathBuf> {
let mut dir = config.bitcoind_dir.clone().or_else(bitcoind_default_dir)?;
match config.network {
Expand Down

0 comments on commit 6be05b8

Please sign in to comment.