From 8f04638f84586befb8ef7796eb6ca73afcfcb6e0 Mon Sep 17 00:00:00 2001 From: Peter Neuroth Date: Thu, 7 Sep 2023 13:17:10 +0200 Subject: [PATCH] rune: Implement Display trait for DefRules It seems handy to be able to print the rule set at least for debugging purposes. Signed-off-by: Peter Neuroth --- libs/gl-client/src/runes.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/libs/gl-client/src/runes.rs b/libs/gl-client/src/runes.rs index 9a7bb373c..7453b374d 100644 --- a/libs/gl-client/src/runes.rs +++ b/libs/gl-client/src/runes.rs @@ -1,3 +1,5 @@ +use std::fmt::Display; + use futhark::{Alternative, Condition, Restriction, Rune, RuneError}; /// Represents an entity that can provide restrictions. @@ -109,6 +111,28 @@ impl<'a> Restricter for DefRules<'a> { } } +impl<'a> Display for DefRules<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + DefRules::ReadOnly => write!(f, "readonly"), + DefRules::Pay => write!(f, "pay"), + DefRules::Add(rules) => { + write!( + f, + "{}", + rules.into_iter().fold(String::new(), |acc, r| { + if acc.is_empty() { + format!("{}", r) + } else { + format!("{}|{}", acc, r) + } + }) + ) + } + } + } +} + /// Creates an `Alternative` based on the provided field, condition, and value. /// /// This function is a shorthand for creating new `Alternative` entities @@ -166,4 +190,14 @@ mod tests { let carved_rune = Rune::from_base64(&carved).unwrap(); assert!(mr.is_authorized(&carved_rune)); } + + #[test] + fn test_defrules_display() { + let r = DefRules::Pay; + assert_eq!(format!("{}", r), "pay"); + let r = DefRules::Add(&[DefRules::Pay]); + assert_eq!(format!("{}", r), "pay"); + let r = DefRules::Add(&[DefRules::Pay, DefRules::ReadOnly]); + assert_eq!(format!("{}", r), "pay|readonly"); + } }