Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
2.0 kB · 70 lines
Rust
at sl/gitmirror
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]pub enum Capability { KnotAcl, RepoDidInput, KeepCommit, AtprotoFirehose,}
impl Capability { const ALL: &'static [Capability] = &[ Capability::KnotAcl, Capability::RepoDidInput, Capability::KeepCommit, Capability::AtprotoFirehose, ];
pub const SERVED: &'static [Capability] = &[ Capability::KnotAcl, Capability::RepoDidInput, Capability::AtprotoFirehose, ];
pub const fn token(self) -> &'static str { match self { Capability::KnotAcl => "knot-acl", Capability::RepoDidInput => "repo-did-input", Capability::KeepCommit => "knot-keepcommit", Capability::AtprotoFirehose => "atproto-firehose", } }
pub fn parse(token: &str) -> Option<Self> { Self::ALL.iter().copied().find(|cap| cap.token() == token) }}
#[cfg(test)]mod tests { use super::*;
#[test] fn every_capability_parses_back_from_its_token() { Capability::ALL.iter().copied().for_each(|cap| { assert_eq!(Capability::parse(cap.token()), Some(cap), "{cap:?}"); }); }
#[test] fn tokens_are_distinct() { let tokens: Vec<&str> = Capability::ALL.iter().map(|cap| cap.token()).collect(); let mut unique = tokens.clone(); unique.sort_unstable(); unique.dedup(); assert_eq!(unique.len(), tokens.len(), "two variants share a token"); }
#[test] fn unknown_tokens_are_rejected() { ["", "knot_acl", "KNOT-ACL", "atproto-firehose "] .iter() .for_each(|token| assert_eq!(Capability::parse(token), None, "{token:?}")); }
#[test] fn served_capabilities_are_real_capabilities() { Capability::SERVED.iter().copied().for_each(|cap| { assert!(Capability::ALL.contains(&cap), "{cap:?} is not in ALL"); }); }}