use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct FeedHttpResponse {
pub status: u16,
pub url: String,
pub headers: HashMap<String, String>,
pub body: Vec<u8>,
pub etag: Option<String>,
pub last_modified: Option<String>,
pub content_type: Option<String>,
pub encoding: Option<String>,
}
impl FeedHttpResponse {
pub fn extract_charset_from_content_type(content_type: &str) -> Option<String> {
content_type.split(';').find_map(|part| {
part.trim()
.strip_prefix("charset=")
.map(|s| s.trim_matches('"').to_string())
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_charset_simple() {
let ct = "text/xml; charset=utf-8";
assert_eq!(
FeedHttpResponse::extract_charset_from_content_type(ct),
Some("utf-8".to_string())
);
}
#[test]
fn test_extract_charset_quoted() {
let ct = "application/xml; charset=\"ISO-8859-1\"";
assert_eq!(
FeedHttpResponse::extract_charset_from_content_type(ct),
Some("ISO-8859-1".to_string())
);
}
#[test]
fn test_extract_charset_no_charset() {
let ct = "application/xml";
assert_eq!(
FeedHttpResponse::extract_charset_from_content_type(ct),
None
);
}
#[test]
fn test_extract_charset_multiple_params() {
let ct = "text/html; boundary=something; charset=utf-8";
assert_eq!(
FeedHttpResponse::extract_charset_from_content_type(ct),
Some("utf-8".to_string())
);
}
}