Skip to content

Commit 9dc9314

Browse files
committed
remove with_ chainable methods
and switch to taking an impl Into<Cow<'a, str>>
1 parent a0094e1 commit 9dc9314

File tree

1 file changed

+31
-50
lines changed

1 file changed

+31
-50
lines changed

src/proxies/forwarded.rs

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,11 @@ impl<'a> Forwarded<'a> {
308308
///
309309
/// ```rust
310310
/// let mut response = http_types::Response::new(200);
311-
/// http_types::proxies::Forwarded::new()
312-
/// .with_for(String::from("192.0.2.43"))
313-
/// .with_for(String::from("[2001:db8:cafe::17]"))
314-
/// .with_proto(String::from("https"))
315-
/// .apply(&mut response);
311+
/// let mut forwarded = http_types::proxies::Forwarded::new();
312+
/// forwarded.add_for("192.0.2.43");
313+
/// forwarded.add_for("[2001:db8:cafe::17]");
314+
/// forwarded.set_proto("https");
315+
/// forwarded.apply(&mut response);
316316
/// assert_eq!(response["Forwarded"], r#"for=192.0.2.43, for="[2001:db8:cafe::17]";proto=https"#);
317317
/// ```
318318
pub fn apply(&self, mut headers: impl AsMut<Headers>) {
@@ -325,10 +325,10 @@ impl<'a> Forwarded<'a> {
325325
///
326326
/// ```rust
327327
/// # fn main() -> http_types::Result<()> {
328-
/// let forwarded = http_types::proxies::Forwarded::new()
329-
/// .with_for(String::from("_haproxy"))
330-
/// .with_for(String::from("[2001:db8:cafe::17]"))
331-
/// .with_proto(String::from("https"));
328+
/// let mut forwarded = http_types::proxies::Forwarded::new();
329+
/// forwarded.add_for("_haproxy");
330+
/// forwarded.add_for("[2001:db8:cafe::17]");
331+
/// forwarded.set_proto("https");
332332
/// assert_eq!(forwarded.value()?, r#"for=_haproxy, for="[2001:db8:cafe::17]";proto=https"#);
333333
/// # Ok(()) }
334334
/// ```
@@ -368,63 +368,44 @@ impl<'a> Forwarded<'a> {
368368
Self::default()
369369
}
370370

371-
/// Returns the `by` field of this header
372-
pub fn by(&self) -> Option<&str> {
373-
self.by.as_deref()
371+
/// Adds a `for` section to this header
372+
pub fn add_for(&mut self, forwarded_for: impl Into<Cow<'a, str>>) {
373+
self.forwarded_for.push(forwarded_for.into());
374374
}
375375

376376
/// Returns the `for` field of this header
377377
pub fn forwarded_for(&self) -> Vec<&str> {
378378
self.forwarded_for.iter().map(|x| x.as_ref()).collect()
379379
}
380380

381+
/// Sets the `host` field of this header
382+
pub fn set_host(&mut self, host: impl Into<Cow<'a, str>>) {
383+
self.host = Some(host.into());
384+
}
385+
381386
/// Returns the `host` field of this header
382387
pub fn host(&self) -> Option<&str> {
383388
self.host.as_deref()
384389
}
385390

391+
/// Sets the `proto` field of this header
392+
pub fn set_proto(&mut self, proto: impl Into<Cow<'a, str>>) {
393+
self.proto = Some(proto.into())
394+
}
395+
386396
/// Returns the `proto` field of this header
387397
pub fn proto(&self) -> Option<&str> {
388398
self.proto.as_deref()
389399
}
390400

391-
/// sets the `host` field of this header
392-
pub fn set_host(&mut self, host: String) {
393-
self.host = Some(Cow::Owned(host));
394-
}
395-
396-
/// chainable builder for the `proto` field
397-
pub fn with_proto(mut self, proto: String) -> Self {
398-
self.proto = Some(Cow::Owned(proto));
399-
self
400-
}
401-
402-
/// adds a `for` section to this header
403-
pub fn add_for(&mut self, forwarded_for: String) {
404-
self.forwarded_for.push(Cow::Owned(forwarded_for));
401+
/// Sets the `by` field of this header
402+
pub fn set_by(&mut self, by: impl Into<Cow<'a, str>>) {
403+
self.by = Some(by.into());
405404
}
406405

407-
/// chainable builder to add a `for` section to this header
408-
pub fn with_for(mut self, forwarded_for: String) -> Self {
409-
self.add_for(forwarded_for);
410-
self
411-
}
412-
413-
/// chainable builder set the `host` field of this header
414-
pub fn with_host(mut self, host: String) -> Self {
415-
self.set_host(host);
416-
self
417-
}
418-
419-
/// sets the `by` field of this header
420-
pub fn set_by(&mut self, by: String) {
421-
self.by = Some(Cow::Owned(by));
422-
}
423-
424-
/// chainable builder for the `by` field of this header
425-
pub fn with_by(mut self, by: String) -> Self {
426-
self.set_by(by);
427-
self
406+
/// Returns the `by` field of this header
407+
pub fn by(&self) -> Option<&str> {
408+
self.by.as_deref()
428409
}
429410
}
430411

@@ -632,9 +613,9 @@ mod tests {
632613

633614
#[test]
634615
fn formatting_edge_cases() -> Result<()> {
635-
let forwarded = Forwarded::new()
636-
.with_for(r#"quote: " backslash: \"#.into())
637-
.with_for(";proto=https".into());
616+
let mut forwarded = Forwarded::new();
617+
forwarded.add_for(r#"quote: " backslash: \"#);
618+
forwarded.add_for(";proto=https");
638619
assert_eq!(
639620
forwarded.to_string(),
640621
r#"for="quote: \" backslash: \\", for=";proto=https""#

0 commit comments

Comments
 (0)