use foundation_errstacks::StructuredErrorTrace;
pub const ERRSTACKS_TYPE_URL: &str =
"type.googleapis.com/foundation.errstacks.StructuredErrorTrace";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ErrorDetail {
type_url: String,
value: Vec<u8>,
wire_json: Option<String>,
}
impl ErrorDetail {
#[must_use]
pub fn from_raw(type_url: impl Into<String>, value: Vec<u8>) -> Self {
Self {
type_url: type_url.into(),
value,
wire_json: None,
}
}
#[must_use]
pub fn from_errstacks(trace: &StructuredErrorTrace) -> Self {
let json = serde_json::to_string(trace).unwrap_or_else(|_| "{}".to_string());
Self {
type_url: ERRSTACKS_TYPE_URL.to_string(),
value: json.clone().into_bytes(),
wire_json: Some(json),
}
}
#[must_use]
pub fn type_url(&self) -> &str {
&self.type_url
}
#[must_use]
pub fn type_name(&self) -> &str {
match self.type_url.rsplit_once('/') {
Some((_, name)) => name,
None => &self.type_url,
}
}
#[must_use]
pub fn value(&self) -> &[u8] {
&self.value
}
#[must_use]
pub fn wire_json(&self) -> Option<&str> {
self.wire_json.as_deref()
}
#[must_use]
pub fn with_wire_json(mut self, json: impl Into<String>) -> Self {
self.wire_json = Some(json.into());
self
}
}