mod auth;
mod billing;
mod database;
mod extension;
mod json;
mod seed;
mod service;
mod ui;
mod validation;
mod views;
mod workflow;
pub use auth::AuthIr;
pub use billing::{BillingIr, BillingPlanIr};
pub use database::{DatabaseDevMode, DatabaseIr, DatabaseMigrationPolicy, DatabaseProvider};
pub use extension::{CommandIr, OperationTypeIr, PageIr, QueryIr, ValueFieldIr, ValueObjectIr};
pub use json::{IrJsonError, from_json};
pub use seed::SeedIr;
use serde::{Deserialize, Serialize};
pub use service::{
ActivityIr, ActivityResourceIr, AuditIr, FileIr, FileProviderIr, FrameOptionsIr, HstsIr,
JobQueueIr, JobScheduleIr, JobsIr, MailIr, MailProviderIr, MailTemplateIr, PresetIr,
RealtimeIr, ReferrerPolicyIr, ReportIr, ReportRendererIr, ReportTemplateIr, SecurityHeadersIr,
ServerIr, TenantIr, WebhookEndpointIr, WebhooksIr,
};
use std::fmt;
pub use ui::FieldSemanticIr;
pub use validation::aggregates::validate_aggregates;
pub use validation::charts::validate_charts;
pub use validation::{IrValidationError, IrValidationErrors, validate_app_ir, validate_form_rules};
pub use views::{
AggregateIr, ChartDimensionIr, ChartIr, ChartKindIr, ChartMeasureIr, EntityViewsIr,
};
pub use workflow::{WorkflowIr, WorkflowTransitionIr};
pub const IR_VERSION: u32 = appstruct_contracts::IR.current;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct AppIr {
pub ir_version: u32,
pub app: AppMeta,
pub database: DatabaseIr,
pub preset: Option<PresetIr>,
pub auth: AuthIr,
pub billing: BillingIr,
#[serde(default, skip_serializing_if = "ServerIr::is_default")]
pub server: ServerIr,
pub tenant: TenantIr,
pub audit: AuditIr,
pub mail: MailIr,
pub jobs: JobsIr,
#[serde(default, skip_serializing_if = "WebhooksIr::is_disabled")]
pub webhooks: WebhooksIr,
#[serde(default, skip_serializing_if = "RealtimeIr::is_disabled")]
pub realtime: RealtimeIr,
pub file: FileIr,
#[serde(default, skip_serializing_if = "ReportIr::is_disabled")]
pub report: ReportIr,
#[serde(default, skip_serializing_if = "ActivityIr::is_disabled")]
pub activity: ActivityIr,
pub enums: Vec<EnumIr>,
pub value_objects: Vec<ValueObjectIr>,
pub entities: Vec<EntityIr>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub seeds: Vec<SeedIr>,
pub relations: Vec<RelationIr>,
pub commands: Vec<CommandIr>,
pub queries: Vec<QueryIr>,
pub pages: Vec<PageIr>,
pub modules: Vec<ResolvedModule>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct AppMeta {
pub name: String,
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct EntityId(pub String);
impl fmt::Display for EntityId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(formatter)
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct FieldId(pub String);
impl fmt::Display for FieldId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(formatter)
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct RelationId(pub String);
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct EntityIr {
pub id: EntityId,
pub rust_name: String,
pub api_name: String,
pub label: String,
pub table_name: String,
pub fields: Vec<FieldIr>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub indexes: Vec<IndexIr>,
pub access: CrudAccessIr,
pub views: EntityViewsIr,
pub hooks: HooksIr,
pub concurrency: ConcurrencyIr,
pub tenant_scoped: bool,
pub audit_enabled: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub workflow: Option<WorkflowIr>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct IndexIr {
pub id: String,
pub entity: EntityId,
pub fields: Vec<FieldId>,
pub unique: bool,
#[serde(default, skip_serializing_if = "IndexMethod::is_default")]
pub method: IndexMethod,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub operator_class: Option<IndexOperatorClass>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub predicate: Option<String>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum IndexMethod {
#[default]
Btree,
Gin,
}
impl IndexMethod {
#[must_use]
pub const fn is_default(&self) -> bool {
matches!(self, Self::Btree)
}
#[must_use]
pub const fn sql(self) -> &'static str {
match self {
Self::Btree => "btree",
Self::Gin => "gin",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum IndexOperatorClass {
GinTrgmOps,
JsonbOps,
JsonbPathOps,
}
impl IndexOperatorClass {
#[must_use]
pub const fn sql(self) -> &'static str {
match self {
Self::GinTrgmOps => "gin_trgm_ops",
Self::JsonbOps => "jsonb_ops",
Self::JsonbPathOps => "jsonb_path_ops",
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct FieldIr {
pub id: FieldId,
pub entity: EntityId,
pub rust_name: String,
pub api_name: String,
pub column_name: String,
pub ty: FieldTypeIr,
pub nullable: bool,
pub primary_key: bool,
pub unique: bool,
pub generated: Option<GeneratedValueIr>,
pub default: Option<String>,
pub validation: ValidationIr,
pub capabilities: FieldCapabilities,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub read_access: Option<AccessRuleIr>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub write_access: Option<AccessRuleIr>,
pub ui_component: Option<String>,
pub ui_semantic: Option<FieldSemanticIr>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum FieldTypeIr {
Uuid,
String,
Text,
Integer,
Bigint,
Decimal,
Boolean,
Date,
Datetime,
Json,
Enum { values: Vec<String> },
Relation { target: EntityId },
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum GeneratedValueIr {
UuidV7,
Now,
AutoIncrement,
Revision,
Tenant,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ValidationIr {
pub min_length: Option<u64>,
pub max_length: Option<u64>,
pub minimum: Option<String>,
pub maximum: Option<String>,
pub required_when: Option<RequiredWhenIr>,
pub greater_than: Option<FieldId>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct RequiredWhenIr {
pub field: FieldId,
pub equals: ConditionValueIr,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ConditionValueIr {
Boolean(bool),
String(String),
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct FieldCapabilities {
pub searchable: bool,
pub filterable: bool,
pub sortable: bool,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct RelationIr {
pub id: RelationId,
pub source: EntityId,
pub target: EntityId,
pub cardinality: Cardinality,
pub foreign_key_owner: EntityId,
pub foreign_key_fields: Vec<FieldId>,
pub inverse: Option<RelationId>,
pub required: bool,
pub unique: bool,
pub on_delete: OnDeleteIr,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Cardinality {
OneToOne,
ManyToOne,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum OnDeleteIr {
Restrict,
Cascade,
SetNull,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CrudAccessIr {
pub list: AccessRuleIr,
pub read: AccessRuleIr,
pub create: AccessRuleIr,
pub update: AccessRuleIr,
pub delete: AccessRuleIr,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "mode", rename_all = "snake_case")]
pub enum AccessRuleIr {
Public,
Authenticated,
Role { role: String },
Owner { field: FieldId },
Any { rules: Vec<AccessRuleIr> },
All { rules: Vec<AccessRuleIr> },
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct HooksIr {}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ConcurrencyIr {
pub enabled: bool,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct EnumIr {
pub name: String,
pub values: Vec<String>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ResolvedModule {
pub name: String,
pub version: String,
pub origin: ModuleOrigin,
pub manifest_path: Option<String>,
pub content_sha256: Option<String>,
pub provides: Vec<String>,
pub requires: Vec<String>,
pub startup_order: u32,
pub artifacts: Vec<ModuleArtifactIr>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ModuleOrigin {
Official,
Local,
Remote,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ModuleArtifactIr {
pub path: String,
pub source: Option<String>,
pub sha256: String,
pub byte_len: u64,
pub content: String,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct SourceSpan {
pub file: String,
pub start: usize,
pub end: usize,
pub line: usize,
pub column: usize,
pub end_line: usize,
pub end_column: usize,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Severity {
Error,
Warning,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Label {
pub span: SourceSpan,
pub message: String,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Diagnostic {
pub severity: Severity,
pub code: String,
pub message: String,
pub primary: Box<Label>,
pub secondary: Vec<Label>,
pub help: Option<String>,
}
impl Diagnostic {
#[must_use]
pub fn error(code: &str, message: impl Into<String>, span: SourceSpan) -> Self {
Self {
severity: Severity::Error,
code: code.to_owned(),
message: message.into(),
primary: Box::new(Label {
span,
message: String::new(),
}),
secondary: Vec::new(),
help: None,
}
}
#[must_use]
pub fn warning(code: &str, message: impl Into<String>, span: SourceSpan) -> Self {
Self {
severity: Severity::Warning,
code: code.to_owned(),
message: message.into(),
primary: Box::new(Label {
span,
message: String::new(),
}),
secondary: Vec::new(),
help: None,
}
}
#[must_use]
pub fn with_primary_message(mut self, message: impl Into<String>) -> Self {
self.primary.message = message.into();
self
}
#[must_use]
pub fn with_secondary(mut self, span: SourceSpan, message: impl Into<String>) -> Self {
self.secondary.push(Label {
span,
message: message.into(),
});
self
}
#[must_use]
pub fn with_help(mut self, help: impl Into<String>) -> Self {
self.help = Some(help.into());
self
}
}
pub fn to_canonical_json(ir: &AppIr) -> Result<String, serde_json::Error> {
let mut output = serde_json::to_string_pretty(ir)?;
output.push('\n');
Ok(output)
}