Skip to content

Commit

Permalink
Update duration ser-deser helper names
Browse files Browse the repository at this point in the history
  • Loading branch information
rustworthy committed Oct 19, 2024
1 parent e9e8ae1 commit cf8f757
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/proto/single/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ pub struct Job {
/// Defaults to 600 seconds.
#[serde(
skip_serializing_if = "Option::is_none",
serialize_with = "utils::ser_optional_duration",
deserialize_with = "utils::deser_as_optional_duration"
serialize_with = "utils::ser_optional_duration_in_seconds",
deserialize_with = "utils::deser_as_optional_duration_in_seconds"
)]
#[builder(default = "Some(Duration::from_secs(JOB_DEFAULT_RESERVED_FOR_SECS))")]
pub reserve_for: Option<Duration>,
Expand Down
4 changes: 2 additions & 2 deletions src/proto/single/resp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ pub struct ServerSnapshot {
pub version: semver::Version,

/// Faktory server process uptime in seconds.
#[serde(deserialize_with = "utils::deser_duration")]
#[serde(serialize_with = "utils::ser_duration")]
#[serde(deserialize_with = "utils::deser_duration_in_seconds")]
#[serde(serialize_with = "utils::ser_duration_in_seconds")]
pub uptime: Duration,

/// Number of clients connected to the server.
Expand Down
12 changes: 7 additions & 5 deletions src/proto/single/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(crate) fn gen_random_wid() -> String {
gen_random_id(WORKER_ID_LENGTH)
}

pub(crate) fn ser_duration<S>(value: &Duration, serializer: S) -> Result<S::Ok, S::Error>
pub(crate) fn ser_duration_in_seconds<S>(value: &Duration, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
Expand All @@ -43,15 +43,17 @@ where
}
}

pub(crate) fn deser_duration<'de, D>(value: D) -> Result<Duration, D::Error>
pub(crate) fn deser_duration_in_seconds<'de, D>(value: D) -> Result<Duration, D::Error>
where
D: Deserializer<'de>,
{
let secs = u64::deserialize(value)?;
Ok(Duration::from_secs(secs))
}

pub(crate) fn deser_as_optional_duration<'de, D>(value: D) -> Result<Option<Duration>, D::Error>
pub(crate) fn deser_as_optional_duration_in_seconds<'de, D>(
value: D,
) -> Result<Option<Duration>, D::Error>
where
D: Deserializer<'de>,
{
Expand Down Expand Up @@ -115,8 +117,8 @@ mod test {
fn test_ser_deser_duration() {
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
struct FaktoryServer {
#[serde(deserialize_with = "deser_duration")]
#[serde(serialize_with = "ser_duration")]
#[serde(deserialize_with = "deser_duration_in_seconds")]
#[serde(serialize_with = "ser_duration_in_seconds")]
uptime: Duration,
}

Expand Down

0 comments on commit cf8f757

Please sign in to comment.