Skip to main content

agent_policy/load/
mod.rs

1// Load and validate pipeline — implemented in Phase 1
2
3pub mod schema;
4pub mod yaml;
5
6use crate::{error::Result, model::policy::RawPolicy};
7
8/// Load and validate an `agent-policy.yaml` from a string.
9///
10/// Parses YAML, validates against the bundled JSON Schema, and returns the
11/// raw policy struct on success. The caller is responsible for normalization.
12///
13/// # Errors
14///
15/// Returns [`crate::Error::Yaml`] on YAML parse failure or [`crate::Error::Schema`] if the
16/// document does not conform to the bundled JSON Schema.
17///
18/// # Panics
19///
20/// Panics if `RawPolicy` cannot be serialized to a JSON value, which is an
21/// internal invariant that holds as long as all field types implement `Serialize`.
22#[allow(clippy::expect_used)] // RawPolicy derives Serialize; to_value is infallible for these types
23pub fn load_str(input: &str) -> Result<RawPolicy> {
24    let raw = yaml::parse(input)?;
25    let doc = serde_json::to_value(&raw).expect("RawPolicy is always serializable to JSON");
26    schema::validate(&doc)?;
27    Ok(raw)
28}
29
30/// Load and validate an `agent-policy.yaml` from a file path.
31///
32/// # Errors
33///
34/// Returns [`crate::Error::Io`] if the file cannot be read, [`crate::Error::Yaml`] on YAML
35/// parse failure, or [`crate::Error::Schema`] if the document does not conform to
36/// the bundled JSON Schema.
37pub fn load_file(path: &camino::Utf8Path) -> Result<RawPolicy> {
38    let content = std::fs::read_to_string(path).map_err(|e| crate::error::Error::Io {
39        path: path.as_std_path().to_owned(),
40        source: e,
41    })?;
42    load_str(&content)
43}