Skip to main content

rescript_openapi/codegen/
mod.rs

1// SPDX-License-Identifier: PMPL-1.0-or-later
2// SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell
3
4//! ReScript code generation from IR
5//!
6//! Generates:
7//! - Type definitions (records, variants, aliases)
8//! - rescript-schema validators
9//! - HTTP client functions using fetch
10
11pub mod client;
12pub mod schema;
13pub mod types;
14
15use anyhow::Result;
16use serde::{Deserialize, Serialize};
17use std::fs;
18use std::path::PathBuf;
19
20#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
21#[serde(rename_all = "kebab-case")]
22pub enum ClientMode {
23    /// OOTB: Includes both the Functor and a default Fetch implementation
24    Full,
25    /// Streamlined: Includes only the Functor (user must provide their own HttpClient)
26    FunctorOnly,
27    /// Minimum: Generates no HTTP client code at all
28    None,
29}
30
31#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
32#[serde(rename_all = "kebab-case")]
33pub enum VariantMode {
34    /// Use polymorphic variants: [ #Admin | #User ]
35    Polymorphic,
36    /// Use standard variants with @as: | @as("admin") Admin | @as("user") User
37    Standard,
38}
39
40pub struct Config {
41    pub output_dir: PathBuf,
42    pub module_prefix: String,
43    pub generate_schema: bool,
44    pub generate_client: bool,
45    pub unified_module: bool,
46    pub client_mode: ClientMode,
47    pub variant_mode: VariantMode,
48}
49
50#[derive(Debug, Clone, Serialize, Deserialize)]
51pub struct ProjectConfig {
52    pub input: Option<PathBuf>,
53    pub output: Option<PathBuf>,
54    pub module: Option<String>,
55    pub with_schema: Option<bool>,
56    pub with_client: Option<bool>,
57    pub unified: Option<bool>,
58    pub client_mode: Option<ClientMode>,
59    pub variant_mode: Option<VariantMode>,
60}
61
62impl ProjectConfig {
63    pub fn load(path: &std::path::Path) -> Result<Self> {
64        let content = fs::read_to_string(path)?;
65        let config: Self = toml::from_str(&content)?;
66        Ok(config)
67    }
68}
69