use std::collections::HashMap;
use std::io::{Error, ErrorKind};
pub fn format_page_range(offset: u64, length: u64) -> Result<String, Error> {
if !offset.is_multiple_of(512) {
return Err(Error::new(
ErrorKind::InvalidInput,
format!(
"provided offset {} is not aligned to a 512-byte boundary.",
offset
),
));
}
if !length.is_multiple_of(512) {
return Err(Error::new(
ErrorKind::InvalidInput,
format!(
"provided length {} is not aligned to a 512-byte boundary.",
length
),
));
}
let end_range = offset + length - 1;
let content_range = format!("bytes={}-{}", offset, end_range);
Ok(content_range)
}
pub fn format_filter_expression(tags: &HashMap<String, String>) -> Result<String, Error> {
if tags.is_empty() {
return Err(Error::new(
ErrorKind::InvalidInput,
"Tags HashMap cannot be empty.".to_string(),
));
}
let format_expression: Vec<String> = tags
.iter()
.map(|(key, value)| format!("\"{}\"='{}'", key, value))
.collect();
Ok(format_expression.join(" and "))
}