#[non_exhaustive]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum EndpointKind {
IngestDoc,
IngestBulk,
Search,
MultiSearch,
Count,
GetById,
MultiGet,
DeleteById,
DeleteByQuery,
Cursor,
Admin,
Unknown,
}
impl EndpointKind {
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::IngestDoc => "IngestDoc",
Self::IngestBulk => "IngestBulk",
Self::Search => "Search",
Self::MultiSearch => "MultiSearch",
Self::Count => "Count",
Self::GetById => "GetById",
Self::MultiGet => "MultiGet",
Self::DeleteById => "DeleteById",
Self::DeleteByQuery => "DeleteByQuery",
Self::Cursor => "Cursor",
Self::Admin => "Admin",
Self::Unknown => "Unknown",
}
}
#[must_use]
pub fn from_name(name: &str) -> Option<Self> {
match name {
"IngestDoc" => Some(Self::IngestDoc),
"IngestBulk" => Some(Self::IngestBulk),
"Search" => Some(Self::Search),
"MultiSearch" => Some(Self::MultiSearch),
"Count" => Some(Self::Count),
"GetById" => Some(Self::GetById),
"MultiGet" => Some(Self::MultiGet),
"DeleteById" => Some(Self::DeleteById),
"DeleteByQuery" => Some(Self::DeleteByQuery),
"Cursor" => Some(Self::Cursor),
"Admin" => Some(Self::Admin),
"Unknown" => Some(Self::Unknown),
_ => None,
}
}
#[must_use]
pub fn is_tenancy_aware(self) -> bool {
matches!(
self,
Self::IngestDoc
| Self::IngestBulk
| Self::Search
| Self::MultiSearch
| Self::Count
| Self::GetById
| Self::MultiGet
| Self::DeleteById
| Self::DeleteByQuery
| Self::Cursor
)
}
#[must_use]
pub fn is_write(self) -> bool {
matches!(self, Self::IngestDoc | Self::IngestBulk | Self::DeleteById)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn admin_and_unknown_are_not_tenancy_aware() {
assert!(!EndpointKind::Admin.is_tenancy_aware());
assert!(!EndpointKind::Unknown.is_tenancy_aware());
}
#[test]
fn ingest_and_read_paths_are_tenancy_aware() {
for kind in [
EndpointKind::IngestDoc,
EndpointKind::IngestBulk,
EndpointKind::Search,
EndpointKind::MultiSearch,
EndpointKind::Count,
EndpointKind::GetById,
EndpointKind::MultiGet,
EndpointKind::DeleteById,
EndpointKind::Cursor,
] {
assert!(kind.is_tenancy_aware(), "{kind:?} should be tenancy-aware");
}
}
#[test]
fn write_classification_matches_intent() {
assert!(EndpointKind::IngestDoc.is_write());
assert!(EndpointKind::IngestBulk.is_write());
assert!(EndpointKind::DeleteById.is_write());
assert!(!EndpointKind::Search.is_write());
assert!(!EndpointKind::GetById.is_write());
}
#[test]
fn every_kind_round_trips_through_its_name() {
for kind in [
EndpointKind::IngestDoc,
EndpointKind::IngestBulk,
EndpointKind::Search,
EndpointKind::MultiSearch,
EndpointKind::Count,
EndpointKind::GetById,
EndpointKind::MultiGet,
EndpointKind::DeleteById,
EndpointKind::Cursor,
EndpointKind::Admin,
EndpointKind::Unknown,
] {
assert_eq!(EndpointKind::from_name(kind.as_str()), Some(kind));
}
assert_eq!(EndpointKind::from_name("nope"), None);
}
}