From d36102c30a4fb471ce8d882d3e9f8b3f6950b6d0 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Wed, 31 Jan 2024 14:03:32 +0100 Subject: [PATCH] Make Problem fields optional --- src/types.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/types.rs b/src/types.rs index 541d4b4..1cd26d3 100644 --- a/src/types.rs +++ b/src/types.rs @@ -108,11 +108,11 @@ pub struct Problem { /// One of an enumerated list of problem types /// /// See - pub r#type: String, + pub r#type: Option, /// A human-readable explanation of the problem - pub detail: String, + pub detail: Option, /// The HTTP status code returned for this response - pub status: u16, + pub status: Option, } impl Problem { @@ -136,7 +136,16 @@ impl Problem { impl fmt::Display for Problem { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "API error: {} ({})", self.detail, self.r#type) + f.write_str("API error")?; + if let Some(detail) = &self.detail { + write!(f, ": {detail}")?; + } + + if let Some(r#type) = &self.r#type { + write!(f, " ({})", r#type)?; + } + + Ok(()) } }