### Handling file metadata operations with and_then Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Demonstrates chaining fallible file system operations like getting metadata and modified time. `and_then` is useful when subsequent operations depend on the success of the previous one. ```rust use std::{io::ErrorKind, path::Path}; // Note: on Windows "/" maps to "C:\\" let root_modified_time = Path::new("/").metadata().and_then(|md| md.modified()); assert!(root_modified_time.is_ok()); let should_fail = Path::new("/bad/path").metadata().and_then(|md| md.modified()); assert!(should_fail.is_err()); assert_eq!(should_fail.unwrap_err().kind(), ErrorKind::NotFound); ``` -------------------------------- ### Get a reference to the Ok value Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html The `as_ref()` method converts a `&Result` into a `Result<&T, &E>`. This allows borrowing the contents of a `Result` without consuming it. ```rust let x: Result = Ok(2); assert_eq!(x.as_ref(), Ok(&2)); let x: Result = Err("Error"); assert_eq!(x.as_ref(), Err(&"Error")); ``` -------------------------------- ### Unwrap Ok Value Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Use `unwrap()` to get the contained `Ok` value. It panics if the `Result` is an `Err`. ```rust let x: Result = Ok(2); assert_eq!(x.unwrap(), 2); ``` -------------------------------- ### Safely Unwrap Infallible Ok Value Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Use `into_ok()` to get the contained `Ok` value from a `Result` where the error type is `!`. This method is guaranteed not to panic. ```rust fn only_good_news() -> Result { Ok("this is fine".into()) } let s: String = only_good_news().into_ok(); println!("{s}"); ``` -------------------------------- ### Unwrap or Default Value Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Use `unwrap_or_default()` to get the contained `Ok` value or the default value of the type if it's an `Err`. This is useful for providing fallback values. ```rust let good_year_from_input = "1909"; let bad_year_from_input = "190blarg"; let good_year = good_year_from_input.parse().unwrap_or_default(); let bad_year = bad_year_from_input.parse().unwrap_or_default(); assert_eq!(1909, good_year); assert_eq!(0, bad_year); ``` -------------------------------- ### Unwrap Err Value Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Use `unwrap_err()` to get the contained `Err` value. It panics if the `Result` is an `Ok`. ```rust let x: Result = Err("emergency failure"); assert_eq!(x.unwrap_err(), "emergency failure"); ``` -------------------------------- ### With session (caching) Source: https://docs.rs/diqwest/3.2.0/diqwest/index.html Shows how to use `DigestAuthSession` for making multiple requests to the same server, caching credentials to avoid repeated 401 challenges. ```APIDOC ## send_digest_auth (With Session Caching) ### Description Sends an HTTP request using a `DigestAuthSession`. This is useful for making multiple requests to the same server, as it caches the authentication credentials after the first successful challenge, improving performance by avoiding subsequent 401 responses. ### Method Extends `reqwest::RequestBuilder` using a `DigestAuthSession`. ### Endpoint N/A (Method on RequestBuilder) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use diqwest::{WithDigestAuth, DigestAuthSession}; use reqwest::Client; let client = Client::new(); let session = DigestAuthSession::new("username", "password"); // First request: 401 -> auth -> 200 (credentials cached) let resp1 = client.get("https://example.com/api") .send_digest_auth(&session) .await?; // Subsequent requests: preemptive auth -> 200 (no 401 challenge) let resp2 = client.get("https://example.com/other") .send_digest_auth(&session) .await?; ``` ### Response #### Success Response (200) Returns the response from the server. The first response might involve a 401 challenge, but subsequent requests using the same session will be authenticated preemptively. #### Response Example (Response body depends on the server) ``` -------------------------------- ### into_ok Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Returns the contained Ok value, never panics. This is an experimental API. ```APIDOC ## into_ok ### Description Returns the contained `Ok` value, but never panics. This method is known to never panic on the result types it is implemented for, making it a maintainability safeguard. ### Method `into_ok()` ### Constraints Requires `E: Into` ### Stability This is a nightly-only experimental API. ### Examples ```rust fn only_good_news() -> Result { Ok("this is fine".into()) } let s: String = only_good_news().into_ok(); println!("{s}"); // Output: this is fine ``` ``` -------------------------------- ### Safely Unwrap Infallible Err Value Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Use `into_err()` to get the contained `Err` value from a `Result` where the success type is `!`. This method is guaranteed not to panic. ```rust fn only_bad_news() -> Result { Err("Oops, it failed".into()) } let error: String = only_bad_news().into_err(); println!("{error}"); ``` -------------------------------- ### Manage Digest Auth with Session (Async) Source: https://docs.rs/diqwest Utilize `DigestAuthSession` for multiple requests to the same server to cache credentials and avoid repeated 401 challenges. Initialize the session with username and password. ```rust use diqwest::{WithDigestAuth, DigestAuthSession}; use reqwest::Client; let client = Client::new(); let session = DigestAuthSession::new("username", "password"); // First request: 401 -> auth -> 200 (credentials cached) let resp1 = client.get("https://example.com/api") .send_digest_auth(&session) .await?; // Subsequent requests: preemptive auth -> 200 (no 401 challenge) let resp2 = client.get("https://example.com/other") .send_digest_auth(&session) .await?; ``` -------------------------------- ### Create Credentials for Digest Auth Source: https://docs.rs/diqwest/3.2.0/diqwest/session/struct.Credentials.html Instantiate Credentials with a username and password for use in Digest authentication. This is ideal for one-off requests where caching is not required. ```rust use diqwest::Credentials; let creds = Credentials::new("username", "password"); request.send_digest_auth(creds).await?; ``` -------------------------------- ### Expect Err Value with Custom Panic Message Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Use `expect_err()` to get the contained `Err` value. It panics with a custom message if the `Result` is an `Ok`. ```rust let x: Result = Ok(10); x.expect_err("Testing expect_err"); // panics with `Testing expect_err: 10` ``` -------------------------------- ### Simple usage (no caching) Source: https://docs.rs/diqwest/3.2.0/diqwest/index.html Demonstrates how to send a single request with digest authentication without caching credentials. ```APIDOC ## send_digest_auth (Simple Usage) ### Description Sends an HTTP request with digest authentication. If the initial response is a 401, it parses the `www-authenticate` header, calculates the response, and retries the request with an `Authorization` header. Otherwise, it returns the initial response. ### Method Extends `reqwest::RequestBuilder`. ### Endpoint N/A (Method on RequestBuilder) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use diqwest::WithDigestAuth; use reqwest::Client; let response = Client::new() .get("https://example.com/api") .send_digest_auth(("username", "password")) .await?; ``` ### Response #### Success Response (200) Returns the response from the server after successful authentication. #### Response Example (Response body depends on the server) ``` -------------------------------- ### Credentials::new Source: https://docs.rs/diqwest/3.2.0/diqwest/session/struct.Credentials.html Creates new credentials with the given username and password. This is useful for one-off requests where caching is not needed. ```APIDOC ## Credentials::new ### Description Creates new credentials with the given username and password. ### Method `pub fn new(username: impl Into, password: impl Into) -> Self` ### Parameters - **username** (impl Into) - The username for authentication. - **password** (impl Into) - The password for authentication. ### Returns A new `Credentials` instance. ### Example ```rust use diqwest::Credentials; let creds = Credentials::new("username", "password"); // Assuming 'request' is a mutable object with a send_digest_auth method // request.send_digest_auth(creds).await?; ``` ``` -------------------------------- ### Create and Use DigestAuthSession Source: https://docs.rs/diqwest/3.2.0/diqwest/session/struct.DigestAuthSession.html Instantiate a DigestAuthSession with username and password. Use it with a reqwest Client to send requests, benefiting from cached credentials on subsequent calls to the same URL. ```rust use diqwest::DigestAuthSession; use reqwest::Client; let client = Client::new(); let session = DigestAuthSession::new("username", "password"); // First request: 401 -> auth -> 200 (caches credentials) let resp1 = client.get(url).send_digest_auth(&session).await?; // Second request: preemptive auth -> 200 (no 401) let resp2 = client.get(url).send_digest_auth(&session).await?; ``` -------------------------------- ### Get a mutable reference to the value Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html The `as_mut()` method converts a `&mut Result` into a `Result<&mut T, &mut E>`. This allows modifying the contents of a `Result` in place. ```rust fn mutate(r: &mut Result) { match r.as_mut() { Ok(v) => *v = 42, Err(e) => *e = 0, } } let mut x: Result = Ok(2); mutate(&mut x); assert_eq!(x.unwrap(), 42); let mut x: Result = Err(13); mutate(&mut x); assert_eq!(x.unwrap_err(), 0); ``` -------------------------------- ### Chaining Fallible Operations with `and_then` Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Demonstrates how to chain operations that may fail using the `and_then` method. This is useful for sequences of operations where each step can produce an error. ```APIDOC ## `and_then` Chains fallible operations that may return `Err`. ### Example ```rust fn sq_then_to_string(x: u32) -> Result { x.checked_mul(x).map(|sq| sq.to_string()).ok_or("overflowed") } assert_eq!(Ok(2).and_then(sq_then_to_string), Ok(4.to_string())); assert_eq!(Ok(1_000_000).and_then(sq_then_to_string), Err("overflowed")); assert_eq!(Err("not a number").and_then(sq_then_to_string), Err("not a number")); ``` ### Example with File Operations ```rust use std::{io::ErrorKind, path::Path}; // Note: on Windows "/" maps to "C:\" let root_modified_time = Path::new("/").metadata().and_then(|md| md.modified()); assert!(root_modified_time.is_ok()); let should_fail = Path::new("/bad/path").metadata().and_then(|md| md.modified()); assert!(should_fail.is_err()); assert_eq!(should_fail.unwrap_err().kind(), ErrorKind::NotFound); ``` ``` -------------------------------- ### ok() Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Converts a Result into an Option, consuming the Result and discarding the error if present. ```APIDOC ## pub fn ok(self) -> Option ### Description Converts from `Result` to `Option`. Consumes `self`, and discarding the error, if any. ### Method `ok()` ### Parameters None ### Request Body None ### Request Example ```rust let x: Result = Ok(2); assert_eq!(x.ok(), Some(2)); let x: Result = Err("Nothing here"); assert_eq!(x.ok(), None); ``` ### Response #### Success Response (Option) - Returns `Some(T)` if the Result was `Ok(T)`. - Returns `None` if the Result was `Err(E)`. #### Response Example ```rust // For Ok(2), returns Some(2) // For Err("Nothing here"), returns None ``` ``` -------------------------------- ### Combine Results with `and` Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Use `and()` to return the second `Result` if the first is `Ok`, otherwise return the `Err` of the first. Arguments are eagerly evaluated. ```rust let x: Result = Ok(2); let y: Result<&str, &str> = Err("late error"); assert_eq!(x.and(y), Err("late error")); let x: Result = Err("early error"); let y: Result<&str, &str> = Ok("foo"); assert_eq!(x.and(y), Err("early error")); let x: Result = Err("not a 2"); let y: Result<&str, &str> = Err("late error"); assert_eq!(x.and(y), Err("not a 2")); let x: Result = Ok(2); let y: Result<&str, &str> = Ok("different result type"); assert_eq!(x.and(y), Ok("different result type")); ``` -------------------------------- ### Convert Result to Option with Ok value Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html The `ok()` method converts a `Result` into an `Option`. It consumes the `Result` and returns `Some(T)` if the `Result` was `Ok`, otherwise `None`. ```rust let x: Result = Ok(2); assert_eq!(x.ok(), Some(2)); let x: Result = Err("Nothing here"); assert_eq!(x.ok(), None); ``` -------------------------------- ### pub fn unwrap(self) -> T Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Returns the contained `Ok` value, consuming the `self` value. Panics if the value is an `Err`. ```APIDOC ## pub fn unwrap(self) -> T ### Description Returns the contained `Ok` value, consuming the `self` value. Because this function may panic, its use is generally discouraged. Panics are meant for unrecoverable errors, and may abort the entire program. Instead, prefer to use the `?` (try) operator, or pattern matching to handle the `Err` case explicitly, or call `unwrap_or`, `unwrap_or_else`, or `unwrap_or_default`. ### Method `unwrap` ### Parameters None ### Request Example None ### Response #### Success Response (200) Returns the contained `Ok` value. #### Response Example None ### Panics Panics if the value is an `Err`. ``` -------------------------------- ### Map Ok value or return default (U: Default) Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html The `map_or_default()` method applies a function to the `Ok` value or returns the default value of type `U` if the `Result` is `Err`. This is an experimental nightly-only feature. ```rust #![feature(result_option_map_or_default)] let x: Result<_, &str> = Ok("foo"); let y: Result<&str, _> = Err("bar"); assert_eq!(x.map_or_default(|x| x.len()), 3); assert_eq!(y.map_or_default(|y| y.len()), 0); ``` -------------------------------- ### pub fn expect(self, msg: &str) -> T Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Returns the contained `Ok` value, consuming the `self` value. Panics if the value is an `Err`. ```APIDOC ## pub fn expect(self, msg: &str) -> T ### Description Returns the contained `Ok` value, consuming the `self` value. Because this function may panic, its use is generally discouraged. Panics if the value is an `Err`, with a panic message including the passed message, and the content of the `Err`. ### Method `expect` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust let x: Result = Err("emergency failure"); x.expect("Testing expect"); // panics with `Testing expect: emergency failure` ``` ### Response #### Success Response (200) Returns the contained `Ok` value. #### Response Example None ### Panics Panics if the value is an `Err`, with a panic message including the passed message, and the content of the `Err`. ``` -------------------------------- ### as_ref() Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Converts a reference to a Result into a Result<&T, &E>, leaving the original Result unchanged. ```APIDOC ## pub const fn as_ref(&self) -> Result<&T, &E> ### Description Converts from `&Result` to `Result<&T, &E>`. Produces a new `Result`, containing a reference into the original, leaving the original in place. ### Method `as_ref()` ### Parameters None ### Request Body None ### Request Example ```rust let x: Result = Ok(2); assert_eq!(x.as_ref(), Ok(&2)); let x: Result = Err("Error"); assert_eq!(x.as_ref(), Err(&"Error")); ``` ### Response #### Success Response (Result<&T, &E>) - Returns `Ok(&T)` if the original Result was `Ok(T)`. - Returns `Err(&E)` if the original Result was `Err(E)`. #### Response Example ```rust // For Ok(2), returns Ok(&2) // For Err("Error"), returns Err(&"Error") ``` ``` -------------------------------- ### Send Request with Digest Auth (Async) Source: https://docs.rs/diqwest Use this when making a single request that requires digest authentication. Ensure the `reqwest` crate is included in your dependencies. ```rust use diqwest::WithDigestAuth; use reqwest::Client; let response = Client::new() .get("https://example.com/api") .send_digest_auth(("username", "password")) .await?; ``` -------------------------------- ### Product for Result Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Implementation of the Product trait for Result. It iterates over a collection of Results, returning the first Err encountered or the product of all Ok values. ```APIDOC ## fn product(iter: I) -> Result ### Description Takes each element in the `Iterator`: if it is an `Err`, no further elements are taken, and the `Err` is returned. Should no `Err` occur, the product of all elements is returned. ### Parameters #### Path Parameters - **iter** (Iterator>) - Required - The iterator to process. ### Response #### Success Response (Result) - **Ok(T)** - The product of all elements if no error occurred. #### Error Response (Result) - **Err(E)** - The first error encountered during iteration. ### Examples This multiplies each number in a vector of strings, if a string could not be parsed the operation returns `Err`: ```rust let nums = vec!["5", "10", "1", "2"]; let total: Result = nums.iter().map(|w| w.parse::()).product(); assert_eq!(total, Ok(100)); let nums = vec!["5", "10", "one", "2"]; let total: Result = nums.iter().map(|w| w.parse::()).product(); assert!(total.is_err()); ``` ``` -------------------------------- ### DigestAuthSession::new Source: https://docs.rs/diqwest/3.2.0/diqwest/session/struct.DigestAuthSession.html Creates a new digest auth session with the given username and password. ```APIDOC ## DigestAuthSession::new ### Description Creates a new digest auth session with the given credentials. ### Signature ```rust pub fn new(username: impl Into, password: impl Into) -> Self ``` ``` -------------------------------- ### Implementing TryInto for T Source: https://docs.rs/diqwest/3.2.0/diqwest/error/enum.Error.html Provides a fallible conversion from type `T` to type `U`. This is the inverse of `TryFrom`. ```rust type Error = >::Error ``` ```rust fn try_into(self) -> Result>::Error> ``` -------------------------------- ### Implementing From for T Source: https://docs.rs/diqwest/3.2.0/diqwest/error/enum.Error.html A trivial implementation where a value is converted into itself. This is often a placeholder or part of blanket implementations. ```rust fn from(t: T) -> T ``` -------------------------------- ### and Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Returns the second Result if the first is Ok, otherwise returns the first Result's Err value. ```APIDOC ## and ### Description Returns `res` if the result is `Ok`, otherwise returns the `Err` value of `self`. Arguments passed to `and` are eagerly evaluated. ### Method `and(self, res: Result) -> Result` ### Examples ```rust let x: Result = Ok(2); let y: Result<&str, &str> = Err("late error"); assert_eq!(x.and(y), Err("late error")); let x: Result = Err("early error"); let y: Result<&str, &str> = Ok("foo"); assert_eq!(x.and(y), Err("early error")); let x: Result = Ok(2); let y: Result<&str, &str> = Ok("different result type"); assert_eq!(x.and(y), Ok("different result type")); ``` ``` -------------------------------- ### Blocking usage Source: https://docs.rs/diqwest/3.2.0/diqwest/index.html Illustrates how to perform digest authentication with blocking HTTP requests by enabling the `blocking` feature. ```APIDOC ## send_digest_auth (Blocking Usage) ### Description Sends a blocking HTTP request with digest authentication. This requires enabling the `blocking` feature in `Cargo.toml` and using the blocking version of `reqwest::Client`. ### Method Extends `reqwest::blocking::RequestBuilder`. ### Endpoint N/A (Method on RequestBuilder) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use diqwest::blocking::WithDigestAuth; use reqwest::blocking::Client; let response = Client::new() .get("https://example.com/api") .send_digest_auth(("username", "password"))?; ``` ### Response #### Success Response (200) Returns the blocking response from the server after successful authentication. #### Response Example (Response body depends on the server) ``` -------------------------------- ### DigestAuthCredentials Implementation for (&str, &str) Source: https://docs.rs/diqwest/3.2.0/diqwest/session/trait.DigestAuthCredentials.html A simple implementation of DigestAuthCredentials for string slices, providing username and password directly. It does not support caching. ```rust impl DigestAuthCredentials for (&str, &str) { // Required methods fn username(&self) -> &str { self.0 } fn password(&self) -> &str { self.1 } fn cached_context<'h>( &self, _host: &'h str, ) -> Result>> { Ok(None) } fn store_context(&self, _host: &str, _www_authenticate: &str) -> Result<()> { Ok(()) } fn clear_context(&self, _host: &str) -> Result<()> { Ok(()) } } ``` -------------------------------- ### Send Request with Username and Password (Deprecated) Source: https://docs.rs/diqwest/3.2.0/diqwest/trait.WithDigestAuth.html This method is deprecated and will be removed in a future version. Use `send_digest_auth` instead. ```rust request.send_with_digest_auth("user", "pass").await?; ``` -------------------------------- ### Implementing PolicyExt Trait Source: https://docs.rs/diqwest/3.2.0/diqwest/error/enum.Error.html Provides methods to combine policies, such as `and` and `or`. These are used for creating more complex decision-making logic. ```rust fn and(self, other: P) -> And where T: Policy, P: Policy ``` ```rust fn or(self, other: P) -> Or where T: Policy, P: Policy ``` -------------------------------- ### Providing a Default Value with `or` Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Illustrates how to use the `or` method to return a secondary `Result` if the initial `Result` is an `Err`. Note that arguments to `or` are eagerly evaluated. ```APIDOC #### `or(self, res: Result) -> Result` Returns `res` if the result is `Err`, otherwise returns the `Ok` value of `self`. Arguments passed to `or` are eagerly evaluated; if you are passing the result of a function call, it is recommended to use `or_else`, which is lazily evaluated. ##### §Examples ```rust let x: Result = Ok(2); let y: Result = Err("late error"); assert_eq!(x.or(y), Ok(2)); let x: Result = Err("early error"); let y: Result = Ok(2); assert_eq!(x.or(y), Ok(2)); let x: Result = Err("not a 2"); let y: Result = Err("late error"); assert_eq!(x.or(y), Err("late error")); let x: Result = Ok(2); let y: Result = Ok(100); assert_eq!(x.or(y), Ok(2)); ``` ``` -------------------------------- ### Implementing ToString for T Source: https://docs.rs/diqwest/3.2.0/diqwest/error/enum.Error.html Provides a standard way to convert a value into a `String` if it implements the `Display` trait. ```rust fn to_string(&self) -> String ``` -------------------------------- ### pub fn as_deref_mut(&mut self) -> Result<&mut ::Target, &mut E> Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Converts from `Result` to `Result<&mut ::Target, &mut E>`. Coerces the `Ok` variant of the original `Result` via `DerefMut`. ```APIDOC ## pub fn as_deref_mut(&mut self) -> Result<&mut ::Target, &mut E> ### Description Converts from `Result` (or `&mut Result`) to `Result<&mut ::Target, &mut E>`. Coerces the `Ok` variant of the original `Result` via `DerefMut` and returns the new `Result`. ### Method `as_deref_mut` ### Parameters None ### Request Example ```rust let mut s = "HELLO".to_string(); let mut x: Result = Ok("hello".to_string()); let y: Result<&mut str, &mut u32> = Ok(&mut s); assert_eq!(x.as_deref_mut().map(|x| { x.make_ascii_uppercase(); x }), y); let mut i = 42; let mut x: Result = Err(42); let y: Result<&mut str, &mut u32> = Err(&mut i); assert_eq!(x.as_deref_mut().map(|x| { x.make_ascii_uppercase(); x }), y); ``` ### Response #### Success Response (200) Returns a mutable `Result` with mutable references to the dereferenced `Ok` value or the original `Err` value. #### Response Example None ``` -------------------------------- ### map_or_default() Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Maps a Result to a U by applying a function to the Ok value or returning the default value for U if Err. ```APIDOC ## pub const fn map_or_default(self, f: F) -> U where F: FnOnce(T) -> U, U: Default, ### Description 🔬This is a nightly-only experimental API. (`result_option_map_or_default`) Maps a `Result` to a `U` by applying function `f` to the contained value if the result is `Ok`, otherwise if `Err`, returns the default value for the type `U`. ### Method `map_or_default(f)` ### Parameters - `f` (FnOnce(T) -> U): A function that takes the `Ok` value and returns a value of type U. ### Request Body None ### Request Example ```rust #![feature(result_option_map_or_default)] let x: Result<_, &str> = Ok("foo"); let y: Result<&str, _> = Err("bar"); assert_eq!(x.map_or_default(|x| x.len()), 3); assert_eq!(y.map_or_default(|y| y.len()), 0); ``` ### Response #### Success Response (U) - Returns the result of applying `f` to the `Ok` value if the Result is `Ok(T)`. - Returns the default value of type `U` if the Result is `Err(E)`. #### Response Example ```rust // For Ok("foo"), returns 3 (length of "foo") // For Err("bar"), returns 0 (default for usize) ``` ``` -------------------------------- ### Implementing Display for Error Source: https://docs.rs/diqwest/3.2.0/diqwest/error/enum.Error.html Provides a human-readable string representation for each error variant. This is useful for logging or displaying errors to users. ```rust fn fmt(&self, f: &mut Formatter<'_>) -> Result ``` -------------------------------- ### impl Result: is_ok_and Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Returns `true` if the result is `Ok` and the contained value satisfies a given predicate. ```APIDOC ## impl Result ### is_ok_and ```rust pub fn is_ok_and(self, f: F) -> bool where F: FnOnce(T) -> bool, ``` Returns `true` if the result is `Ok` and the value inside of it matches a predicate. #### Examples ```rust let x: Result = Ok(2); assert_eq!(x.is_ok_and(|x| x > 1), true); let x: Result = Ok(0); assert_eq!(x.is_ok_and(|x| x > 1), false); let x: Result = Err("hey"); assert_eq!(x.is_ok_and(|x| x > 1), false); let x: Result = Ok("ownership".to_string()); assert_eq!(x.as_ref().is_ok_and(|x| x.len() > 1), true); println!("still alive {:?}", x); ``` ``` -------------------------------- ### fn from_iter(iter: I) -> Result Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Collects an iterator of Results into a single Result. If any element is an Err, it's returned immediately. Otherwise, a container with all Ok values is returned. ```APIDOC ## fn from_iter(iter: I) -> Result ### Description Takes each element in the `Iterator`: if it is an `Err`, no further elements are taken, and the `Err` is returned. Should no `Err` occur, a container with the values of each `Result` is returned. ### Parameters #### Path Parameters - **iter** (I) - Required - An iterator where each item is a `Result`. ### Response - **Result** - A container `V` with the values of each `Result` if all elements are `Ok`, otherwise the first `Err` encountered. ### Examples #### Example 1: Successful collection ```rust let v = vec![1, 2]; let res: Result, &'static str> = v.iter().map(|x: &u32| { x.checked_add(1).ok_or("Overflow!") }).collect(); assert_eq!(res, Ok(vec![2, 3])); ``` #### Example 2: Collection with an error ```rust let v = vec![1, 2, 0]; let res: Result, &'static str> = v.iter().map(|x: &u32| { x.checked_sub(1).ok_or("Underflow!") }).collect(); assert_eq!(res, Err("Underflow!")); ``` #### Example 3: Demonstrating early return on error ```rust let v = vec![3, 2, 1, 10]; let mut shared = 0; let res: Result, &'static str> = v.iter().map(|x: &u32| { shared += x; x.checked_sub(2).ok_or("Underflow!") }).collect(); assert_eq!(res, Err("Underflow!")); assert_eq!(shared, 6); // Only elements before the error are processed ``` ``` -------------------------------- ### fn hash<__H>(&self, state: &mut __H) -> () Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Feeds the Result value into a given Hasher. This allows Result types to be used in hashing contexts. ```APIDOC ## fn hash<__H>(&self, state: &mut __H) ### Description Feeds this value into the given `Hasher`. ### Parameters - **state** (`&mut __H`) - A mutable reference to a Hasher. ### Type Parameters - **__H**: Must implement the `Hasher` trait. ``` -------------------------------- ### impl Result<&mut T, E>: cloned Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Maps a `Result<&mut T, E>` to a `Result` by cloning the contents of the `Ok` part. Requires `T` to implement `Clone`. ```APIDOC ## impl Result<&mut T, E> ### cloned ```rust pub fn cloned(self) -> Result where T: Clone, ``` Maps a `Result<&mut T, E>` to a `Result` by cloning the contents of the `Ok` part. #### Examples ```rust let mut val = 12; let x: Result<&mut i32, i32> = Ok(&mut val); assert_eq!(x, Ok(&mut 12)); let cloned = x.cloned(); assert_eq!(cloned, Ok(12)); ``` ``` -------------------------------- ### unwrap Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Returns the contained Ok value or panics if the value is an Err. The panic message is provided by the Err's value. ```APIDOC ## unwrap ### Description Returns the contained `Ok` value or panics if the value is an `Err`, with a panic message provided by the `Err`’s value. ### Method `unwrap()` ### Examples ```rust let x: Result = Ok(2); assert_eq!(x.unwrap(), 2); let x: Result = Err("emergency failure"); x.unwrap(); // panics with `emergency failure` ``` ``` -------------------------------- ### Implementing Instrument for T Source: https://docs.rs/diqwest/3.2.0/diqwest/error/enum.Error.html Integrates with tracing spans to add context to operations. Use `instrument` to attach a span to a value. ```rust fn instrument(self, span: Span) -> Instrumented ``` ```rust fn in_current_span(self) -> Instrumented ``` -------------------------------- ### with_subscriber Source: https://docs.rs/diqwest/3.2.0/diqwest/session/struct.Credentials.html Attaches a provided Subscriber to the current type, returning a WithDispatch wrapper. This allows for custom subscriber management. ```APIDOC ## with_subscriber(self, subscriber: S) -> WithDispatch ### Description Attaches the provided `Subscriber` to this type, returning a `WithDispatch` wrapper. ### Parameters - **subscriber** (S): The subscriber to attach. Must implement `Into`. ``` -------------------------------- ### pub fn as_deref(&self) -> Result<&::Target, &E> Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Converts from `Result` to `Result<&::Target, &E>`. Coerces the `Ok` variant of the original `Result` via `Deref`. ```APIDOC ## pub fn as_deref(&self) -> Result<&::Target, &E> ### Description Converts from `Result` (or `&Result`) to `Result<&::Target, &E>`. Coerces the `Ok` variant of the original `Result` via `Deref` and returns the new `Result`. ### Method `as_deref` ### Parameters None ### Request Example ```rust let x: Result = Ok("hello".to_string()); let y: Result<&str, &u32> = Ok("hello"); assert_eq!(x.as_deref(), y); let x: Result = Err(42); let y: Result<&str, &u32> = Err(&42); assert_eq!(x.as_deref(), y); ``` ### Response #### Success Response (200) Returns a `Result` with references to the dereferenced `Ok` value or the original `Err` value. #### Response Example None ``` -------------------------------- ### impl Result<&mut T, E>: copied Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Maps a `Result<&mut T, E>` to a `Result` by copying the contents of the `Ok` part. Requires `T` to implement `Copy`. ```APIDOC ## impl Result<&mut T, E> ### copied ```rust pub const fn copied(self) -> Result where T: Copy, ``` Maps a `Result<&mut T, E>` to a `Result` by copying the contents of the `Ok` part. #### Examples ```rust let mut val = 12; let x: Result<&mut i32, i32> = Ok(&mut val); assert_eq!(x, Ok(&mut 12)); let copied = x.copied(); assert_eq!(copied, Ok(12)); ``` ``` -------------------------------- ### as_mut() Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Converts a mutable reference to a Result into a Result<&mut T, &mut E>, allowing in-place modification. ```APIDOC ## pub const fn as_mut(&mut self) -> Result<&mut T, &mut E> ### Description Converts from `&mut Result` to `Result<&mut T, &mut E>`. ### Method `as_mut()` ### Parameters None ### Request Body None ### Request Example ```rust fn mutate(r: &mut Result) { match r.as_mut() { Ok(v) => *v = 42, Err(e) => *e = 0, } } let mut x: Result = Ok(2); mutate(&mut x); assert_eq!(x.unwrap(), 42); let mut x: Result = Err(13); mutate(&mut x); assert_eq!(x.unwrap_err(), 0); ``` ### Response #### Success Response (Result<&mut T, &mut E>) - Returns `Ok(&mut T)` if the original Result was `Ok(T)`. - Returns `Err(&mut E)` if the original Result was `Err(E)`. #### Response Example ```rust // Allows mutable access to the contained value. ``` ``` -------------------------------- ### Implementing Into for T Source: https://docs.rs/diqwest/3.2.0/diqwest/error/enum.Error.html Allows conversion from type `T` to type `U` if `U` implements `From`. This is a common conversion pattern in Rust. ```rust fn into(self) -> U ``` -------------------------------- ### into_err Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Returns the contained Err value, never panics. This is an experimental API. ```APIDOC ## into_err ### Description Returns the contained `Err` value, but never panics. This method is known to never panic on the result types it is implemented for, making it a maintainability safeguard. ### Method `into_err()` ### Constraints Requires `T: Into` ### Stability This is a nightly-only experimental API. ### Examples ```rust fn only_bad_news() -> Result { Err("Oops, it failed".into()) } let error: String = only_bad_news().into_err(); println!("{error}"); // Output: Oops, it failed ``` ``` -------------------------------- ### Implementing VZip for T Source: https://docs.rs/diqwest/3.2.0/diqwest/error/enum.Error.html A specialized trait for zipping operations, likely related to parallel processing or vectorized computations. ```rust fn vzip(self) -> V ``` -------------------------------- ### impl Result, E>: transpose Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Transposes a `Result` of an `Option` into an `Option` of a `Result`. `Ok(None)` becomes `None`, while `Ok(Some(_))` and `Err(_)` become `Some(Ok(_))` and `Some(Err(_))` respectively. ```APIDOC ## impl Result, E> ### transpose ```rust pub const fn transpose(self) -> Option> ``` Transposes a `Result` of an `Option` into an `Option` of a `Result`. `Ok(None)` will be mapped to `None`. `Ok(Some(_))` and `Err(_)` will be mapped to `Some(Ok(_))` and `Some(Err(_))`. #### Examples ```rust #[derive(Debug, Eq, PartialEq)] struct SomeErr; let x: Result, SomeErr> = Ok(Some(5)); let y: Option> = Some(Ok(5)); assert_eq!(x.transpose(), y); ``` ``` -------------------------------- ### Providing an alternative Result with or Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Use `or` to return the `Ok` value of `self` if it is `Ok`, otherwise return the provided `res` `Result`. Arguments are eagerly evaluated. ```rust let x: Result = Ok(2); let y: Result = Err("late error"); assert_eq!(x.or(y), Ok(2)); let x: Result = Err("early error"); let y: Result = Ok(2); assert_eq!(x.or(y), Ok(2)); let x: Result = Err("not a 2"); let y: Result = Err("late error"); assert_eq!(x.or(y), Err("late error")); let x: Result = Ok(2); let y: Result = Ok(100); assert_eq!(x.or(y), Ok(2)); ``` -------------------------------- ### DigestAuthCredentials for (&str, &str) Source: https://docs.rs/diqwest/3.2.0/diqwest/session/trait.DigestAuthCredentials.html Implementation of DigestAuthCredentials for simple username/password string slices. This implementation does not support caching. ```APIDOC ### impl DigestAuthCredentials for (&str, &str) - `fn username(&self) -> &str` - `fn password(&self) -> &str` - `fn cached_context<'h>(&self, _host: &'h str) -> Result>>` - `fn store_context(&self, _host: &str, _www_authenticate: &str) -> Result<()>` - `fn clear_context(&self, _host: &str) -> Result<()>` ``` -------------------------------- ### Map Ok value with a function Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html The `map()` method applies a function to the contained value of an `Ok` variant, returning a new `Result` with the transformed value. `Err` variants are passed through unchanged. ```rust let line = "1\n2\n3\n4\n"; for num in line.lines() { match num.parse::().map(|i| i * 2) { Ok(n) => println!(/* */ "{}", n), Err(..) => {} } } ``` -------------------------------- ### fn hash_slice(data: &[Self], state: &mut H) -> () Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Feeds a slice of Result values into a given Hasher. This is an efficient way to hash multiple Result values. ```APIDOC ## fn hash_slice(data: &[Self], state: &mut H) ### Description Feeds a slice of this type into the given `Hasher`. ### Parameters - **data** (`&[Self]`) - A slice of Result values to be hashed. - **state** (`&mut H`) - A mutable reference to a Hasher. ### Type Parameters - **H**: Must implement the `Hasher` trait. ``` -------------------------------- ### Implementing ToStringFallible for T Source: https://docs.rs/diqwest/3.2.0/diqwest/error/enum.Error.html Offers a fallible conversion to `String`, avoiding panics on out-of-memory errors. ```rust fn try_to_string(&self) -> Result ``` -------------------------------- ### pub fn inspect(self, f: F) -> Result Source: https://docs.rs/diqwest/3.2.0/diqwest/error/type.Result.html Calls a function with a reference to the contained value if `Ok`. Returns the original result. ```APIDOC ## pub fn inspect(self, f: F) -> Result ### Description Calls a function with a reference to the contained value if `Ok`. Returns the original result. ### Method `inspect` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust let x: u8 = "4" .parse::() .inspect(|x| println!("original: {x}")) .map(|x| x.pow(3)) .expect("failed to parse number"); ``` ### Response #### Success Response (200) Returns the original `Result`. #### Response Example None ``` -------------------------------- ### Send Request with Digest Auth (Blocking) Source: https://docs.rs/diqwest This snippet demonstrates how to send a request with digest authentication using the blocking API. Enable the `blocking` feature in your `Cargo.toml` to use this. ```rust use diqwest::blocking::WithDigestAuth; use reqwest::blocking::Client; let response = Client::new() .get("https://example.com/api") .send_digest_auth(("username", "password"))?; ``` -------------------------------- ### Implementing Same Trait Source: https://docs.rs/diqwest/3.2.0/diqwest/error/enum.Error.html A trait where the output type is the same as the input type. It signifies an identity transformation. ```rust type Output = T ```