Skip to content

Instantly share code, notes, and snippets.

@ecumene
Last active February 11, 2022 03:54
Show Gist options
  • Save ecumene/f2ae89aa95a002633da8087b1208463d to your computer and use it in GitHub Desktop.
Save ecumene/f2ae89aa95a002633da8087b1208463d to your computer and use it in GitHub Desktop.
Parse cookies
#[derive(Debug, Clone)]
struct BadCookie;
type CookieResult<T> = std::result::Result<T, BadCookie>;
#[derive(Debug)]
struct Cookies<'a> {
cookies: HashMap<&'a str, &'a str>,
}
impl<'a> Cookies<'a> {
fn from(item: &str) -> CookieResult<Cookies> {
let cookies = item
.split(" ")
.into_iter()
.map(|kv| {
let mut split = kv.split("=");
Ok((
split.next().ok_or(BadCookie)?,
split.next().ok_or(BadCookie)?,
))
})
.collect::<CookieResult<HashMap<&str, &str>>>()?;
Ok(Cookies { cookies })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment