zynk-gen-ts 0.1.2

Plain TypeScript generator for Zynk API graphs
Documentation
//! Plain TypeScript generator for Zynk API graphs.

pub mod lowering;
pub mod naming;
pub mod printer;
pub mod runtime;

use std::path::PathBuf;

use zynk_codegen::{GeneratedFile, GenerationContext, GenerationResult, Generator};
use zynk_schema::ApiGraph;

/// Generate plain TypeScript client files in memory for the provided API graph.
pub fn generate(graph: &ApiGraph) -> GenerationResult {
    let api = printer::print_api(graph);
    GenerationResult::new(vec![
        GeneratedFile::new(PathBuf::from("api.ts"), api),
        GeneratedFile::new(PathBuf::from("_internal.ts"), runtime::internal_ts()),
    ])
}

/// Concrete generator implementation for the plain TypeScript target.
#[derive(Debug, Default, Clone, Copy)]
pub struct TypeScriptGenerator;

impl TypeScriptGenerator {
    /// Create a TypeScript generator.
    pub fn new() -> Self {
        Self
    }
}

impl Generator for TypeScriptGenerator {
    fn generate(&self, ctx: &GenerationContext) -> GenerationResult {
        generate(ctx.graph)
    }
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use zynk_schema::ApiGraph;

    #[test]
    fn generate_returns_api_and_embedded_runtime_files() {
        let result = super::generate(&ApiGraph::new());

        assert_eq!(result.files.len(), 2);
        assert_eq!(result.files[0].path, PathBuf::from("api.ts"));
        assert_eq!(result.files[1].path, PathBuf::from("_internal.ts"));
        assert!(result.files[0]
            .contents
            .contains("Generated by zynk-gen-ts"));
        assert!(result.files[1]
            .contents
            .contains("export function initBridge"));
    }
}