create struct for meta
@@ -8,10 +8,25 @@ use cookie::Cookie;
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION, COOKIE, SET_COOKIE};
use serde::{Deserialize, Serialize};
+#[derive(Debug, Serialize, Deserialize)]
+struct Meta {
+ about: String,
+ xh: String,
+}
+
+impl Default for Meta {
+ fn default() -> Self {
+ Meta {
+ xh: env!("CARGO_PKG_VERSION").into(),
+ about: "xh session file".into(),
+ }
+ }
+}
+
#[derive(Debug, Serialize, Deserialize)]
struct Content {
#[serde(rename = "__meta__")]
- meta: HashMap<String, String>,
+ meta: Meta,
// TODO: Replace String with Cookie
// (need to use Serde's serialize_with and deserialize_with)
cookies: HashMap<String, String>,
@@ -20,11 +35,8 @@ struct Content {
impl Default for Content {
fn default() -> Self {
- let mut meta = HashMap::new();
- meta.insert("about".into(), "xh session file".into());
- meta.insert("xh".into(), env!("CARGO_PKG_VERSION").into());
Content {
- meta,
+ meta: Meta::default(),
cookies: HashMap::new(),
headers: HashMap::new(),
}
@@ -37,16 +49,6 @@ pub struct Session {
content: Content,
}
-fn ensure_file_exists(path: PathBuf) -> Result<PathBuf> {
- if path.exists() {
- Ok(path)
- } else {
- fs::create_dir_all(path.parent().unwrap())?;
- fs::File::create(&path)?;
- Ok(path)
- }
-}
-
impl Session {
pub fn load_session(host: &str, name_or_path: &str, read_only: bool) -> Result<Self> {
let path = if name_or_path.contains(std::path::is_separator) {
@@ -81,8 +83,8 @@ impl Session {
let cookies = self
.content
.cookies
- .iter()
- .map(|(_, value)| Cookie::parse(value).unwrap())
+ .values()
+ .map(|value| Cookie::parse(value).unwrap())
.map(|c| format!("{}={}", c.name(), c.value()))
.collect::<Vec<_>>()
.join("; ");
@@ -95,13 +97,12 @@ impl Session {
pub fn save_headers(&mut self, request_headers: &HeaderMap) -> Result<()> {
for (key, value) in request_headers.iter() {
let key = key.as_str();
- if !key.starts_with("content-") && !key.starts_with("if-") && key != "cookie" {
+ if key != "cookie" && !key.starts_with("content-") && !key.starts_with("if-") {
self.content
.headers
.insert(key.into(), value.to_str()?.into());
}
}
-
Ok(())
}
@@ -111,7 +112,6 @@ impl Session {
.headers
.insert("authorization".into(), value.to_str()?.into());
}
-
Ok(())
}
@@ -123,7 +123,6 @@ impl Session {
.cookies
.insert(parsed_cookie.name().into(), raw_cookie.into());
}
-
Ok(())
}
@@ -132,3 +131,13 @@ impl Session {
Ok(())
}
}
+
+fn ensure_file_exists(path: PathBuf) -> Result<PathBuf> {
+ if path.exists() {
+ Ok(path)
+ } else {
+ fs::create_dir_all(path.parent().unwrap())?;
+ fs::File::create(&path)?;
+ Ok(path)
+ }
+}
