diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-09 17:27:10 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-09 17:27:10 -0600 |
| commit | c203a32775e10f9a5a8e474ab36631b95cf13bb3 (patch) | |
| tree | 74957b94b0cb69833c1eca4f7a1a2ed39d9bef32 | |
| parent | 9c62299e87a89b48e829eca0491076a82d61263e (diff) | |
refactor: recursively load files and directories
| -rw-r--r-- | src/authorization/cedar_authorizer.rs | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/src/authorization/cedar_authorizer.rs b/src/authorization/cedar_authorizer.rs index 230c10a8..5ed810a7 100644 --- a/src/authorization/cedar_authorizer.rs +++ b/src/authorization/cedar_authorizer.rs @@ -38,24 +38,29 @@ impl CedarAuthorizer { fn load_from( path: &std::path::Path, ) -> Result<cedar_policy::PolicySet, Box<dyn std::error::Error>> { - if !path.exists() || !path.is_dir() { + if !path.exists() { return Ok(cedar_policy::PolicySet::default()); } - let mut policies = cedar_policy::PolicySet::new(); - for entry in fs::read_dir(path)? { - let file_path = entry?.path(); - - if let Some(extension) = file_path.extension() { + if path.is_file() { + if let Some(extension) = path.extension() { if extension == "cedar" { - let content = fs::read_to_string(&file_path)?; - let file_policies = cedar_policy::PolicySet::from_str(&content)?; - policies.merge(&file_policies, true)?; + let content = fs::read_to_string(&path)?; + return Ok(cedar_policy::PolicySet::from_str(&content)?); } } } - Ok(policies) + if !path.is_dir() { + return Ok(cedar_policy::PolicySet::default()); + } + + let mut policies = cedar_policy::PolicySet::new(); + for entry in fs::read_dir(path)? { + let file_path = entry?.path(); + policies.merge(&Self::load_from(&file_path)?, true)?; + } + return Ok(policies); } fn map_from( |
