use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct UploadError {
pub error: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub request_id: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct CommentOutcome {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct UploadCreated {
pub id: String,
pub repository: String,
pub benchmark: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub baseline: Option<String>,
#[serde(default)]
pub comment: CommentOutcome,
}
#[cfg(test)]
mod tests {
use crate::json::cloud_api::{CommentOutcome, UploadCreated, UploadError};
#[test]
fn upload_error_parses_with_extra_fields() {
let err: UploadError =
serde_json::from_str(r#"{"error":"nope","request_id":"1bac4db9-15a","code":"later"}"#)
.unwrap();
assert_eq!(err.error, "nope");
assert_eq!(err.request_id.as_deref(), Some("1bac4db9-15a"));
let minimal: UploadError = serde_json::from_str(r#"{"error":"boom"}"#).unwrap();
assert_eq!(minimal.request_id, None);
}
#[test]
fn upload_created_parses_with_and_without_comment() {
let full: UploadCreated = serde_json::from_str(
r#"{"id":"r1","repository":"a/b","benchmark":"meta","baseline":"r0",
"comment":{"url":"https://github.com/c/1","error":"the report does not parse"},
"later":true}"#,
)
.unwrap();
assert_eq!(full.baseline.as_deref(), Some("r0"));
assert_eq!(full.comment.url.as_deref(), Some("https://github.com/c/1"));
assert_eq!(
full.comment.error.as_deref(),
Some("the report does not parse")
);
let bare: UploadCreated =
serde_json::from_str(r#"{"id":"r1","repository":"a/b","benchmark":"meta"}"#).unwrap();
assert_eq!(bare.baseline, None);
assert_eq!(bare.comment, CommentOutcome::default());
}
#[test]
fn optional_fields_are_omitted_when_none() {
let json = serde_json::to_string(&UploadCreated {
id: "r1".into(),
repository: "a/b".into(),
benchmark: "meta".into(),
baseline: None,
comment: CommentOutcome::default(),
})
.unwrap();
assert_eq!(
json,
r#"{"id":"r1","repository":"a/b","benchmark":"meta","comment":{}}"#
);
}
}