Skip to content

Commit

Permalink
Merge pull request #43 from dcampbell24/add-serialization-deserializa…
Browse files Browse the repository at this point in the history
…tion

Add serialization deserialization
  • Loading branch information
pfernie authored Oct 9, 2024
2 parents c6752f9 + 9645dd5 commit 3d4c286
Show file tree
Hide file tree
Showing 10 changed files with 1,108 additions and 642 deletions.
33 changes: 24 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,43 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
default = ["public_suffix"]
default = ["public_suffix", "serde_json"]

# uses `indexmap::IndexMap` in lieu of HashMap internally, so cookies are maintained in insertion/creation order
preserve_order = ["indexmap"]
public_suffix = ["publicsuffix"]
## uses `indexmap::IndexMap` in lieu of HashMap internally, so cookies are maintained in insertion/creation order
preserve_order = ["dep:indexmap"]
## Add support for public suffix lists, as provided by [publicsuffix](https://crates.io/crates/publicsuffix).
public_suffix = ["dep:publicsuffix"]
## Enables transitive feature `time/wasm-bindgen`; necessary in `wasm` contexts.
wasm-bindgen = ["time/wasm-bindgen"]

# Enable logging the values of cookies marked 'secure', off by default as values may be sensitive
## Enable logging the values of cookies marked 'secure', off by default as values may be sensitive
log_secure_cookie_values = []

#! ### Serialization
## Supports generic (format-agnostic) de/serialization for a `CookieStore`. Adds dependencies `serde` and `serde_derive`.
serde = ["dep:serde", "dep:serde_derive"]
## Supports de/serialization for a `CookieStore` via the JSON format. Enables feature `serde` and adds depenency `serde_json`.
serde_json = ["serde", "dep:serde_json"]
## Supports de/serialization for a `CookieStore` via the RON format. Enables feature `serde` and adds depenency `ron`.
serde_ron = ["serde", "dep:ron"]

[dependencies]
document-features = "0.2.10"
idna = "0.5.0"
log = "0.4.17"
publicsuffix = { version = "2.2.3", optional = true }
serde = "1.0.147"
serde_derive = "1.0.147"
serde_json = "1.0.87"
time = "0.3.16"
url = "2.3.1"

indexmap = { version = "2.0.0", optional = true }

publicsuffix = { version = "2.2.3", optional = true }

# serialization dependencies
serde = { version = "1.0.147", optional = true }
serde_derive = { version = "1.0.147", optional = true }
serde_json = { version = "1.0.87", optional = true }
ron = { version = "0.8.1", optional = true }

[dependencies.cookie]
features = ["percent-encode"]
version = "0.18.0"
13 changes: 8 additions & 5 deletions src/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::cookie_path::CookiePath;

use crate::utils::{is_http_scheme, is_secure};
use cookie::{Cookie as RawCookie, CookieBuilder as RawCookieBuilder, ParseError};
#[cfg(feature = "serde")]
use serde_derive::{Deserialize, Serialize};
use std::borrow::Cow;
use std::convert::TryFrom;
Expand Down Expand Up @@ -68,11 +69,12 @@ impl From<ParseError> for Error {
pub type CookieResult<'a> = Result<Cookie<'a>, Error>;

/// A cookie conforming more closely to [IETF RFC6265](https://datatracker.ietf.org/doc/html/rfc6265)
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
#[derive(PartialEq, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Cookie<'a> {
/// The parsed Set-Cookie data
#[serde(serialize_with = "serde_raw_cookie::serialize")]
#[serde(deserialize_with = "serde_raw_cookie::deserialize")]
#[cfg_attr(feature = "serde", serde(serialize_with = "serde_raw_cookie::serialize"))]
#[cfg_attr(feature = "serde", serde(deserialize_with = "serde_raw_cookie::deserialize"))]
raw_cookie: RawCookie<'a>,
/// The Path attribute from a Set-Cookie header or the default-path as
/// determined from
Expand All @@ -91,6 +93,7 @@ pub struct Cookie<'a> {
pub expires: CookieExpiration,
}

#[cfg(feature = "serde")]
mod serde_raw_cookie {
use cookie::Cookie as RawCookie;
use serde::de::Error;
Expand Down Expand Up @@ -723,8 +726,8 @@ mod tests {
}
}

#[cfg(test)]
mod serde_tests {
#[cfg(all(test, feature = "serde_json"))]
mod serde_json_tests {
use crate::cookie::Cookie;
use crate::cookie_expiration::CookieExpiration;
use crate::utils::test as test_utils;
Expand Down
8 changes: 5 additions & 3 deletions src/cookie_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cookie::Cookie as RawCookie;
use idna;
#[cfg(feature = "public_suffix")]
use publicsuffix::{List, Psl, Suffix};
#[cfg(feature = "serde")]
use serde_derive::{Deserialize, Serialize};
use std::convert::TryFrom;
use url::{Host, Url};
Expand All @@ -18,7 +19,8 @@ pub fn is_match(domain: &str, request_url: &Url) -> bool {
}

/// The domain of a `Cookie`
#[derive(PartialEq, Eq, Clone, Debug, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[derive(PartialEq, Eq, Clone, Debug, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum CookieDomain {
/// No Domain attribute in Set-Cookie header
HostOnly(String),
Expand Down Expand Up @@ -365,8 +367,8 @@ mod tests {
}
}

#[cfg(test)]
mod serde_tests {
#[cfg(all(test, feature = "serde_json"))]
mod serde_json_tests {
use serde_json;
use std::convert::TryFrom;

Expand Down
6 changes: 4 additions & 2 deletions src/cookie_expiration.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std;

#[cfg(feature = "serde")]
use serde_derive::{Deserialize, Serialize};
use time::{self, OffsetDateTime};

/// When a given `Cookie` expires
#[derive(Eq, Clone, Debug, Serialize, Deserialize)]
#[derive(Eq, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum CookieExpiration {
/// `Cookie` expires at the given UTC time, as set from either the Max-Age
/// or Expires attribute of a Set-Cookie header
#[serde(with = "crate::rfc3339_fmt")]
#[cfg_attr(feature = "serde", serde(with = "crate::rfc3339_fmt"))]
AtUtc(OffsetDateTime),
/// `Cookie` expires at the end of the current `Session`; this means the cookie
/// is not persistent
Expand Down
4 changes: 3 additions & 1 deletion src/cookie_path.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(feature = "serde")]
use serde_derive::{Deserialize, Serialize};
use std::cmp::max;
use std::ops::Deref;
Expand All @@ -10,7 +11,8 @@ pub fn is_match(path: &str, request_url: &Url) -> bool {
}

/// The path of a `Cookie`
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, Hash, PartialOrd, Ord)]
#[derive(PartialEq, Eq, Clone, Debug, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CookiePath(String, bool);
impl CookiePath {
/// Determine if `request_url` path-matches this `CookiePath` per
Expand Down
Loading

0 comments on commit 3d4c286

Please sign in to comment.