use std::error::Error;
use std::fmt;
use std::str::FromStr;
use crate::config_bag::{Storable, StoreReplace};
const WHEN_SUPPORTED: &str = "when_supported";
const WHEN_REQUIRED: &str = "when_required";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum RequestChecksumCalculation {
#[default]
WhenSupported,
WhenRequired,
}
impl Storable for RequestChecksumCalculation {
type Storer = StoreReplace<Self>;
}
impl FromStr for RequestChecksumCalculation {
type Err = UnknownRequestChecksumCalculationError;
fn from_str(request_checksum_calculation: &str) -> Result<Self, Self::Err> {
if request_checksum_calculation.eq_ignore_ascii_case(WHEN_SUPPORTED) {
Ok(Self::WhenSupported)
} else if request_checksum_calculation.eq_ignore_ascii_case(WHEN_REQUIRED) {
Ok(Self::WhenRequired)
} else {
Err(UnknownRequestChecksumCalculationError::new(
request_checksum_calculation,
))
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum ResponseChecksumValidation {
#[default]
WhenSupported,
WhenRequired,
}
impl Storable for ResponseChecksumValidation {
type Storer = StoreReplace<Self>;
}
impl FromStr for ResponseChecksumValidation {
type Err = UnknownResponseChecksumValidationError;
fn from_str(response_checksum_validation: &str) -> Result<Self, Self::Err> {
if response_checksum_validation.eq_ignore_ascii_case(WHEN_SUPPORTED) {
Ok(Self::WhenSupported)
} else if response_checksum_validation.eq_ignore_ascii_case(WHEN_REQUIRED) {
Ok(Self::WhenRequired)
} else {
Err(UnknownResponseChecksumValidationError::new(
response_checksum_validation,
))
}
}
}
#[derive(Debug)]
#[non_exhaustive]
pub struct UnknownRequestChecksumCalculationError {
request_checksum_calculation: String,
}
impl UnknownRequestChecksumCalculationError {
pub(crate) fn new(request_checksum_calculation: impl Into<String>) -> Self {
Self {
request_checksum_calculation: request_checksum_calculation.into(),
}
}
pub fn request_checksum_calculation(&self) -> &str {
&self.request_checksum_calculation
}
}
impl fmt::Display for UnknownRequestChecksumCalculationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
r#"unknown request_checksum_calculation value "{}", please pass a known name ("when_supported", "when_required")"#,
self.request_checksum_calculation
)
}
}
impl Error for UnknownRequestChecksumCalculationError {}
#[derive(Debug)]
#[non_exhaustive]
pub struct UnknownResponseChecksumValidationError {
response_checksum_validation: String,
}
impl UnknownResponseChecksumValidationError {
pub(crate) fn new(response_checksum_validation: impl Into<String>) -> Self {
Self {
response_checksum_validation: response_checksum_validation.into(),
}
}
pub fn response_checksum_validation(&self) -> &str {
&self.response_checksum_validation
}
}
impl fmt::Display for UnknownResponseChecksumValidationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
r#"unknown response_checksum_validation value "{}", please pass a known name ("when_supported", "when_required")"#,
self.response_checksum_validation
)
}
}
impl Error for UnknownResponseChecksumValidationError {}