From 2264bd9ad6908495c491702b6f1bbec9159c027b Mon Sep 17 00:00:00 2001 From: Dovydas Date: Sun, 11 Feb 2024 18:18:54 +0000 Subject: [PATCH] allow floats for value time split start times and durations --- Cargo.toml | 2 +- src/basic.rs | 14 ++++++++++++++ src/parse.rs | 6 +++--- tests/data/podcast_namespace_example.xml | 2 +- tests/tests.rs | 2 +- 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 320e773..57e5a00 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0" name = "badpod" readme = "README.md" repository = "https://github.com/rssblue/badpod" -version = "0.8.1" +version = "0.8.2" [dependencies] chrono = "0.4.33" diff --git a/src/basic.rs b/src/basic.rs index 884924c..6e95bda 100644 --- a/src/basic.rs +++ b/src/basic.rs @@ -259,4 +259,18 @@ impl Duration { )), } } + + pub fn parse_from_float_string(s: &str) -> Self { + match s.parse::() { + Ok(x) => { + // Convert to nanoseconds and round to the nearest integer. + let x = (x * 1_000_000_000.0).round() as i64; + Self::Duration(chrono::Duration::nanoseconds(x)) + } + Err(_) => Self::Other(( + s.to_string(), + "should be a non-negative floating-point number".to_string(), + )), + } + } } diff --git a/src/parse.rs b/src/parse.rs index 33df607..5cddd70 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -796,15 +796,15 @@ fn parse_podcast_value_time_split(value_time_split: roxmltree::Node) -> podcast: match attribute.name() { "startTime" => { new_value_time_split.start_time = - Some(basic::Duration::parse_from_int_string(attribute.value())) + Some(basic::Duration::parse_from_float_string(attribute.value())) } "duration" => { new_value_time_split.duration = - Some(basic::Duration::parse_from_int_string(attribute.value())) + Some(basic::Duration::parse_from_float_string(attribute.value())) } "remoteStartTime" => { new_value_time_split.remote_start_time = - Some(basic::Duration::parse_from_int_string(attribute.value())) + Some(basic::Duration::parse_from_float_string(attribute.value())) } "remotePercentage" => { new_value_time_split.remote_percentage = diff --git a/tests/data/podcast_namespace_example.xml b/tests/data/podcast_namespace_example.xml index e834ffa..b659d1e 100644 --- a/tests/data/podcast_namespace_example.xml +++ b/tests/data/podcast_namespace_example.xml @@ -55,7 +55,7 @@ - + diff --git a/tests/tests.rs b/tests/tests.rs index 90bb93f..b4f4fba 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -467,7 +467,7 @@ fn deserialize() { value_time_split: vec![ podcast::ValueTimeSplit{ start_time: Some(badpod::Duration::Duration(chrono::Duration::minutes(1))), - duration: Some(badpod::Duration::Duration(chrono::Duration::minutes(3) + chrono::Duration::seconds(57))), + duration: Some(badpod::Duration::Duration(chrono::Duration::minutes(3) + chrono::Duration::seconds(57) + chrono::Duration::milliseconds(500))), remote_start_time: None, remote_percentage: Some(Integer::Ok(95)), value_recipient: vec![],