return HashMap for Titan header options, but replace with IndexMap to keep original arguments order

This commit is contained in:
yggverse 2025-02-23 09:22:40 +02:00
parent 3cb2296784
commit 1a1c0a9898
2 changed files with 9 additions and 3 deletions

View file

@ -11,5 +11,6 @@ repository = "https://github.com/YGGverse/titanite"
[dependencies]
anyhow = "1.0.96"
indexmap = "2.7.1"
regex = "1.11.1"
url = "2.5.4"

View file

@ -3,7 +3,7 @@ pub struct Header {
pub url: String,
pub mime: Option<String>,
pub token: Option<String>,
pub options: Option<Vec<(String, String)>>,
pub options: Option<IndexMap<String, String>>,
}
impl Header {
@ -33,11 +33,15 @@ impl Header {
options: match Regex::new(r"\?(.*)$")?.captures(header) {
Some(c) => match c.get(1) {
Some(v) => {
let mut options = Vec::new();
let mut options = IndexMap::new();
for option in v.as_str().split("&") {
let kv: Vec<&str> = option.split('=').collect();
if kv.len() == 2 {
options.push((kv[0].to_string(), kv[1].to_string()))
if let Some(v) =
options.insert(kv[0].to_string(), kv[1].to_string())
{
bail!("Option key duplicate with value: {v}")
}
} else {
bail!("Invalid options format")
}
@ -97,4 +101,5 @@ fn test() {
}
use anyhow::{bail, Result};
use indexmap::IndexMap;
use std::str::from_utf8;