use azure_core::http::Etag;
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Precondition {
IfMatch(Etag),
IfNoneMatch(Etag),
}
impl Precondition {
pub fn if_match(etag: impl Into<Etag>) -> Self {
Self::IfMatch(etag.into())
}
pub fn if_none_match(etag: impl Into<Etag>) -> Self {
Self::IfNoneMatch(etag.into())
}
pub fn as_if_match(&self) -> Option<&Etag> {
match self {
Self::IfMatch(etag) => Some(etag),
Self::IfNoneMatch(_) => None,
}
}
pub fn as_if_none_match(&self) -> Option<&Etag> {
match self {
Self::IfNoneMatch(etag) => Some(etag),
Self::IfMatch(_) => None,
}
}
pub fn is_if_match(&self) -> bool {
matches!(self, Self::IfMatch(_))
}
pub fn is_if_none_match(&self) -> bool {
matches!(self, Self::IfNoneMatch(_))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn if_match_accessors() {
let etag = Etag::from("abc123");
let condition = Precondition::if_match(etag.clone());
assert!(condition.is_if_match());
assert!(!condition.is_if_none_match());
assert_eq!(condition.as_if_match(), Some(&etag));
assert_eq!(condition.as_if_none_match(), None);
}
#[test]
fn if_none_match_accessors() {
let etag = Etag::from("*");
let condition = Precondition::if_none_match(etag.clone());
assert!(!condition.is_if_match());
assert!(condition.is_if_none_match());
assert_eq!(condition.as_if_match(), None);
assert_eq!(condition.as_if_none_match(), Some(&etag));
}
}