use crate::graph::Graph;
use crate::projection::flows::{collect_flows, model_namespace};
use crate::projection::ids::{ident, NameRegistrar};
use crate::projection::sink::ArtifactSink;
use serde_json::{json, Map, Value};
use std::collections::{BTreeMap, BTreeSet};
pub const SCHEMA_FILE: &str = "schema.cedarschema.json";
pub const POLICIES_FILE: &str = "policies.cedar";
pub fn emit(
graph: &Graph,
model_ref: &str,
created_at: Option<String>,
sink: &mut ArtifactSink,
) -> Result<Vec<String>, String> {
let created_at = created_at.unwrap_or_else(|| chrono::Utc::now().to_rfc3339());
let ns = model_namespace(graph)?;
let schema = build_schema(graph, &ns)?;
let policies = build_policies(graph, &ns, model_ref, &created_at)?;
sink.write(SCHEMA_FILE, &schema)?;
sink.write(POLICIES_FILE, &policies)?;
Ok(vec![SCHEMA_FILE.to_string(), POLICIES_FILE.to_string()])
}
pub fn project_cedar_in_memory(
graph: &Graph,
model_ref: &str,
created_at: Option<String>,
) -> Result<BTreeMap<String, String>, String> {
let mut map = BTreeMap::new();
let mut sink = ArtifactSink::Memory {
prefix: String::new(),
map: &mut map,
};
emit(graph, model_ref, created_at, &mut sink)?;
Ok(map)
}
fn build_schema(graph: &Graph, ns: &str) -> Result<String, String> {
let mut reg = NameRegistrar::new();
let ns_ident = reg.register("ident", ns);
let mut type_names: Vec<String> = Vec::new();
for e in graph.all_entities() {
type_names.push(reg.register("ident", e.name()));
}
for r in graph.all_resources() {
type_names.push(reg.register("ident", r.name()));
}
type_names.sort();
type_names.dedup();
let mut entity_types: Map<String, Value> = Map::new();
for t in &type_names {
entity_types.insert(t.clone(), json!({}));
}
let flows = collect_flows(graph)?;
let mut by_resource: BTreeMap<String, (BTreeSet<String>, BTreeSet<String>)> = BTreeMap::new();
for f in &flows {
let resource_id = reg.register("ident", &f.resource);
let from_id = reg.register("ident", &f.from);
let to_id = reg.register("ident", &f.to);
let entry = by_resource
.entry(resource_id)
.or_insert_with(|| (BTreeSet::new(), BTreeSet::new()));
entry.0.insert(from_id);
entry.1.insert(to_id);
}
let mut actions: Map<String, Value> = Map::new();
for (resource, (principals, targets)) in &by_resource {
let principal_types: Vec<Value> = principals.iter().map(|p| json!(p)).collect();
let mut annotations: Map<String, Value> = Map::new();
annotations.insert(
"to".to_string(),
json!(targets.iter().cloned().collect::<Vec<_>>().join(",")),
);
let action_name = format!("Issue{resource}");
actions.insert(
action_name,
json!({
"appliesTo": {
"principalTypes": principal_types,
"resourceTypes": [ resource ]
},
"annotations": annotations,
}),
);
}
let mut ns_map: Map<String, Value> = Map::new();
ns_map.insert("entityTypes".to_string(), Value::Object(entity_types));
ns_map.insert("actions".to_string(), Value::Object(actions));
let mut root: Map<String, Value> = Map::new();
root.insert(ns_ident, Value::Object(ns_map));
Ok(serde_json::to_string_pretty(&Value::Object(root)).expect("schema serializes"))
}
fn build_policies(
graph: &Graph,
ns: &str,
model_ref: &str,
created_at: &str,
) -> Result<String, String> {
let mut reg = NameRegistrar::new();
let ns_ident = reg.register("ident", ns);
let flows = collect_flows(graph)?;
let mut pairs: Vec<(String, String)> = flows
.iter()
.map(|f| {
(
reg.register("ident", &f.resource),
reg.register("ident", &f.from),
)
})
.collect();
pairs.sort();
pairs.dedup();
let mut s = String::new();
s.push_str(&format!(
"// Cedar policies projected by DomainForge from {model_ref} at {created_at}.\n\
// PERMISSIVE BASELINE: one scoped `permit` per Action — the authority grant\n\
// that the flow exists in the `{ns_ident}` architecture. Each permit is scoped\n\
// to the flow's source entity type (`principal is <From>`) and resource type\n\
// (`resource is <Resource>`). This does NOT project SEA `policy` expressions;\n\
// tighten with `when`/`unless` clauses derived from SEA `policy` obligations\n\
// (e.g. require_approval) as needed.\n\n"
));
for (resource, from) in &pairs {
s.push_str(&format!(
"permit (\n principal is {ns_ident}::{from},\n action == {ns_ident}::Action::\"Issue{resource}\",\n resource is {ns_ident}::{resource}\n);\n\n"
));
}
Ok(s)
}
pub fn cedar_ident(name: &str) -> String {
ident(name)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::parser::parse_to_graph;
const FIXED_TS: &str = "2026-07-02T00:00:00+00:00";
const SOURCE: &str = r#"
@namespace "procurement"
Entity "Buyer" in procurement
Entity "Supplier" in procurement
Resource "PurchaseOrder" units in procurement
Flow "PurchaseOrder" from "Buyer" to "Supplier" quantity 1
"#;
fn project(source: &str) -> BTreeMap<String, String> {
let graph = parse_to_graph(source).expect("fixture parses");
project_cedar_in_memory(&graph, "test.sea", Some(FIXED_TS.to_string()))
.expect("projection succeeds")
}
fn schema_doc(files: &BTreeMap<String, String>) -> Value {
let body = &files[SCHEMA_FILE];
serde_json::from_str(body).expect("schema parses as JSON")
}
#[test]
fn emits_two_artifacts() {
let files = project(SOURCE);
assert_eq!(
files.keys().collect::<Vec<_>>(),
vec!["policies.cedar", "schema.cedarschema.json"]
);
}
#[test]
fn schema_is_strict_json_no_comment_header() {
let files = project(SOURCE);
let body = &files[SCHEMA_FILE];
let trimmed = body.trim_start();
assert!(
trimmed.starts_with('{'),
"schema must be strict JSON (no comment header); first chars: {:?}",
&body[..body.len().min(40)]
);
let _: Value = serde_json::from_str(body).expect("strict JSON parse");
}
#[test]
fn schema_declares_entity_types_and_one_action_per_resource() {
let files = project(SOURCE);
let d = schema_doc(&files);
let ns_obj = d["procurement"].as_object().expect("namespace present");
let entity_types = ns_obj["entityTypes"]
.as_object()
.expect("entityTypes object");
assert!(entity_types.contains_key("Buyer"));
assert!(entity_types.contains_key("Supplier"));
assert!(entity_types.contains_key("PurchaseOrder"));
let actions = ns_obj["actions"].as_object().expect("actions object");
let action = &actions["IssuePurchaseOrder"];
assert_eq!(action["appliesTo"]["principalTypes"][0], "Buyer");
assert_eq!(action["appliesTo"]["resourceTypes"][0], "PurchaseOrder");
assert_eq!(action["annotations"]["to"], "Supplier");
}
#[test]
fn policies_has_one_scoped_permit_per_action() {
let files = project(SOURCE);
let p = &files[POLICIES_FILE];
assert_eq!(p.matches("permit (").count(), 1);
assert!(
p.contains("principal is procurement::Buyer"),
"permit must scope principal to source entity type (H6): {p}"
);
assert!(
p.contains("resource is procurement::PurchaseOrder"),
"permit must scope resource to resource type (H6): {p}"
);
}
#[test]
fn output_is_deterministic() {
assert_eq!(project(SOURCE), project(SOURCE));
}
#[test]
fn empty_model_emits_empty_types_and_actions() {
let files = project("@namespace \"empty\"\n");
let d = schema_doc(&files);
let ns_obj = d["default"].as_object().expect("default namespace");
assert!(ns_obj["entityTypes"].as_object().unwrap().is_empty());
assert!(ns_obj["actions"].as_object().unwrap().is_empty());
let p = &files[POLICIES_FILE];
assert_eq!(p.matches("permit (").count(), 0);
}
#[test]
fn hostile_names_do_not_corrupt_output() {
let hostile = r#"
@namespace "h"
Entity "a\"b" in h
Resource "R x" units in h
Flow "R x" from "a\"b" to "a\"b" quantity 1
"#;
let files = project(hostile);
let _doc = schema_doc(&files);
let p = &files[POLICIES_FILE];
assert!(p.contains("permit ("), "must have a permit");
assert!(
p.contains("principal is h::a_b"),
"hostile entity name must be sanitized: {p}"
);
}
}