1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use std::time::{Duration, SystemTime};

#[derive(Debug)]
pub struct Cookie {
	pub name: String,
	pub value: String,
	pub http_only: bool,
	pub secure: bool,
	pub same_site_lax: bool,
	pub same_site_strict: bool,
	pub path: Option<String>,
	pub max_age: Option<Duration>,
	pub expires: Option<SystemTime>,
}

pub fn from_tower_cookie_deref(val: &cookie::Cookie) -> Cookie {
	let max_age: Option<Duration> = val
		.max_age()
		.map(|d| d.try_into().expect("time::Duration into std::time::Duration"));
	let expires = match val.expires() {
		Some(cookie::Expiration::DateTime(offset)) => Some(SystemTime::from(offset)),
		None | Some(cookie::Expiration::Session) => None,
	};

	Cookie {
		name: val.name().to_string(),
		value: val.value().to_string(),
		http_only: val.http_only().unwrap_or(false),
		secure: val.secure().unwrap_or(false),
		same_site_lax: matches!(val.same_site(), Some(cookie::SameSite::Lax)),
		same_site_strict: matches!(val.same_site(), Some(cookie::SameSite::Strict)),
		path: val.path().map(String::from),
		max_age,
		expires,
	}
}

impl From<&cookie::Cookie<'_>> for Cookie {
	fn from(val: &cookie::Cookie) -> Self {
		let max_age: Option<Duration> = val
			.max_age()
			.map(|d| d.try_into().expect("time::Duration into std::time::Duration"));
		let expires = match val.expires() {
			Some(cookie::Expiration::DateTime(offset)) => Some(SystemTime::from(offset)),
			None | Some(cookie::Expiration::Session) => None,
		};

		Cookie {
			name: val.name().to_string(),
			value: val.value().to_string(),
			http_only: val.http_only().unwrap_or(false),
			secure: val.secure().unwrap_or(false),
			same_site_lax: matches!(val.same_site(), Some(cookie::SameSite::Lax)),
			same_site_strict: matches!(val.same_site(), Some(cookie::SameSite::Strict)),
			path: val.path().map(String::from),
			max_age,
			expires,
		}
	}
}

impl From<reqwest::cookie::Cookie<'_>> for Cookie {
	fn from(val: reqwest::cookie::Cookie<'_>) -> Self {
		Cookie {
			name: val.name().to_string(),
			value: val.value().to_string(),
			http_only: val.http_only(),
			secure: val.secure(),
			same_site_lax: val.same_site_lax(),
			same_site_strict: val.same_site_strict(),
			path: val.path().map(String::from),
			max_age: val.max_age(),
			expires: val.expires(),
		}
	}
}

impl From<&reqwest::cookie::Cookie<'_>> for Cookie {
	fn from(val: &reqwest::cookie::Cookie) -> Self {
		Cookie {
			name: val.name().to_string(),
			value: val.value().to_string(),
			http_only: val.http_only(),
			secure: val.secure(),
			same_site_lax: val.same_site_lax(),
			same_site_strict: val.same_site_strict(),
			path: val.path().map(String::from),
			max_age: val.max_age(),
			expires: val.expires(),
		}
	}
}