use crate::ConvertError;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Endpoint {
Query,
QueryGroups,
GetPoints,
Facet,
Scroll,
Count,
Upsert,
Delete,
ClearPayload,
DeletePayload,
DeleteVectors,
UpdateVectors,
UpdatePayload,
CreateCollection,
AlterCollection,
DropCollection,
CreateIndex,
DropIndex,
CreateShardKey,
DropShardKey,
ShowShardKeys,
ShowCollections,
ShowCollection,
ShowQuotas,
SetQuota,
QueryBatch,
PointsBatch,
}
impl Endpoint {
pub(crate) fn body_required(self) -> bool {
!matches!(
self,
Self::DropCollection
| Self::DropIndex
| Self::ShowShardKeys
| Self::ShowCollections
| Self::ShowCollection
| Self::ShowQuotas
)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct EndpointMatch {
pub(crate) op: Endpoint,
pub(crate) collection: Option<String>,
pub(crate) field: Option<String>,
}
pub(crate) fn parse(method: &str, path: &str) -> Result<EndpointMatch, ConvertError> {
let trimmed = path.trim_start_matches('/');
let trimmed = trimmed.strip_suffix('/').unwrap_or(trimmed);
let parts: Vec<&str> = trimmed.split('/').collect();
let unsupported = || ConvertError::UnsupportedEndpoint(format!("{method} {trimmed}"));
let collection_of = |parts: &[&str]| -> Result<Option<String>, ConvertError> {
match parts.get(1) {
None | Some(&"") => Err(unsupported()),
Some(name) => {
reject_template_collection(name)?;
Ok(Some((*name).to_string()))
}
}
};
if method.eq_ignore_ascii_case("GET") {
if trimmed == "collections" {
return Ok(EndpointMatch {
op: Endpoint::ShowCollections,
collection: None,
field: None,
});
}
if trimmed == "quotas" {
return Ok(EndpointMatch {
op: Endpoint::ShowQuotas,
collection: None,
field: None,
});
}
}
if method.eq_ignore_ascii_case("PUT") && trimmed == "quotas" {
return Ok(EndpointMatch {
op: Endpoint::SetQuota,
collection: None,
field: None,
});
}
if parts.first() != Some(&"collections") || parts.len() < 2 {
return Err(unsupported());
}
let collection = collection_of(&parts)?;
let rest = &parts[2..];
let op = match (method.to_ascii_uppercase().as_str(), rest) {
("POST", ["points", "query"]) => Endpoint::Query,
("POST", ["points", "query", "groups"]) => Endpoint::QueryGroups,
("POST", ["points"]) => Endpoint::GetPoints,
("POST", ["facet"]) => Endpoint::Facet,
("POST", ["points", "scroll"]) => Endpoint::Scroll,
("POST", ["points", "count"]) => Endpoint::Count,
("PUT", ["points"]) => Endpoint::Upsert,
("POST", ["points", "delete"]) => Endpoint::Delete,
("POST", ["points", "query", "batch"]) => Endpoint::QueryBatch,
("POST", ["points", "batch"]) => Endpoint::PointsBatch,
("POST", ["points", "payload", "clear"]) => Endpoint::ClearPayload,
("POST", ["points", "payload", "delete"]) => Endpoint::DeletePayload,
("POST", ["points", "vectors", "delete"]) => Endpoint::DeleteVectors,
("PUT", ["points", "vectors"]) => Endpoint::UpdateVectors,
("POST", ["points", "payload"]) => Endpoint::UpdatePayload,
("PUT", []) => Endpoint::CreateCollection,
("PATCH", []) => Endpoint::AlterCollection,
("DELETE", []) => Endpoint::DropCollection,
("PUT", ["index"]) => Endpoint::CreateIndex,
("DELETE", ["index", field]) => {
return Ok(EndpointMatch {
op: Endpoint::DropIndex,
collection,
field: Some((*field).to_string()),
});
}
("PUT", ["shards"]) => Endpoint::CreateShardKey,
("POST", ["shards", "delete"]) => Endpoint::DropShardKey,
("GET", ["shards"]) => Endpoint::ShowShardKeys,
("GET", []) => Endpoint::ShowCollection,
_ => return Err(unsupported()),
};
Ok(EndpointMatch {
op,
collection,
field: None,
})
}
pub(crate) fn reject_template_collection(name: &str) -> Result<(), ConvertError> {
if name
.chars()
.any(|c| matches!(c, '<' | '>' | '{' | '}' | '$') || c.is_whitespace())
{
return Err(ConvertError::InvalidField {
path: "collection".to_string(),
detail: format!(
"looks like a placeholder (`{name}`) — replace it with the real collection name"
),
});
}
Ok(())
}