m3u8_parser/m3u8/parser.rs
1/// Parses attributes from a given input string and returns a map of key-value pairs.
2///
3/// This function processes an input string formatted as key-value pairs separated by commas.
4/// Each key-value pair is expected to be in the format `key="value"`. The function will trim
5/// the quotes around the values and construct a `HashMap` containing the parsed attributes.
6///
7/// # Arguments
8///
9/// * `input` - A string containing the attributes to be parsed. The expected format is
10/// `key1="value1",key2="value2",...`.
11///
12/// # Returns
13///
14/// A result containing a `HashMap<String, String>` of attributes if parsing is successful,
15/// or an error message as a string if an error occurs during parsing.
16///
17/// # Example
18///
19/// ```
20/// use m3u8_parser::m3u8::parser::parse_attributes;
21/// let input = r#"METHOD="AES-128",URI="https://example.com/key",IV="1234567890abcdef""#;
22/// let attributes = parse_attributes(input).expect("Failed to parse attributes");
23/// assert_eq!(attributes.get("METHOD"), Some(&"AES-128".to_string()));
24/// assert_eq!(attributes.get("URI"), Some(&"https://example.com/key".to_string()));
25/// ```
26///
27pub fn parse_attributes(input: &str) -> Result<std::collections::HashMap<String, String>, String> {
28 let mut attributes = std::collections::HashMap::new();
29 for part in input.split(',') {
30 let parts: Vec<&str> = part.splitn(2, '=').collect();
31 if parts.len() == 2 {
32 attributes.insert(parts[0].to_string(), parts[1].trim_matches('"').to_string());
33 }
34 }
35 Ok(attributes)
36}