use taba_client::base64;
use taba_client::json;
use crate::tui::model::BodyView;
use crate::yaml;
pub fn formatted(bytes: &[u8]) -> Option<String> {
let text = std::str::from_utf8(bytes).ok()?;
let trimmed = text.trim();
if trimmed.is_empty() {
return None;
}
if trimmed.starts_with('{') || trimmed.starts_with('[') {
return json::validate(trimmed)
.ok()
.and_then(|()| json::pretty(trimmed).ok());
}
let map = yaml::parse(text).ok()?;
if map.is_empty() || !trimmed.contains(':') {
return None;
}
Some(yaml::render(&map))
}
pub fn base64_lines(bytes: &[u8]) -> Vec<String> {
let encoded = base64::encode(bytes);
encoded
.as_bytes()
.chunks(76)
.map(|chunk| String::from_utf8_lossy(chunk).into_owned())
.collect()
}
pub fn lines(bytes: &[u8], view: BodyView) -> (Vec<String>, bool) {
let as_sent = || {
String::from_utf8_lossy(bytes)
.lines()
.map(str::to_owned)
.collect::<Vec<_>>()
};
match view {
BodyView::Sent => (as_sent(), true),
BodyView::Formatted => match formatted(bytes) {
Some(text) => (text.lines().map(str::to_owned).collect(), true),
None => (as_sent(), false),
},
BodyView::Base64 => (base64_lines(bytes), true),
}
}
#[cfg(test)]
mod tests;