mod access;
mod activity;
mod api;
mod audit;
mod auth;
mod billing;
mod context;
mod embedded;
mod entity;
mod extensions;
mod file;
mod jobs;
mod mail;
mod manifest;
mod operations;
mod query;
mod realtime;
mod report;
mod runtime;
mod scalar;
mod security;
mod startup;
mod tenant;
mod validation;
mod webhooks;
use crate::{Artifact, ArtifactKind, CodegenError, format_rust, generated_header};
use appstruct_ir::{AppIr, EntityIr, FieldTypeIr};
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;
use syn::LitStr;
pub(crate) fn plan(ir: &AppIr) -> Result<Vec<Artifact>, CodegenError> {
let mut artifacts = vec![
Artifact::text(
"backend/Cargo.toml",
manifest::cargo(ir),
ArtifactKind::RustManifest,
),
Artifact::text(
"backend/core/Cargo.toml",
manifest::core_cargo(),
ArtifactKind::RustManifest,
),
Artifact::text(
"backend/runtime/Cargo.toml",
manifest::runtime_cargo(),
ArtifactKind::RustManifest,
),
Artifact::text(
"backend/contracts/Cargo.toml",
manifest::contracts_cargo(),
ArtifactKind::RustManifest,
),
Artifact::text(
"backend/contracts/src/lib.rs",
embedded::crate_source(appstruct_contracts::__source::LIB),
ArtifactKind::RustSource,
),
];
artifacts.extend(embedded::core_artifacts());
artifacts.extend(embedded::runtime_artifacts());
artifacts.extend([
Artifact::text(
"server/Cargo.toml",
manifest::server_cargo(),
ArtifactKind::RustManifest,
),
Artifact::text(
"server/src/main.rs",
rust_template(include_str!("../templates/backend/server_main.rs"))?,
ArtifactKind::RustSource,
),
Artifact::text(
"backend/src/error.rs",
rust_template(include_str!("../templates/backend/error.rs"))?,
ArtifactKind::RustSource,
),
Artifact::text(
"backend/src/logging.rs",
rust_template(include_str!("../templates/backend/logging.rs"))?,
ArtifactKind::RustSource,
),
database_artifact()?,
Artifact::text(
"backend/src/import_batches.rs",
rust_template(include_str!("../templates/backend/import_batches.rs"))?,
ArtifactKind::RustSource,
),
Artifact::text(
"backend/src/database/query.rs",
rust_template(include_str!("../templates/backend/database/query.rs"))?,
ArtifactKind::RustSource,
),
runtime::metrics_artifact()?,
Artifact::text(
"backend/src/entities/mod.rs",
module_source(ir)?,
ArtifactKind::RustSource,
),
Artifact::text(
"backend/src/api/mod.rs",
module_source(ir)?,
ArtifactKind::RustSource,
),
Artifact::text(
"backend/src/lib.rs",
library_source(ir)?,
ArtifactKind::RustSource,
),
Artifact::text(
"backend/src/openapi.rs",
crate::openapi::rust_source(ir)?,
ArtifactKind::RustSource,
),
Artifact::text(
"backend/src/extensions.rs",
extensions::source(ir)?,
ArtifactKind::RustSource,
),
Artifact::text(
"backend/src/operations.rs",
operations::source(ir)?,
ArtifactKind::RustSource,
),
]);
artifacts.extend(service_artifacts(ir)?);
Ok(artifacts)
}
fn entity_artifacts(ir: &AppIr) -> Result<Vec<Artifact>, CodegenError> {
if ir.entities.is_empty() {
return Ok(Vec::new());
}
let workers = std::thread::available_parallelism()
.map_or(1, usize::from)
.min(ir.entities.len());
let chunk_size = ir.entities.len().div_ceil(workers);
std::thread::scope(|scope| {
let handles = ir
.entities
.chunks(chunk_size)
.map(|entities| {
scope.spawn(move || {
entities
.iter()
.map(|entity| plan_entity(ir, entity))
.collect::<Result<Vec<_>, _>>()
})
})
.collect::<Vec<_>>();
let mut artifacts = Vec::with_capacity(ir.entities.len() * 2);
for handle in handles {
let planned = handle
.join()
.map_err(|_| CodegenError::new("backend generation worker panicked"))??;
artifacts.extend(planned.into_iter().flatten());
}
Ok(artifacts)
})
}
fn plan_entity(ir: &AppIr, entity: &EntityIr) -> Result<[Artifact; 2], CodegenError> {
let module = module_name(entity);
let entity_source = entity::source(ir, entity)
.map_err(|error| CodegenError::new(format!("entity `{module}` failed: {error}")))?;
let api_source = api::source(ir, entity)
.map_err(|error| CodegenError::new(format!("API `{module}` failed: {error}")))?;
Ok([
Artifact::text(
format!("backend/src/entities/{module}.rs"),
entity_source,
ArtifactKind::RustSource,
),
Artifact::text(
format!("backend/src/api/{module}.rs"),
api_source,
ArtifactKind::RustSource,
),
])
}
fn module_source(ir: &AppIr) -> Result<String, CodegenError> {
let modules = ir
.entities
.iter()
.map(|entity| format!("pub mod {};", module_name(entity)))
.collect::<Vec<_>>()
.join("\n");
format_rust(&format!("{}{}\n", generated_header("//"), modules))
}
fn library_source(ir: &AppIr) -> Result<String, CodegenError> {
let routes = ir
.entities
.iter()
.map(|entity| {
let module = parse_ident(&module_name(entity))?;
let path = LitStr::new(&format!("/api/{}/", entity.table_name), Span::call_site());
Ok(quote! { .nest(#path, api::#module::router()) })
})
.collect::<Result<Vec<_>, CodegenError>>()?;
let auth_exports = if ir.auth.enabled {
quote! { pub use auth::{AuthMailSender, AuthState, DevMailSender, SmtpMailSender}; }
} else {
quote! { pub use auth::AuthState; }
};
let service_exports = service_exports(ir);
let runtime = runtime::source(ir, &routes)?;
let runtime_api_version = appstruct_runtime::RUNTIME_API_VERSION;
render(quote! {
pub mod database;
pub mod api;
pub mod entities;
pub mod extensions;
pub mod audit;
mod activity;
mod auth;
mod billing;
mod error;
mod file;
mod jobs;
mod import_batches;
mod logging;
mod mail;
mod metrics;
mod openapi;
mod operations;
mod realtime;
mod report;
mod tenant;
mod webhooks;
pub use error::{ApiError, FieldViolation};
pub use logging::{LoggingError, init_tracing};
pub use appstruct_runtime::{
Actor, BackgroundTaskExit, BackgroundTaskExitKind, BackgroundTaskObserver,
BulkDeleteInput, BulkFailure, BulkResult, BulkUpdateInput, CSV_EXPORT_PAGE_SIZE,
CsvError, ListMeta, ListQuery, ListResponse, MAX_BULK_ITEMS, MAX_CSV_EXPORT_ROWS,
MAX_CSV_IMPORT_ROWS, ModuleDescriptor, ModuleEvent, ModuleObserver, ModulePhase,
ModulePlan, ModuleRuntime, ModuleStarter, RUNTIME_API_VERSION, ServiceHandle,
ServiceHandles, ShutdownError, ShutdownFailure, ShutdownFailureKind, ShutdownReport,
StartupError, SupervisedTaskHandle, TenantId, bulk_failure,
bulk_request_size_is_valid, csv_cell, csv_escape, csv_json_value, decode_cursor,
encode_cursor, parse_csv_rows, parse_revision_etag, revision_etag,
};
pub const GENERATED_RUNTIME_API_VERSION: u32 = #runtime_api_version;
const _: [(); GENERATED_RUNTIME_API_VERSION as usize] =
[(); appstruct_runtime::RUNTIME_API_VERSION as usize];
pub use extensions::{AppExtensions, HookOperation, RequestContext};
#service_exports
#auth_exports
#runtime
})
}
fn service_exports(ir: &AppIr) -> TokenStream {
let mail_job_exports = (ir.jobs.enabled && ir.mail.enabled).then(|| {
quote! { pub use jobs::{MailJobHandler, MailJobPayload}; }
});
let report_exports = ir.report.enabled.then(|| {
quote! { pub use report::{ReportRun, ReportTemplate}; }
});
let activity_exports = ir.activity.enabled.then(|| {
quote! { pub use activity::ActivityEntry; }
});
quote! {
pub use file::{FileError, FileMetadata, FileProvider, FileState};
pub use jobs::{
Job, JobError, JobHandler, JobHandlerError, JobReceipt, JobWorker, JobWorkerHandle,
};
#mail_job_exports
pub use mail::{MailDelivery, MailError, MailMessage, MailProvider, MailState};
pub use realtime::{RealtimeEvent, RealtimeState};
#report_exports
#activity_exports
pub use webhooks::{WebhookError, WebhookReceipt, WebhookWorker, WebhookWorkerHandle};
}
}
pub(super) fn rust_type(field_type: &FieldTypeIr) -> TokenStream {
match field_type {
FieldTypeIr::Uuid | FieldTypeIr::Relation { .. } => quote! { uuid::Uuid },
FieldTypeIr::String | FieldTypeIr::Text | FieldTypeIr::Enum { .. } => quote! { String },
FieldTypeIr::Integer => quote! { i32 },
FieldTypeIr::Bigint => quote! { i64 },
FieldTypeIr::Decimal => quote! { rust_decimal::Decimal },
FieldTypeIr::Boolean => quote! { bool },
FieldTypeIr::Date => quote! { chrono::NaiveDate },
FieldTypeIr::Datetime => quote! { chrono::DateTime<chrono::Utc> },
FieldTypeIr::Json => quote! { serde_json::Value },
}
}
pub(super) fn optional_type(base: TokenStream, nullable: bool) -> TokenStream {
if nullable {
quote! { Option<#base> }
} else {
base
}
}
pub(super) fn find_entity<'ir>(ir: &'ir AppIr, id: &str) -> Result<&'ir EntityIr, CodegenError> {
ir.entities
.iter()
.find(|entity| entity.id.0 == id)
.ok_or_else(|| CodegenError::new(format!("missing entity `{id}`")))
}
pub(super) fn module_name(entity: &EntityIr) -> String {
let mut output = String::new();
for (index, character) in entity.rust_name.chars().enumerate() {
if character.is_ascii_uppercase() {
if index > 0 {
output.push('_');
}
output.push(character.to_ascii_lowercase());
} else {
output.push(character);
}
}
output
}
pub(super) fn parse_ident(value: &str) -> Result<Ident, CodegenError> {
syn::parse_str(value)
.map_err(|error| CodegenError::new(format!("invalid Rust identifier `{value}`: {error}")))
}
pub(super) fn render(tokens: TokenStream) -> Result<String, CodegenError> {
if tokens.is_empty() {
return Err(CodegenError::new("generated Rust source was empty"));
}
let source = tokens.into_iter().collect::<TokenStream>();
Ok(format!("{}{}", generated_header("//"), source))
}
fn rust_template(source: &str) -> Result<String, CodegenError> {
format_rust(&format!("{}{}", generated_header("//"), source))
}
fn database_artifact() -> Result<Artifact, CodegenError> {
Ok(Artifact::text(
"backend/src/database.rs",
rust_template(include_str!("../templates/backend/database.rs"))?,
ArtifactKind::RustSource,
))
}
fn service_artifacts(ir: &AppIr) -> Result<Vec<Artifact>, CodegenError> {
let mut artifacts = Vec::new();
artifacts.extend(audit::plan(ir)?);
artifacts.extend(activity::plan(ir)?);
artifacts.extend(billing::plan(ir)?);
artifacts.extend(auth::plan(ir)?);
artifacts.extend(file::plan(ir)?);
artifacts.extend(jobs::plan(ir)?);
artifacts.extend(mail::plan(ir)?);
artifacts.extend(realtime::plan(ir)?);
artifacts.extend(report::plan(ir)?);
artifacts.extend(tenant::plan(ir)?);
artifacts.extend(webhooks::plan(ir)?);
artifacts.extend(entity_artifacts(ir)?);
Ok(artifacts)
}