gofer/
lib.rs

1// This is free and unencumbered software released into the public domain.
2
3//! This crate provides a universal, protocol-independent `open()`.
4//!
5//! ```edition2021
6//! # use gofer::*;
7//! ```
8
9#![deny(unsafe_code)]
10//#![allow(unused)]
11
12use std::collections::HashMap;
13#[cfg(feature = "std")]
14pub use std::io::{Cursor, Read};
15
16#[cfg(not(feature = "std"))]
17todo!("the 'std' feature is currently required"); // TODO
18
19pub use dogma::Uri as Url;
20pub use dogma::UriError as UrlError;
21pub use dogma::UriScheme as UrlScheme;
22
23mod features;
24pub use features::*;
25
26mod error;
27pub use error::*;
28
29mod open;
30pub use open::*;
31
32mod schemes;
33pub use schemes::*;
34
35#[doc = include_str!("../../../README.md")]
36#[cfg(doctest)]
37pub struct ReadmeDoctests;
38
39#[derive(Clone, Debug)]
40pub struct RequestConfig {
41    pub headers: HashMap<String, String>,
42}
43
44/// Configuration for HTTP requests.
45///
46/// This struct allows customizing HTTP requests with headers. It is used with protocol-specific
47/// `open_with_config` functions to provide additional request parameters, such as custom HTTP headers.
48///
49/// # Examples
50/// ```rust
51/// use gofer::RequestConfig;
52///
53/// let config = RequestConfig::new()
54///     .with_header("User-Agent", "gofer/1.0")
55///     .with_header("Accept", "application/json");
56/// assert_eq!(config.headers.len(), 2);
57/// ```
58impl RequestConfig {
59    /// Creates a new, empty `RequestConfig`.
60    ///
61    /// # Returns
62    /// A `RequestConfig` instance with no headers set.
63    pub fn new() -> Self {
64        RequestConfig {
65            headers: HashMap::new(),
66        }
67    }
68
69    /// Adds a header to the configuration and returns the modified `RequestConfig`.
70    ///
71    /// This method allows chaining to add multiple headers fluently.
72    ///
73    /// # Arguments
74    /// * `key` - The header name (e.g., `"User-Agent"`).
75    /// * `value` - The header value (e.g., `"gofer/1.0"`).
76    ///
77    /// # Returns
78    /// The modified `RequestConfig` with the new header added.
79    ///
80    /// # Examples
81    /// ```rust
82    /// use gofer::RequestConfig;
83    ///
84    /// let config = RequestConfig::new().with_header("Authorization", "Bearer token");
85    /// assert_eq!(config.headers.get("Authorization"), Some(&"Bearer token".to_string()));
86    /// ```
87    pub fn with_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
88        self.headers.insert(key.into(), value.into());
89        self
90    }
91}