securityctx.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. use ansiterm::Style;
  2. use crate::fs::fields as f;
  3. use crate::output::cell::{TextCell, DisplayWidth};
  4. impl f::SecurityContext<'_> {
  5. pub fn render<C: Colours>(&self, colours: &C) -> TextCell {
  6. match &self.context {
  7. f::SecurityContextType::None => {
  8. TextCell::paint_str(colours.none(), "?")
  9. }
  10. f::SecurityContextType::SELinux(context) => {
  11. let mut chars = Vec::with_capacity(7);
  12. for (i, part) in context.split(':').enumerate() {
  13. let partcolour = match i {
  14. 0 => colours.selinux_user(),
  15. 1 => colours.selinux_role(),
  16. 2 => colours.selinux_type(),
  17. _ => colours.selinux_range()
  18. };
  19. if i > 0 {
  20. chars.push(colours.selinux_colon().paint(":"));
  21. }
  22. chars.push(partcolour.paint(String::from(part)));
  23. }
  24. TextCell {
  25. contents: chars.into(),
  26. width: DisplayWidth::from(context.len())
  27. }
  28. }
  29. }
  30. }
  31. }
  32. pub trait Colours {
  33. fn none(&self) -> Style;
  34. fn selinux_colon(&self) -> Style;
  35. fn selinux_user(&self) -> Style;
  36. fn selinux_role(&self) -> Style;
  37. fn selinux_type(&self) -> Style;
  38. fn selinux_range(&self) -> Style;
  39. }