use crate::options::Region;
use std::time::Duration;
const MIN_END_TO_END_OPERATION_TIMEOUT: Duration = Duration::from_secs(1);
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ContentResponseOnWrite {
Enabled,
#[default]
Disabled,
}
impl From<bool> for ContentResponseOnWrite {
fn from(value: bool) -> Self {
if value {
Self::Enabled
} else {
Self::Disabled
}
}
}
impl From<ContentResponseOnWrite> for bool {
fn from(value: ContentResponseOnWrite) -> Self {
matches!(value, ContentResponseOnWrite::Enabled)
}
}
impl std::str::FromStr for ContentResponseOnWrite {
type Err = azure_core::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"true" | "enabled" => Ok(Self::Enabled),
"false" | "disabled" => Ok(Self::Disabled),
_ => Err(azure_core::Error::with_message(
azure_core::error::ErrorKind::DataConversion,
format!(
"Unknown content response on write value: '{}'. Expected 'true'/'false' or 'enabled'/'disabled'",
s
),
)),
}
}
}
impl std::fmt::Display for ContentResponseOnWrite {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Enabled => f.write_str("Enabled"),
Self::Disabled => f.write_str("Disabled"),
}
}
}
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct EndToEndOperationLatencyPolicy {
timeout: Duration,
}
impl EndToEndOperationLatencyPolicy {
pub fn new(timeout: Duration) -> Self {
Self {
timeout: timeout.max(MIN_END_TO_END_OPERATION_TIMEOUT),
}
}
pub fn timeout(&self) -> Duration {
self.timeout
}
}
impl From<Duration> for EndToEndOperationLatencyPolicy {
fn from(timeout: Duration) -> Self {
Self::new(timeout)
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ExcludedRegions(pub Vec<Region>);
impl ExcludedRegions {
pub fn new() -> Self {
Self::default()
}
pub fn with_region(mut self, region: impl Into<Region>) -> Self {
self.0.push(region.into());
self
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn iter(&self) -> impl Iterator<Item = &Region> {
self.0.iter()
}
}
impl<T: Into<Region>> FromIterator<T> for ExcludedRegions {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
Self(iter.into_iter().map(Into::into).collect())
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum EmulatorServerCertValidation {
#[default]
Enabled,
DangerousDisabled,
}
impl From<bool> for EmulatorServerCertValidation {
fn from(disabled: bool) -> Self {
if disabled {
Self::DangerousDisabled
} else {
Self::Enabled
}
}
}
impl From<EmulatorServerCertValidation> for bool {
fn from(value: EmulatorServerCertValidation) -> Self {
matches!(value, EmulatorServerCertValidation::DangerousDisabled)
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use super::EndToEndOperationLatencyPolicy;
#[test]
fn end_to_end_latency_policy_clamps_timeout_below_one_second() {
let policy = EndToEndOperationLatencyPolicy::new(Duration::from_millis(250));
assert_eq!(policy.timeout(), Duration::from_secs(1));
}
#[test]
fn end_to_end_latency_policy_keeps_timeout_at_or_above_one_second() {
let policy = EndToEndOperationLatencyPolicy::new(Duration::from_secs(2));
assert_eq!(policy.timeout(), Duration::from_secs(2));
}
}