Repository: awslabs/llrt
File: llrt_core/src/modules/http/headers.rs (Rust)

Code before:
        Ok(Self {
            headers: Vec::new(),
        })
    }

    pub fn append(&mut self, key: String, value: String) {
        let key = key.to_lowercase();
        if key == HEADERS_KEY_SET_COOKIE {
            return self.headers.push((key, value));
        }
        if let Some((_, existing_value)) = self.headers.iter_mut().find(|(k, _)| k == &key) {
            match key.as_str() {
                HEADERS_KEY_COOKIE => existing_value.push_str("; "),
                _ => existing_value.push_str(", "),
            }
            existing_value.push_str(&value);
        } else {
            self.headers.push((key, value));
        }
    }

    pub fn get(&self, key: String) -> Option<String> {
        let key = key.to_lowercase();
        let mut result = String::new();
        for (k, v) in &self.headers {
            if k == &key {
                if !result.is_empty() {
                    result.push_str(", ");
                }
                result.push_str(v);
            }
        }
        if result.is_empty() {
            None
        } else {
            Some(result)
        }
    }

    pub fn get_set_cookie(&self) -> Vec<String> {
        self.headers
            .iter()
            .filter_map(|(k, v)| {
                if k.eq_ignore_ascii_case(HEADERS_KEY_SET_COOKIE) {
                    Some(v.clone())
                } else {
                    None
                }
            })
            .collect()
    }

Review comment (refactor):
Why do we need this logic here? We can only get a single header value right?
Can maybe simplify to this?
```rust
&self.headers.iter().find(|(k,_)|k == &key).map(|(_,v)|v.clone())
```

Code after:
        Ok(Self {
            headers: Vec::new(),
        })
    }

    pub fn append(&mut self, key: String, value: String) {
        let key = key.to_lowercase();
        if key == HEADERS_KEY_SET_COOKIE {
            return self.headers.push((key, value));
        }
        if let Some((_, existing_value)) = self.headers.iter_mut().find(|(k, _)| k == &key) {
            match key.as_str() {
                HEADERS_KEY_COOKIE => existing_value.push_str("; "),
                _ => existing_value.push_str(", "),
            }
            existing_value.push_str(&value);
        } else {
            self.headers.push((key, value));
        }
    }

    pub fn get(&self, key: String) -> Option<String> {
        let key = key.to_lowercase();
        if key == HEADERS_KEY_SET_COOKIE {
            let result: Vec<String> = self
                .headers
                .iter()
                .filter_map(|(k, v)| if k == &key { Some(v.clone()) } else { None })
                .collect();
            return if result.is_empty() {
                None
            } else {
                Some(result.join(", "))
            };
        }
        self.headers
            .iter()
            .find(|(k, _)| k == &key)
            .map(|(_, v)| v.clone())
    }

    pub fn get_set_cookie(&self) -> Vec<String> {
        self.headers
            .iter()
            .filter_map(|(k, v)| {
                if k == HEADERS_KEY_SET_COOKIE {
                    Some(v.clone())
                } else {
                    None
                }
            })