use anyhow::{bail, Result};
use serde::de::DeserializeOwned;
#[derive(Debug, serde::Deserialize)]
struct Envelope<T> {
#[serde(default)]
success: bool,
#[serde(default = "no_data")]
data: Option<T>,
#[serde(default)]
error_code: Option<String>,
}
fn no_data<T>() -> Option<T> {
None
}
pub fn unwrap<T: DeserializeOwned>(body: &str) -> Result<T> {
let envelope: Envelope<T> = serde_json::from_str(body)
.map_err(|failure| anyhow::anyhow!("unexpected answer ({failure}): {body}"))?;
match envelope.data {
Some(data) if envelope.success => Ok(data),
_ => bail!(
"the platform answered {}: {body}",
envelope.error_code.unwrap_or_else(|| "no data".to_string())
),
}
}
pub fn error_code(body: &str) -> Option<String> {
serde_json::from_str::<Envelope<serde_json::Value>>(body)
.ok()
.and_then(|envelope| envelope.error_code)
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug, serde::Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
struct Token {
access_token: String,
}
#[test]
fn the_useful_body_lives_under_data() {
let parsed: Token =
unwrap(r#"{"success":true,"data":{"accessToken":"abc"}}"#).expect("unwrap");
assert_eq!(parsed.access_token, "abc");
}
#[test]
fn a_flat_read_of_the_same_body_would_have_failed() {
assert!(
serde_json::from_str::<Token>(r#"{"success":true,"data":{"accessToken":"abc"}}"#)
.is_err()
);
}
#[test]
fn an_error_body_names_its_code_instead_of_pretending_to_be_data() {
let body = r#"{"success":false,"error_code":"authorization_pending"}"#;
assert_eq!(error_code(body).as_deref(), Some("authorization_pending"));
assert!(unwrap::<Token>(body)
.unwrap_err()
.to_string()
.contains("authorization_pending"));
}
#[test]
fn a_body_that_is_not_an_envelope_reports_the_body_itself() {
let failure = unwrap::<Token>("<html>502</html>").unwrap_err().to_string();
assert!(failure.contains("502"), "{failure}");
assert!(error_code("<html>502</html>").is_none());
}
}