Skip to content

Commit

Permalink
Fix parsing of recurring maintenance with no effective date range
Browse files Browse the repository at this point in the history
fixes #43
  • Loading branch information
BigBoot committed May 18, 2024
1 parent 1c77d74 commit 8e16d11
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
- Add missing `query` property for `sqlserver`, `postgres` and `mysql`, see [#36](https://github.com/BigBoot/AutoKuma/issues/36)
- Fix parsing of recurring maintenance with no effective date range, see [#43](https://github.com/BigBoot/AutoKuma/issues/43)

## [0.6.0] - 2024-04-28
### Changed
Expand Down
82 changes: 49 additions & 33 deletions kuma-client/src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,49 +237,65 @@ impl SerializeAs<PrimitiveDateTime> for SerializeDateTime {
}
pub(crate) struct SerializeDateRange;

impl<'de> DeserializeAs<'de, Range<PrimitiveDateTime>> for SerializeDateRange {
fn deserialize_as<D>(deserializer: D) -> Result<Range<PrimitiveDateTime>, D::Error>
impl<'de> DeserializeAs<'de, Option<Range<PrimitiveDateTime>>> for SerializeDateRange {
fn deserialize_as<D>(deserializer: D) -> Result<Option<Range<PrimitiveDateTime>>, D::Error>
where
D: Deserializer<'de>,
{
let value = Vec::<String>::deserialize(deserializer)
.map_err(serde::de::Error::custom)?
.into_iter()
.map(|s| PrimitiveDateTime::parse(&s, &Iso8601::DATE_TIME))
.collect::<Result<Vec<_>, _>>()
.map_err(serde::de::Error::custom)?;

if value.len() != 2 {
return Err(serde::de::Error::custom(format!(
"Expected array of length 2 but got array of length {}",
if let Some(source) = Option::<Vec<Option<String>>>::deserialize(deserializer)? {
let value = source
.into_iter()
.map(|o| {
o.map(|s| PrimitiveDateTime::parse(&s, &Iso8601::DATE_TIME))
.transpose()
})
.collect::<Result<Vec<_>, _>>()
.map_err(serde::de::Error::custom)?;

match value.len() {
1 if value[0].is_none() => Ok(None),
2 => {
let mut iter = value.into_iter();
Ok(Some(Range {
start: iter.next().unwrap().unwrap(),
end: iter.next().unwrap().unwrap(),
}))
}
_ => Err(serde::de::Error::custom(format!(
"Expected DateRange to be [Null] or array of length 2 but got array of length {}",
value.len()
)));
};

let mut iter = value.into_iter();
Ok(Range {
start: iter.next().unwrap(),
end: iter.next().unwrap(),
})
))),
}
} else {
Ok(None)
}
}
}

impl SerializeAs<Range<PrimitiveDateTime>> for SerializeDateRange {
fn serialize_as<S>(source: &Range<PrimitiveDateTime>, serializer: S) -> Result<S::Ok, S::Error>
impl SerializeAs<Option<Range<PrimitiveDateTime>>> for SerializeDateRange {
fn serialize_as<S>(
source: &Option<Range<PrimitiveDateTime>>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
vec![
&source
.start
.format(&Iso8601::DATE_TIME)
.map_err(serde::ser::Error::custom)?,
&source
.end
.format(&Iso8601::DATE_TIME)
.map_err(serde::ser::Error::custom)?,
]
.serialize(serializer)
let values = match &source {
None => vec![None],
Some(Range { start, end }) => vec![
Some(
start
.format(&Iso8601::DATE_TIME)
.map_err(serde::ser::Error::custom)?,
),
Some(
end.format(&Iso8601::DATE_TIME)
.map_err(serde::ser::Error::custom)?,
),
],
};

values.serialize(serializer)
}
}

Expand Down
3 changes: 2 additions & 1 deletion kuma-client/src/models/maintenance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ pub struct MaintenanceCommon {
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct MaintenanceSchedule {
#[serde(rename = "dateRange")]
#[serde_as(as = "Option<SerializeDateRange>")]
#[serde_as(as = "SerializeDateRange")]
#[serialize_always]
pub date_range: Option<Range<PrimitiveDateTime>>,

#[serde(rename = "timeRange")]
Expand Down

0 comments on commit 8e16d11

Please sign in to comment.