httpc_test/
cookie.rs

1use std::time::{Duration, SystemTime};
2
3#[derive(Debug)]
4pub struct Cookie {
5	pub name: String,
6	pub value: String,
7	pub http_only: bool,
8	pub secure: bool,
9	pub same_site_lax: bool,
10	pub same_site_strict: bool,
11	pub path: Option<String>,
12	pub max_age: Option<Duration>,
13	pub expires: Option<SystemTime>,
14}
15
16pub fn from_tower_cookie_deref(val: &cookie::Cookie) -> Cookie {
17	let max_age: Option<Duration> = val
18		.max_age()
19		.map(|d| d.try_into().expect("time::Duration into std::time::Duration"));
20	let expires = match val.expires() {
21		Some(cookie::Expiration::DateTime(offset)) => Some(SystemTime::from(offset)),
22		None | Some(cookie::Expiration::Session) => None,
23	};
24
25	Cookie {
26		name: val.name().to_string(),
27		value: val.value().to_string(),
28		http_only: val.http_only().unwrap_or(false),
29		secure: val.secure().unwrap_or(false),
30		same_site_lax: matches!(val.same_site(), Some(cookie::SameSite::Lax)),
31		same_site_strict: matches!(val.same_site(), Some(cookie::SameSite::Strict)),
32		path: val.path().map(String::from),
33		max_age,
34		expires,
35	}
36}
37
38impl From<&cookie::Cookie<'_>> for Cookie {
39	fn from(val: &cookie::Cookie) -> Self {
40		let max_age: Option<Duration> = val
41			.max_age()
42			.map(|d| d.try_into().expect("time::Duration into std::time::Duration"));
43		let expires = match val.expires() {
44			Some(cookie::Expiration::DateTime(offset)) => Some(SystemTime::from(offset)),
45			None | Some(cookie::Expiration::Session) => None,
46		};
47
48		Cookie {
49			name: val.name().to_string(),
50			value: val.value().to_string(),
51			http_only: val.http_only().unwrap_or(false),
52			secure: val.secure().unwrap_or(false),
53			same_site_lax: matches!(val.same_site(), Some(cookie::SameSite::Lax)),
54			same_site_strict: matches!(val.same_site(), Some(cookie::SameSite::Strict)),
55			path: val.path().map(String::from),
56			max_age,
57			expires,
58		}
59	}
60}
61
62impl From<reqwest::cookie::Cookie<'_>> for Cookie {
63	fn from(val: reqwest::cookie::Cookie<'_>) -> Self {
64		Cookie {
65			name: val.name().to_string(),
66			value: val.value().to_string(),
67			http_only: val.http_only(),
68			secure: val.secure(),
69			same_site_lax: val.same_site_lax(),
70			same_site_strict: val.same_site_strict(),
71			path: val.path().map(String::from),
72			max_age: val.max_age(),
73			expires: val.expires(),
74		}
75	}
76}
77
78impl From<&reqwest::cookie::Cookie<'_>> for Cookie {
79	fn from(val: &reqwest::cookie::Cookie) -> Self {
80		Cookie {
81			name: val.name().to_string(),
82			value: val.value().to_string(),
83			http_only: val.http_only(),
84			secure: val.secure(),
85			same_site_lax: val.same_site_lax(),
86			same_site_strict: val.same_site_strict(),
87			path: val.path().map(String::from),
88			max_age: val.max_age(),
89			expires: val.expires(),
90		}
91	}
92}