Skip to main content

qubit_http/options/
http_retry_method_policy.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10
11use std::str::FromStr;
12
13use http::Method;
14use parse_display::FromStr as DeriveFromStr;
15use serde::{Deserialize, Serialize};
16
17use super::HttpConfigError;
18
19/// HTTP method policy used to decide whether a request can be retried.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, DeriveFromStr)]
21#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
22pub enum HttpRetryMethodPolicy {
23    /// Retry only HTTP methods that are safe to replay by default.
24    #[default]
25    #[display("IDEMPOTENT_ONLY")]
26    #[from_str(regex = "(?i)IDEMPOTENT_ONLY|IDEMPOTENT")]
27    IdempotentOnly,
28    /// Retry all HTTP methods, including `POST` and `PATCH`.
29    #[display("ALL_METHODS")]
30    #[from_str(regex = "(?i)ALL_METHODS|ALL")]
31    AllMethods,
32    /// Disable method-level retry eligibility.
33    #[display("NONE")]
34    #[from_str(regex = "(?i)NONE|DISABLED")]
35    None,
36}
37
38impl HttpRetryMethodPolicy {
39    pub(super) fn from_config_value(value: &str) -> Result<Self, HttpConfigError> {
40        Self::from_str(value.trim()).map_err(|_| {
41            HttpConfigError::invalid_value(
42                "method_policy",
43                format!("Unsupported retry method policy: {value}"),
44            )
45        })
46    }
47
48    /// Returns whether the retry executor permits replaying `method`.
49    ///
50    /// # Parameters
51    /// - `method`: HTTP method to evaluate.
52    ///
53    /// # Returns
54    /// `true` when automatic retry may replay the method.
55    pub fn allows_method(&self, method: &Method) -> bool {
56        match self {
57            Self::IdempotentOnly => matches!(
58                *method,
59                Method::GET
60                    | Method::HEAD
61                    | Method::PUT
62                    | Method::DELETE
63                    | Method::OPTIONS
64                    | Method::TRACE
65            ),
66            Self::AllMethods => true,
67            Self::None => false,
68        }
69    }
70}