Skip to content

Commit

Permalink
feat(builder): add value method
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAntix committed Jan 30, 2024
1 parent cf37d0a commit 5099f28
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 87 deletions.
33 changes: 27 additions & 6 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::{Cow, Borrow, BorrowMut};
use std::borrow::{Borrow, BorrowMut, Cow};

use crate::{Cookie, SameSite, Expiration};
use crate::{Cookie, Expiration, SameSite};

/// Structure that follows the builder pattern for building `Cookie` structs.
///
Expand Down Expand Up @@ -59,10 +59,28 @@ impl<'c> CookieBuilder<'c> {
/// assert_eq!(c.name_value(), ("foo", "bar"));
/// ```
pub fn new<N, V>(name: N, value: V) -> Self
where N: Into<Cow<'c, str>>,
V: Into<Cow<'c, str>>
where
N: Into<Cow<'c, str>>,
V: Into<Cow<'c, str>>,
{
CookieBuilder { cookie: Cookie::new(name, value) }
CookieBuilder {
cookie: Cookie::new(name, value),
}
}

/// Sets the `value` field in the cookie being built.
///
/// # Example
///
/// ```rust
/// use cookie::Cookie;
///
/// let c = Cookie::build("foo").value("bar");
/// assert_eq!(c.inner().name_value(), ("foo", "bar"));
/// ```
pub fn value<V: Into<Cow<'c, str>>>(mut self, value: V) -> Self {
self.cookie.set_value(value);
self
}

/// Sets the `expires` field in the cookie being built.
Expand Down Expand Up @@ -353,7 +371,10 @@ impl<'c> CookieBuilder<'c> {
/// Instead of using this method, pass a `CookieBuilder` directly into
/// methods expecting a `T: Into<Cookie>`. For other cases, use
/// [`CookieBuilder::build()`].
#[deprecated(since="0.18.0", note="`CookieBuilder` can be passed in to methods expecting a `Cookie`; for other cases, use `CookieBuilder::build()`")]
#[deprecated(
since = "0.18.0",
note = "`CookieBuilder` can be passed in to methods expecting a `Cookie`; for other cases, use `CookieBuilder::build()`"
)]
pub fn finish(self) -> Cookie<'c> {
self.cookie
}
Expand Down
Loading

0 comments on commit 5099f28

Please sign in to comment.