Skip to content

Commit

Permalink
rune: Implement Display trait for DefRules
Browse files Browse the repository at this point in the history
It seems handy to be able to print the rule set at least for debugging
purposes.

Signed-off-by: Peter Neuroth <[email protected]>
  • Loading branch information
nepet committed Sep 10, 2023
1 parent 42ca93a commit 8f04638
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions libs/gl-client/src/runes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use futhark::{Alternative, Condition, Restriction, Rune, RuneError};

/// Represents an entity that can provide restrictions.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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");
}
}

0 comments on commit 8f04638

Please sign in to comment.