use std::collections::HashMap;
use crate::common::Options;
pub struct RESTUtil;
impl RESTUtil {
pub fn encode_string(value: &str) -> String {
url::form_urlencoded::byte_serialize(value.as_bytes()).collect()
}
pub fn decode_string(encoded: &str) -> String {
url::form_urlencoded::parse(encoded.as_bytes())
.map(|(k, _)| k.to_string())
.collect()
}
pub fn extract_prefix_map(options: &Options, prefix: &str) -> HashMap<String, String> {
options.extract_prefix_map(prefix)
}
pub fn merge(
base_properties: Option<&HashMap<String, String>>,
override_properties: Option<&HashMap<String, String>>,
) -> HashMap<String, String> {
let mut result = HashMap::new();
if let Some(base) = base_properties {
for (key, value) in base {
result.insert(key.clone(), value.clone());
}
}
if let Some(overrides) = override_properties {
for (key, value) in overrides {
result.insert(key.clone(), value.clone());
}
}
result
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_encode_decode_string() {
let original = "hello world=/&?#";
let encoded = RESTUtil::encode_string(original);
let decoded = RESTUtil::decode_string(&encoded);
assert_eq!(decoded, original);
}
#[test]
fn test_extract_prefix_map() {
let mut options = Options::new();
options.set("header.Content-Type", "application/json");
options.set("header.Authorization", "Bearer token");
options.set("other.key", "value");
let headers = RESTUtil::extract_prefix_map(&options, "header.");
assert_eq!(headers.len(), 2);
assert_eq!(
headers.get("Content-Type"),
Some(&"application/json".to_string())
);
}
}