From 820e26f9d2d766345b1958847ba311ec84130fc7 Mon Sep 17 00:00:00 2001 From: kevin zhao Date: Thu, 13 Nov 2025 13:47:26 -0500 Subject: [PATCH] better naming in Policy.parse() --- codex-rs/execpolicy2/src/main.rs | 9 ++++++--- codex-rs/execpolicy2/src/parser.rs | 15 ++++++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/codex-rs/execpolicy2/src/main.rs b/codex-rs/execpolicy2/src/main.rs index 9924152daf..c0786a320c 100644 --- a/codex-rs/execpolicy2/src/main.rs +++ b/codex-rs/execpolicy2/src/main.rs @@ -44,8 +44,11 @@ fn cmd_check(policy_path: PathBuf, args: Vec) -> Result<()> { } fn load_policy(policy_path: &Path) -> Result { - let content = fs::read_to_string(policy_path) + let policy_file_contents = fs::read_to_string(policy_path) .with_context(|| format!("failed to read policy at {}", policy_path.display()))?; - let policy_source = policy_path.to_string_lossy(); - Ok(PolicyParser::parse(policy_source.as_ref(), &content)?) + let policy_identifier = policy_path.to_string_lossy(); + Ok(PolicyParser::parse( + policy_identifier.as_ref(), + &policy_file_contents, + )?) } diff --git a/codex-rs/execpolicy2/src/parser.rs b/codex-rs/execpolicy2/src/parser.rs index 12eb2ad6c0..8cd165ea49 100644 --- a/codex-rs/execpolicy2/src/parser.rs +++ b/codex-rs/execpolicy2/src/parser.rs @@ -28,11 +28,20 @@ use crate::rule::validate_not_match_examples; pub struct PolicyParser; impl PolicyParser { - pub fn parse(policy_source: &str, unparsed_policy: &str) -> Result { + /// Parses a policy, tagging parser errors with `policy_identifier` so failures include the + /// identifier alongside line numbers. + pub fn parse( + policy_identifier: &str, + policy_file_contents: &str, + ) -> Result { let mut dialect = Dialect::Extended.clone(); dialect.enable_f_strings = true; - let ast = AstModule::parse(policy_source, unparsed_policy.to_string(), &dialect) - .map_err(|e| Error::Starlark(e.to_string()))?; + let ast = AstModule::parse( + policy_identifier, + policy_file_contents.to_string(), + &dialect, + ) + .map_err(|e| Error::Starlark(e.to_string()))?; let globals = GlobalsBuilder::standard().with(policy_builtins).build(); let module = Module::new();