| 147 | body: Arc<str>, |
| 148 | body_bytes: Bytes, |
| 149 | cached_raw_body: Option<Arc<str>>, |
| 150 | cached_formatted_body: Option<Arc<str>>, |
| 151 | is_json: bool, |
| 152 | } |
| 153 | |
| 154 | impl ResponseTextSnapshot { |
| 155 | pub fn source_len(&self) -> usize { |
| 156 | if self.body.is_empty() { |
| 157 | self.body_bytes.len() |
| 158 | } else { |
| 159 | self.body.len() |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | pub fn raw_body(&self) -> Arc<str> { |
| 164 | if !self.body.is_empty() || self.body_bytes.is_empty() { |
| 165 | return self.body.clone(); |
| 166 | } |
| 167 | if let Some(raw) = &self.cached_raw_body { |
| 168 | return raw.clone(); |
| 169 | } |
| 170 | |
| 171 | Arc::from(String::from_utf8_lossy(&self.body_bytes).into_owned()) |
| 172 | } |
| 173 | |
| 174 | pub fn formatted_body(&self) -> Arc<str> { |
| 175 | if let Some(formatted) = &self.cached_formatted_body { |
| 176 | return formatted.clone(); |
| 177 | } |
| 178 | |
| 179 | let body = self.raw_body(); |
| 180 | if !self.is_json { |
| 181 | return body; |
| 182 | } |
| 183 | |
| 184 | serde_json::from_str::<serde_json::Value>(&body) |
| 185 | .ok() |
| 186 | .and_then(|value| serde_json::to_string_pretty(&value).ok()) |
| 187 | .map(Arc::<str>::from) |