CookieJar iter return &Cookie now

This commit is contained in:
2023-03-27 11:18:28 +00:00
committed by GitHub
parent f80fd2efae
commit e29362d80f
2 changed files with 35 additions and 20 deletions

View File

@@ -355,14 +355,9 @@ impl CookieJar {
#[allow(dead_code)]
pub fn get<S: AsRef<str> + ?Sized>(&self, name: &S) -> Option<&Cookie> {
let name = name.as_ref();
for i in self.cookies.iter() {
match i {
CookieJarLine::Cookie(i) => {
if i._name == name {
return Some(i);
}
}
_ => {}
for i in self.iter() {
if i._name == name {
return Some(i);
}
}
None
@@ -448,8 +443,34 @@ impl CookieJar {
true
}
pub fn iter(&self) -> core::slice::Iter<CookieJarLine> {
self.cookies.iter()
pub fn iter<'a>(&'a self) -> CookieJarItor<'a> {
CookieJarItor {
iter: self.cookies.iter(),
}
}
}
pub struct CookieJarItor<'a> {
iter: core::slice::Iter<'a, CookieJarLine>,
}
impl<'a> Iterator for CookieJarItor<'a> {
type Item = &'a Cookie;
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.iter.next() {
Some(item) => match item {
CookieJarLine::Cookie(c) => {
return Some(c);
}
_ => {}
},
None => {
break;
}
}
}
None
}
}

View File

@@ -1,5 +1,4 @@
use crate::cookies::Cookie;
use crate::cookies::CookieJarLine;
use crate::cookies::ManagedCookieJar;
use crate::ext::atomic::AtomicQuick;
use crate::ext::json::ToJson;
@@ -64,16 +63,11 @@ pub fn gen_cookie_header<U: IntoUrl>(c: &WebClient, url: U) -> String {
let mut s = String::from("");
let u = url.as_str();
for a in c.get_cookies().jar.get_ref().iter() {
match a {
CookieJarLine::Cookie(a) => {
if a.matched(u) {
if s.len() > 0 {
s += " ";
}
s += a.get_name_value().as_str();
}
if a.matched(u) {
if s.len() > 0 {
s += " ";
}
_ => {}
s += a.get_name_value().as_str();
}
}
s