mod schema;
mod targets;
use serde::Deserialize;
use thiserror::Error;
pub use schema::{ContractSchema, Property, PropertyKind};
#[derive(Debug, Error)]
pub enum CompileError {
#[error("contract schema is not valid JSON Schema 2020-12: {message}")]
InvalidSchema { message: String },
#[error(
"construct '{construct}' does not round-trip to all targets; \
use one of the supported alternatives: {alternatives}"
)]
Unrepresentable {
construct: String,
alternatives: String,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Target {
Zod,
Pydantic,
Rust,
Sql,
}
#[derive(Debug)]
pub struct CompileRequest {
pub contract_name: String,
pub schema_json: String,
pub target: Target,
pub epoch: u32,
}
#[derive(Debug)]
pub struct CompiledBinding {
pub content: String,
}
pub fn compile(request: &CompileRequest) -> Result<CompiledBinding, CompileError> {
let schema = schema::parse(&request.contract_name, &request.schema_json)?;
let content = match request.target {
Target::Zod => targets::zod::emit(&schema, request.epoch),
Target::Pydantic => targets::pydantic::emit(&schema, request.epoch),
Target::Rust => targets::rust::emit(&targets::rust::EmitInput {
schema: &schema,
raw_schema_json: &request.schema_json,
epoch: request.epoch,
})?,
Target::Sql => targets::sql::emit(&schema, request.epoch),
};
Ok(CompiledBinding { content })
}