Unified · Split
| @@ -28,6 +28,15 @@ impl Permission { · 27 unmodified lines | |||
| 28 | }), | 28 | }), |
| 29 | } | 29 | } |
| 30 | } | 30 | } |
| 31 | |||
| 32 | /// Like [`check()`][Self::check()], but degenerates the type to an option to make it more useful in cases where | ||
| 33 | /// `Forbid` shoudn't abort the entire operation. | ||
| 34 | pub fn check_opt<R: std::fmt::Debug>(&self, resource: R) -> Option<R> { | ||
| 35 | match self { | ||
| 36 | Permission::Allow => Some(resource), | ||
| 37 | Permission::Deny | Permission::Forbid => None, | ||
| 38 | } | ||
| 39 | } | ||
| 31 | } | 40 | } |
| 32 | 41 | ||
| 33 | impl Display for Permission { | 42 | impl Display for Permission { |
| @@ -9,4 +9,22 @@ mod trust { · 8 unmodified lines | |||
| 9 | } | 9 | } |
| 10 | } | 10 | } |
| 11 | 11 | ||
| 12 | mod permission { | ||
| 13 | use git_sec::Permission; | ||
| 14 | |||
| 15 | #[test] | ||
| 16 | fn check() { | ||
| 17 | assert_eq!(Permission::Allow.check("hi").unwrap(), Some("hi")); | ||
| 18 | assert_eq!(Permission::Deny.check("hi").unwrap(), None); | ||
| 19 | assert!(Permission::Forbid.check("hi").is_err()); | ||
| 20 | } | ||
| 21 | |||
| 22 | #[test] | ||
| 23 | fn check_opt() { | ||
| 24 | assert_eq!(Permission::Allow.check_opt("hi"), Some("hi")); | ||
| 25 | assert_eq!(Permission::Deny.check_opt("hi"), None); | ||
| 26 | assert_eq!(Permission::Forbid.check_opt("hi"), None); | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 12 | mod identity; | 30 | mod identity; |