use http::Method;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Operation {
List,
Create,
Retrieve,
Update,
Delete,
}
impl Operation {
pub const ALL: [Self; 5] = [
Self::List,
Self::Retrieve,
Self::Create,
Self::Update,
Self::Delete,
];
#[must_use]
pub const fn method(self) -> Method {
match self {
Self::List | Self::Retrieve => Method::GET,
Self::Create => Method::POST,
Self::Update => Method::PATCH,
Self::Delete => Method::DELETE,
}
}
#[must_use]
pub const fn on_item(self) -> bool {
matches!(self, Self::Retrieve | Self::Update | Self::Delete)
}
#[must_use]
pub fn from_method(method: &Method, on_item: bool) -> Option<Self> {
Self::ALL
.into_iter()
.find(|op| op.on_item() == on_item && op.method() == method)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Collection {
pub name: &'static str,
pub operations: &'static [Operation],
}
impl Collection {
#[must_use]
pub const fn new(name: &'static str, operations: &'static [Operation]) -> Self {
Self { name, operations }
}
#[must_use]
pub const fn crud(name: &'static str) -> Self {
Self::new(
name,
&[
Operation::List,
Operation::Create,
Operation::Retrieve,
Operation::Update,
Operation::Delete,
],
)
}
#[must_use]
pub const fn read_only(name: &'static str) -> Self {
Self::new(name, &[Operation::List, Operation::Retrieve])
}
#[must_use]
pub const fn task(name: &'static str) -> Self {
Self::new(
name,
&[Operation::List, Operation::Create, Operation::Retrieve],
)
}
#[must_use]
pub fn declares(&self, operation: Operation) -> bool {
self.operations.contains(&operation)
}
#[must_use]
pub fn allows(&self, method: &Method, on_item: bool) -> bool {
Operation::from_method(method, on_item).is_some_and(|op| self.declares(op))
}
#[must_use]
pub fn allowed_methods(&self, on_item: bool) -> Vec<Method> {
Operation::ALL
.into_iter()
.filter(|op| op.on_item() == on_item && self.declares(*op))
.map(Operation::method)
.collect()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Api {
pub name: &'static str,
pub path: &'static str,
pub version: &'static str,
pub collections: &'static [Collection],
}
impl Api {
#[must_use]
pub const fn new(
name: &'static str,
path: &'static str,
version: &'static str,
collections: &'static [Collection],
) -> Self {
Self {
name,
path,
version,
collections,
}
}
#[must_use]
pub fn collection(&self, name: &str) -> Option<&Collection> {
self.collections.iter().find(|c| c.name == name)
}
#[must_use]
pub fn url_on(&self, host: &str) -> String {
format!("{}/{}", host.trim_end_matches('/'), self.path)
}
}
#[cfg(test)]
mod tests {
use super::*;
const BILLS: Api = Api::new(
"TMF678",
"tmf-api/customerBillManagement/v5",
"5.0.0",
&[
Collection::new(
"customerBill",
&[Operation::List, Operation::Retrieve, Operation::Update],
),
Collection::read_only("appliedCustomerBillingRate"),
],
);
#[test]
fn allowed_methods_follow_the_declaration_in_a_stable_order() {
let bills = BILLS.collection("customerBill").unwrap();
assert_eq!(bills.allowed_methods(false), [Method::GET]);
assert_eq!(bills.allowed_methods(true), [Method::GET, Method::PATCH]);
let crud = Collection::crud("x");
assert_eq!(
crud.allowed_methods(true),
[Method::GET, Method::PATCH, Method::DELETE]
);
assert_eq!(crud.allowed_methods(false), [Method::GET, Method::POST]);
}
#[test]
fn a_method_maps_to_an_operation_only_on_its_own_path_form() {
assert_eq!(
Operation::from_method(&Method::POST, false),
Some(Operation::Create)
);
assert_eq!(Operation::from_method(&Method::POST, true), None);
assert_eq!(Operation::from_method(&Method::PUT, true), None);
assert_eq!(Operation::from_method(&Method::PATCH, false), None);
}
#[test]
fn undeclared_collections_and_methods_are_not_allowed() {
assert!(BILLS.collection("listener").is_none());
let bills = BILLS.collection("customerBill").unwrap();
assert!(!bills.allows(&Method::POST, false));
assert!(!bills.allows(&Method::DELETE, true));
assert!(bills.allows(&Method::PATCH, true));
}
#[test]
fn the_base_url_joins_host_and_path_once() {
assert_eq!(
BILLS.url_on("https://host/"),
"https://host/tmf-api/customerBillManagement/v5"
);
}
}