easycookie/
lib.rs

1use reqwest::header::HeaderMap;
2use reqwest::header::HeaderValue;
3use serde::__private::fmt::Debug;
4use serde::{Serialize, Deserialize};
5
6#[derive(Debug)]
7pub struct HeaderCookie{
8    header: HeaderMap
9}
10
11#[derive(Serialize, Deserialize, Debug)]
12pub struct CookieValue {
13    value: String
14}
15
16impl HeaderCookie{
17    pub fn get_header(&self) -> &HeaderMap {
18        &self.header
19    }
20    pub async fn get_cookie(&self, _name: &str) -> CookieValue{
21        get_cookie(self.header.clone(), _name).await
22    }
23    pub async fn get_cookie_num(&self, _num: i64) -> CookieValue{
24        get_cookie_num(self.header.clone(), _num).await
25    }
26    pub fn list_cookies(&self){
27        list_cookies(self.header.clone())
28    }
29}
30
31impl CookieValue {
32    pub fn get_value(self) -> String {
33        self.value
34    }
35}
36
37fn __parse_cookie(cookie: &HeaderValue) -> Option<&str>{
38    let str_cookie = cookie.to_str().unwrap();
39    let parse_cookie = str_cookie.split(';').next();
40    return parse_cookie;
41}
42
43pub async fn set_header(_header: HeaderMap) -> HeaderCookie{
44    HeaderCookie{header: _header}
45}
46
47pub async fn get_cookie(_header: HeaderMap, _name: &str) -> CookieValue{
48    let cookies = _header.get_all("set-cookie");
49    for cookie in cookies {
50        let parsed_cookie = __parse_cookie(cookie);
51        let _cookie_name = parsed_cookie.and_then(|cookie| cookie.split("=").next()).unwrap();
52        if _cookie_name == _name {
53            let cookie_value = parsed_cookie.and_then(|cookie| cookie.split("=").nth(1)).unwrap();
54            return CookieValue{value: String::from(cookie_value)};
55        }
56    }
57    CookieValue{value: String::from("")}
58}
59
60pub async fn get_cookie_num(_header: HeaderMap, _num: i64) -> CookieValue{
61    let cookies = _header.get_all("set-cookie");
62    let cookie = cookies.iter().nth(_num.try_into().unwrap()).unwrap();
63    let parsed_cookie = __parse_cookie(cookie);
64    let cookie_value = parsed_cookie.and_then(|cookie| cookie.split("=").nth(1)).unwrap();
65    CookieValue{value: String::from(cookie_value)}
66}
67
68pub fn list_cookies(_header: HeaderMap){
69    println!("[EasyCookie Lib] Printing the cookie list\n---------------------");
70    let cookies = _header.get_all("set-cookie");
71    for cookie in cookies {
72        let parsed_cookie = __parse_cookie(cookie);
73        let _cookie_name = parsed_cookie.and_then(|cookie| cookie.split("=").next()).unwrap();
74        println!("{:?}", _cookie_name)
75    }
76    println!("---------------------");
77}