Improve duration deserialization
This commit is contained in:
parent
5539b7706d
commit
06de91a6fd
@ -98,8 +98,8 @@ mod types {
|
||||
start: DateTime<Utc>,
|
||||
stop: Option<DateTime<Utc>>,
|
||||
|
||||
// TODO This should be an Option<u32> as all negatives signify currently running time entries
|
||||
duration: i32,
|
||||
#[serde(with = "duration_field")]
|
||||
duration: Option<u32>,
|
||||
|
||||
at: DateTime<Utc>,
|
||||
|
||||
@ -115,6 +115,32 @@ mod types {
|
||||
server_deleted_at: Option<DateTime<Utc>>,
|
||||
permissions: Option<String>,
|
||||
}
|
||||
|
||||
mod duration_field {
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub fn serialize<S>(duration: &Option<u32>, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
match duration {
|
||||
None => i32::serialize(&-1, serializer),
|
||||
Some(duration) => i32::serialize(&(*duration as i32), serializer),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<u32>, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let duration = i32::deserialize(deserializer)?;
|
||||
if duration < 0 {
|
||||
Ok(None)
|
||||
} else {
|
||||
Ok(Some(duration as u32))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
|
||||
Loading…
Reference in New Issue
Block a user