use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[cfg(feature = "json-schema")]
use schemars::JsonSchema;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub struct ImportDecl {
pub specifier: ImportSpecifier,
pub from_module: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
#[serde(tag = "type")]
pub enum ImportSpecifier {
Named { items: Vec<ImportItem> },
Wildcard { alias: String },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub struct ImportItem {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub alias: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub struct FileMetadata {
#[serde(skip_serializing_if = "Option::is_none")]
pub namespace: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub owner: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub profile: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub imports: Vec<ImportDecl>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub enum PolicyKind {
Constraint,
Derivation,
Obligation,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub enum PolicyModality {
Obligation,
Prohibition,
Permission,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub struct PolicyMetadata {
#[serde(skip_serializing_if = "Option::is_none")]
pub kind: Option<PolicyKind>,
#[serde(skip_serializing_if = "Option::is_none")]
pub modality: Option<PolicyModality>,
#[serde(skip_serializing_if = "Option::is_none")]
pub priority: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub rationale: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub tags: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub enum Severity {
Info,
Warning,
Error,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub struct MetricMetadata {
#[serde(skip_serializing_if = "Option::is_none")]
pub refresh_interval: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub unit: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub threshold: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub severity: Option<Severity>,
#[serde(skip_serializing_if = "Option::is_none")]
pub target: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub window: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub enum TargetFormat {
Calm,
Kg,
Sbvr,
Protobuf,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub struct MappingRule {
pub primitive_type: String,
pub primitive_name: String,
pub target_type: String,
#[serde(default)]
pub fields: HashMap<String, serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub struct ProjectionOverride {
pub primitive_type: String,
pub primitive_name: String,
#[serde(default)]
pub fields: HashMap<String, serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub struct WindowSpec {
pub duration: u64,
pub unit: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub enum BinaryOp {
And,
Or,
Equal,
NotEqual,
GreaterThan,
LessThan,
GreaterThanOrEqual,
LessThanOrEqual,
Plus,
Minus,
Multiply,
Divide,
Contains,
StartsWith,
EndsWith,
Matches,
HasRole,
Before,
After,
During,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub enum UnaryOp {
Not,
Negate,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub enum Quantifier {
ForAll,
Exists,
ExistsUnique,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub enum AggregateFunction {
Count,
Sum,
Min,
Max,
Avg,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
#[serde(tag = "type")]
pub enum Expression {
Literal {
value: serde_json::Value,
},
QuantityLiteral {
value: String,
unit: String,
},
TimeLiteral {
timestamp: String,
},
IntervalLiteral {
start: String,
end: String,
},
Variable {
name: String,
},
GroupBy {
variable: String,
collection: Box<Expression>,
#[serde(skip_serializing_if = "Option::is_none")]
filter: Option<Box<Expression>>,
key: Box<Expression>,
condition: Box<Expression>,
},
Binary {
op: BinaryOp,
left: Box<Expression>,
right: Box<Expression>,
},
Unary {
op: UnaryOp,
operand: Box<Expression>,
},
Cast {
operand: Box<Expression>,
target_type: String,
},
Quantifier {
quantifier: Quantifier,
variable: String,
collection: Box<Expression>,
condition: Box<Expression>,
},
MemberAccess {
object: String,
member: String,
},
Aggregation {
function: AggregateFunction,
collection: Box<Expression>,
#[serde(skip_serializing_if = "Option::is_none")]
field: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
filter: Option<Box<Expression>>,
},
AggregationComprehension {
function: AggregateFunction,
variable: String,
collection: Box<Expression>,
#[serde(skip_serializing_if = "Option::is_none")]
window: Option<WindowSpec>,
predicate: Box<Expression>,
projection: Box<Expression>,
#[serde(skip_serializing_if = "Option::is_none")]
target_unit: Option<String>,
},
RoleReference {
role: String,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub struct SpannedAstNode {
pub node: AstNode,
pub line: usize,
pub column: usize,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
#[serde(tag = "type")]
pub enum AstNode {
Export { declaration: Box<SpannedAstNode> },
Entity {
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
version: Option<String>,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
annotations: HashMap<String, serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
domain: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
body: Option<EntityBody>,
},
Resource {
name: String,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
annotations: HashMap<String, serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
unit_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
domain: Option<String>,
},
Flow {
resource_name: String,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
annotations: HashMap<String, serde_json::Value>,
from_entity: String,
to_entity: String,
#[serde(skip_serializing_if = "Option::is_none")]
quantity: Option<String>,
},
Pattern { name: String, regex: String },
Role {
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
domain: Option<String>,
},
Relation {
name: String,
subject_role: String,
predicate: String,
object_role: String,
#[serde(skip_serializing_if = "Option::is_none")]
via_flow: Option<String>,
},
RoleBinding { role: String, entity: String },
Dimension { name: String },
UnitDeclaration {
symbol: String,
dimension: String,
factor: String,
base_unit: String,
},
Policy {
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
version: Option<String>,
metadata: PolicyMetadata,
expression: Expression,
},
Instance {
name: String,
entity_type: String,
#[serde(default)]
fields: HashMap<String, Expression>,
},
ConceptChange {
name: String,
from_version: String,
to_version: String,
migration_policy: String,
breaking_change: bool,
},
Metric {
name: String,
expression: Expression,
metadata: MetricMetadata,
},
MappingDecl {
name: String,
target: TargetFormat,
rules: Vec<MappingRule>,
},
ProjectionDecl {
name: String,
target: TargetFormat,
overrides: Vec<ProjectionOverride>,
},
Cell {
name: String,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
annotations: HashMap<String, serde_json::Value>,
},
SystemDependency {
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
version: Option<String>,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
annotations: HashMap<String, serde_json::Value>,
},
Runtime {
name: String,
version: String,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
annotations: HashMap<String, serde_json::Value>,
},
Tool {
name: String,
version: String,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
annotations: HashMap<String, serde_json::Value>,
},
DependencySet {
name: String,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
annotations: HashMap<String, serde_json::Value>,
},
Service {
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
version: Option<String>,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
annotations: HashMap<String, serde_json::Value>,
},
Mount {
name: String,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
annotations: HashMap<String, serde_json::Value>,
},
Endpoint {
name: String,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
annotations: HashMap<String, serde_json::Value>,
},
NetworkFlow {
name: String,
from_ref: String,
to_ref: String,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
annotations: HashMap<String, serde_json::Value>,
},
Credential {
name: String,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
annotations: HashMap<String, serde_json::Value>,
},
Record { declaration: RecordDecl },
Enum { declaration: EnumDecl },
Operation { declaration: OperationDecl },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub struct EntityBody {
pub fields: Vec<FieldDecl>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub struct FieldDecl {
pub is_key: bool,
pub name: String,
pub field_type: FieldType,
pub is_optional: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub constraints: Vec<FieldConstraintDecl>,
#[serde(skip_serializing_if = "Option::is_none")]
pub default: Option<Expression>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
#[serde(tag = "kind", content = "data", rename_all = "snake_case")]
pub enum FieldType {
Scalar { symbol: FieldTypeRef },
Quantity { symbol: FieldTypeRef },
Ref { symbol: FieldTypeRef },
List { element: Box<FieldType> },
Named { symbol: FieldTypeRef },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub struct FieldTypeRef {
#[serde(skip_serializing_if = "Option::is_none")]
pub alias: Option<String>,
pub symbol: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
#[serde(tag = "kind", content = "data", rename_all = "snake_case")]
pub enum FieldConstraintDecl {
MinLength { value: u64 },
MaxLength { value: u64 },
MinItems { value: u64 },
MaxItems { value: u64 },
ExclusiveMin { value: String },
ExclusiveMax { value: String },
Min { value: String },
Max { value: String },
Pattern { symbol: String },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub struct RecordDecl {
pub name: String,
pub fields: Vec<FieldDecl>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub struct EnumDecl {
pub name: String,
pub members: Vec<EnumMember>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub struct EnumMember {
pub name: String,
pub wire: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub struct OperationDecl {
pub name: String,
pub clauses: Vec<OperationClause>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
#[serde(tag = "kind", content = "data", rename_all = "snake_case")]
pub enum OperationClause {
Intent {
text: String,
},
Direction {
kind: String,
},
Actor {
actor: String,
},
AccessPublic,
AccessPolicyGoverned {
bindings: Vec<PolicyBinding>,
},
Input {
reference: String,
},
Output {
reference: String,
},
State {
reference: String,
},
Effect {
kind: String,
reference: String,
},
Transaction {
kind: String,
},
Failure {
code: String,
kinds: Vec<String>,
message: String,
},
IdempotencyKeyed {
field: String,
},
IdempotencyInherent,
IdempotencyNotApplicable {
reason: String,
explanation: String,
},
ConcurrencyUnique {
field: String,
},
ConcurrencyOptimistic {
field: String,
},
ConcurrencyReadSnapshot,
EvidenceOperationTrace,
LifecycleSynchronousRequestResponse,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub struct PolicyBinding {
pub policy: String,
pub enforcement_point: String,
pub failure_code: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
pub struct Ast {
pub metadata: FileMetadata,
pub declarations: Vec<SpannedAstNode>,
}
#[cfg(all(test, feature = "json-schema"))]
mod tests {
use super::*;
use schemars::schema_for;
#[test]
#[ignore] fn generate_ast_schema() {
let schema = schema_for!(Ast);
let json = serde_json::to_string_pretty(&schema).expect("Failed to serialize schema");
let schema_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.join("schemas")
.join("ast-v3.schema.json");
std::fs::write(&schema_path, &json).expect("Failed to write schema file");
println!("Schema written to: {}", schema_path.display());
println!("\n{}", json);
}
}