Skip to content

Commit 6be05b8

Browse files
committed
Parse duration fields in JSON as an integer seconds
1 parent c0b733e commit 6be05b8

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

src/config.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ pub struct Config {
110110
feature = "cli",
111111
structopt(short = "m", long, env, hide_env_values(true), display_order(36), parse(try_from_str = parse_duration))
112112
)]
113+
#[serde(default, deserialize_with = "parse_duration_serde_opt")]
113114
pub bitcoind_timeout: Option<time::Duration>,
114115

115116
/// Create the specified bitcoind wallet if it's missing [env: CREATE_WALLET_IF_MISSING]
@@ -323,7 +324,10 @@ pub struct Config {
323324
env, hide_env_values(true),
324325
display_order(90)
325326
))]
326-
#[serde(default = "default_poll_interval")]
327+
#[serde(
328+
default = "default_poll_interval",
329+
deserialize_with = "parse_duration_serde"
330+
)]
327331
pub poll_interval: time::Duration,
328332

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

702+
fn parse_duration_serde<'de, D>(deserializer: D) -> std::result::Result<time::Duration, D::Error>
703+
where
704+
D: serde::Deserializer<'de>,
705+
{
706+
use serde::Deserialize;
707+
let secs = u64::deserialize(deserializer)?;
708+
Ok(time::Duration::from_secs(secs))
709+
}
710+
711+
fn parse_duration_serde_opt<'de, D>(
712+
deserializer: D,
713+
) -> std::result::Result<Option<time::Duration>, D::Error>
714+
where
715+
D: serde::Deserializer<'de>,
716+
{
717+
Ok(Some(parse_duration_serde(deserializer)?))
718+
}
719+
698720
fn get_cookie(config: &Config) -> Option<path::PathBuf> {
699721
let mut dir = config.bitcoind_dir.clone().or_else(bitcoind_default_dir)?;
700722
match config.network {

0 commit comments

Comments
 (0)