axum_gate/authz/access_hierarchy.rs
1/// Marker trait representing a linear privilege hierarchy using Rust's derived ordering.
2///
3/// Semantics:
4/// - Implementors MUST derive (or implement) `Ord`, `PartialOrd`, `Eq`, `PartialEq`, `Copy`.
5/// - Ordering direction: HIGHER privilege > LOWER privilege
6/// (i.e. the greatest / largest value in ordering terms is the most privileged).
7/// - The "baseline" (least privileged authenticated role) is therefore the MIN element.
8///
9/// Baseline Role:
10/// - The baseline (lowest privilege) role MUST be returned by `Default::default()`.
11/// - Implement `Default` for your role enum to return the least privileged variant.
12///
13/// Rationale:
14/// Using total ordering plus `Default` gives:
15/// - Constant‑time privilege comparisons
16/// - Clear, compiler‑enforced hierarchy
17/// - A uniform way (`R::default()`) to obtain the baseline for helpers like `require_login()`
18///
19/// Supervisor / Hierarchy Checks:
20/// - A role A is the same or a supervisor (higher privilege) of role B if `A >= B`.
21/// - Policies must use `user_role >= required_role`.
22///
23/// Example:
24/// ```
25/// #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
26/// enum Role { #[default] User, Reporter, Moderator, Admin } // Admin highest
27///
28/// assert!(Role::Admin > Role::Moderator);
29/// assert!(Role::Moderator > Role::User);
30/// assert_eq!(Role::default(), Role::User);
31/// // Supervisor check (Admin supervises User): Role::Admin >= Role::User
32/// ```
33///
34/// **NOTE**: Reordering variants changes access semantics and is a breaking change.
35pub trait AccessHierarchy: Copy + Eq + Ord + Default {}