Skip to content

Commit

Permalink
data: derive Serialize for data types
Browse files Browse the repository at this point in the history
  • Loading branch information
decathorpe committed Jan 14, 2020
1 parent 075bd0a commit fdc13da
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 25 deletions.
33 changes: 32 additions & 1 deletion src/data/dates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub(crate) mod bodhi_date_format {

// https://github.com/serde-rs/serde/issues/1444#issuecomment-447546415
#[allow(dead_code)]
pub(crate) mod option_bodhi_date_format {
pub(crate) mod option_bodhi_date_format_ref {
use super::BodhiDate;

use serde::{self, Deserialize, Deserializer, Serializer};
Expand Down Expand Up @@ -115,3 +115,34 @@ pub(crate) mod option_bodhi_date_format {
Ok(v.map(|Wrapper(a)| a))
}
}

// https://github.com/serde-rs/serde/issues/1444#issuecomment-447546415
#[allow(dead_code)]
pub(crate) mod option_bodhi_date_format {
use super::BodhiDate;

use serde::{self, Deserialize, Deserializer, Serializer};

// this &Option reference is intentional, the API requires it
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn serialize<S>(date: &Option<BodhiDate>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match date {
Some(ref dt) => super::bodhi_date_format::serialize(dt, serializer),
None => serializer.serialize_none(),
}
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<BodhiDate>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct Wrapper(#[serde(with = "super::bodhi_date_format")] BodhiDate);

let v: Option<Wrapper> = Deserialize::deserialize(deserializer)?;
Ok(v.map(|Wrapper(a)| a))
}
}
30 changes: 15 additions & 15 deletions src/data/types.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::collections::HashMap;
use std::fmt::{Display, Formatter};

use serde::Deserialize;
use serde::{Deserialize, Serialize};
use url::Url;

use super::dates::*;
use super::enums::*;

/// This struct represents a specific BugZilla bug that is associated with an update.
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct Bug {
/// bug ID in the BugZilla system: <https://bugzilla.redhat.com/show_bug.cgi?id={bug_id}>
pub bug_id: u32,
Expand Down Expand Up @@ -48,7 +48,7 @@ impl Bug {
}

/// This struct represents an update feedback item associated with a specific bug.
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct BugFeedback {
/// bug this feedback is associated with
pub bug: Option<Bug>,
Expand All @@ -72,7 +72,7 @@ impl Display for BugFeedback {


/// This struct represents a specific koji build that bodhi is aware of.
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct Build {
/// epoch associated with this build
pub epoch: Option<u32>,
Expand Down Expand Up @@ -110,7 +110,7 @@ impl Display for Build {

/// This struct represents one comment against a specific update, along with its associated bug and
/// test case feedback.
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct Comment {
/// author of the comment (username), only provided for backwards compatibility
author: Option<String>,
Expand Down Expand Up @@ -158,7 +158,7 @@ impl Display for Comment {


/// This struct represents a currently running compose.
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct Compose {
/// string of JSON-formatted checkpoint data for the compose
pub checkpoints: String,
Expand Down Expand Up @@ -215,7 +215,7 @@ impl Display for Compose {


/// This struct represents a group from the fedora accounts system (FAS).
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct Group {
/// name of the group
pub name: String,
Expand All @@ -233,7 +233,7 @@ impl Display for Group {


/// This struct represents a buildroot override, along with the associated build.
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct Override {
/// build associated with this buildroot override
pub build: Build,
Expand Down Expand Up @@ -282,7 +282,7 @@ impl Display for Override {


/// This struct represents a specific fedora package (or another distributable unit)
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct Package {
/// unique name of the (source) package (or container, flatpak, or module, as appropriate)
pub name: String,
Expand Down Expand Up @@ -311,7 +311,7 @@ impl Display for Package {

/// This struct represents a fedora release as present in the bodhi database. This includes variants
/// (Modular, Container, Flatpak), identified with the "C", "F", and "M" suffixes.
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct Release {
/// dist-git branch for this release
pub branch: String,
Expand Down Expand Up @@ -378,7 +378,7 @@ impl Display for Release {


/// This struct represents a specific test case as associated with a package.
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct TestCase {
/// name of this test case
pub name: String,
Expand Down Expand Up @@ -410,7 +410,7 @@ impl Display for TestCase {


/// This struct represents an update feedback item associated with a specific test case.
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct TestCaseFeedback {
/// ID of the comment this feedback is associated with
pub comment_id: Option<u32>,
Expand All @@ -435,7 +435,7 @@ impl Display for TestCaseFeedback {

/// This struct represents a bodhi update, with associated items: bugs, builds, comments, release,
/// status, submitter, etc.
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct Update {
/// user-visible, human-readable update alias (`FEDORA-2019-1A2BB23E`)
pub alias: String,
Expand Down Expand Up @@ -593,7 +593,7 @@ impl Display for Update {

/// This struct wraps the short update summaries that are included in running
/// [`Compose`](struct.Compose.html)s.
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct UpdateSummary {
/// unique update alias identifying the update
pub alias: String,
Expand All @@ -609,7 +609,7 @@ impl Display for UpdateSummary {


/// This struct represents one fedora user that bodhi is aware of.
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct User {
/// URL of the [libravatar](https://www.libravatar.org/) avatar for this user
pub avatar: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion src/query/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ struct CommentPageQuery<'a> {
like: Option<&'a str>,
packages: Option<&'a Vec<&'a str>>,
search: Option<&'a str>,
#[serde(with = "crate::option_bodhi_date_format")]
#[serde(with = "crate::option_bodhi_date_format_ref")]
since: Option<&'a BodhiDate>,
update_owners: Option<&'a Vec<&'a str>>,
updates: Option<&'a Vec<&'a str>>,
Expand Down
16 changes: 8 additions & 8 deletions src/query/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,9 @@ struct UpdatePageQuery<'a> {
active_releases: Option<bool>,
#[serde(rename = "alias")]
aliases: Option<&'a Vec<&'a str>>,
#[serde(with = "crate::option_bodhi_date_format")]
#[serde(with = "crate::option_bodhi_date_format_ref")]
approved_before: Option<&'a BodhiDate>,
#[serde(with = "crate::option_bodhi_date_format")]
#[serde(with = "crate::option_bodhi_date_format_ref")]
approved_since: Option<&'a BodhiDate>,
bugs: Option<&'a Vec<u32>>,
builds: Option<&'a Vec<&'a str>>,
Expand All @@ -518,24 +518,24 @@ struct UpdatePageQuery<'a> {
cves: Option<&'a Vec<&'a str>>,
like: Option<&'a str>,
locked: Option<bool>,
#[serde(with = "crate::option_bodhi_date_format")]
#[serde(with = "crate::option_bodhi_date_format_ref")]
modified_before: Option<&'a BodhiDate>,
#[serde(with = "crate::option_bodhi_date_format")]
#[serde(with = "crate::option_bodhi_date_format_ref")]
modified_since: Option<&'a BodhiDate>,
packages: Option<&'a Vec<&'a str>>,
pushed: Option<bool>,
#[serde(with = "crate::option_bodhi_date_format")]
#[serde(with = "crate::option_bodhi_date_format_ref")]
pushed_before: Option<&'a BodhiDate>,
#[serde(with = "crate::option_bodhi_date_format")]
#[serde(with = "crate::option_bodhi_date_format_ref")]
pushed_since: Option<&'a BodhiDate>,
releases: Option<&'a Vec<FedoraRelease>>,
request: Option<&'a UpdateRequest>,
search: Option<&'a str>,
severity: Option<&'a UpdateSeverity>,
status: Option<&'a UpdateStatus>,
#[serde(with = "crate::option_bodhi_date_format")]
#[serde(with = "crate::option_bodhi_date_format_ref")]
submitted_before: Option<&'a BodhiDate>,
#[serde(with = "crate::option_bodhi_date_format")]
#[serde(with = "crate::option_bodhi_date_format_ref")]
submitted_since: Option<&'a BodhiDate>,
suggest: Option<&'a UpdateSuggestion>,
#[serde(rename = "updateid")]
Expand Down

0 comments on commit fdc13da

Please sign in to comment.