gluer 0.9.2

A wrapper for Rust frameworks that eliminates redundant type and function definitions between the frontend and backend
Documentation
use std::{collections::HashMap, path::PathBuf};

use crate::{
    parsing::{
        generate::Route,
        rust::{FnInfo, TypeCategory},
    },
    util::{PKG_NAME, PKG_REPOSITORY, PKG_VERSION},
};

pub(crate) mod js;
pub(crate) mod ts;

pub fn generated_comment() -> String {
    format!(
        "// Generated by {} v{}{}\n// Do not edit this file manually; any changes will be overwritten on the next build.\n",
        PKG_NAME, PKG_VERSION, PKG_REPOSITORY
    )
}

pub fn get_prefix(prefix: String) -> String {
    format!("const PREFIX = \"{}\";\n", prefix)
}

pub fn generate_docstring(docs: &[String], ident: &str) -> String {
    let mut docstring = String::new();
    if !docs.is_empty() {
        docstring.push_str(&format!("{}/**\n", ident));
        for doc in docs {
            docstring.push_str(&format!("{}    {}\n", ident, doc));
        }
        docstring.push_str(&format!("{}*/\n", ident));
    }
    docstring
}

pub fn write_client(
    output: String,
    prefix: String,
    parsed_routes: Vec<Route>,
    fn_infos: HashMap<String, FnInfo>,
    type_infos: HashMap<String, TypeCategory>,
) -> std::io::Result<()> {
    let path = PathBuf::from(output);
    let ending = path
        .extension()
        .unwrap_or_default()
        .to_str()
        .unwrap_or_default();

    let generated_code = match ending {
        "ts" => ts::generate(prefix, parsed_routes, fn_infos, type_infos),
        "js" => js::generate(prefix, parsed_routes, fn_infos, type_infos),
        _ => panic!("Unsupported output file type"),
    };

    std::fs::write(path, generated_code)
}