use std::str::FromStr;
use http::Method;
use parse_display::FromStr as DeriveFromStr;
use serde::{Deserialize, Serialize};
use super::HttpConfigError;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, DeriveFromStr)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum HttpRetryMethodPolicy {
#[default]
#[display("IDEMPOTENT_ONLY")]
#[from_str(regex = "(?i)IDEMPOTENT_ONLY|IDEMPOTENT")]
IdempotentOnly,
#[display("ALL_METHODS")]
#[from_str(regex = "(?i)ALL_METHODS|ALL")]
AllMethods,
#[display("NONE")]
#[from_str(regex = "(?i)NONE|DISABLED")]
None,
}
impl HttpRetryMethodPolicy {
pub(super) fn from_config_value(value: &str) -> Result<Self, HttpConfigError> {
Self::from_str(value.trim()).map_err(|_| {
HttpConfigError::invalid_value(
"method_policy",
format!("Unsupported retry method policy: {value}"),
)
})
}
pub fn allows_method(&self, method: &Method) -> bool {
match self {
Self::IdempotentOnly => matches!(
*method,
Method::GET
| Method::HEAD
| Method::PUT
| Method::DELETE
| Method::OPTIONS
| Method::TRACE
),
Self::AllMethods => true,
Self::None => false,
}
}
}