Skip to content

Commit

Permalink
allow floats for value time split start times and durations
Browse files Browse the repository at this point in the history
  • Loading branch information
joksas committed Feb 11, 2024
1 parent 87090db commit 2264bd9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,18 @@ impl Duration {
)),
}
}

pub fn parse_from_float_string(s: &str) -> Self {
match s.parse::<f64>() {
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(),
)),
}
}
}
6 changes: 3 additions & 3 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion tests/data/podcast_namespace_example.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<podcast:valueRecipient name="podcaster" type="node" address="036557ea56b3b86f08be31bcd2557cae8021b0e3a9413f0c0e52625c6696972e57" split="99" />
<podcast:valueRecipient name="hosting company" type="node" address="036557ea56b3b86f08be31bcd2557cae8021b0e3a9413f0c0e52625c6696972e57" split="1" />

<podcast:valueTimeSplit startTime="60" duration="237" remotePercentage="95">
<podcast:valueTimeSplit startTime="60" duration="237.5" remotePercentage="95">
<podcast:remoteItem itemGuid="https://podcastindex.org/podcast/4148683#1" feedGuid="a94f5cc9-8c58-55fc-91fe-a324087a655b" medium="music" />
</podcast:valueTimeSplit>
</podcast:value>
Expand Down
2 changes: 1 addition & 1 deletion tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![],
Expand Down

0 comments on commit 2264bd9

Please sign in to comment.