pctx_codegen 0.3.0

Code generation utilities for pctx
Documentation
pub mod case;
pub mod format;
pub mod schema_type;
pub mod tools;
pub mod typegen;
pub mod utils;

use indexmap::IndexMap;
use schemars::schema::Schema;
use thiserror::Error;

// re-export RootSchema
pub use schemars::schema::RootSchema;

pub use tools::{Tool, ToolSet};

pub type SchemaDefinitions = IndexMap<String, Schema>;
pub type CodegenResult<T> = Result<T, CodegenError>;

#[derive(Debug, Error)]
pub enum CodegenError {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("Type generation error: {0}")]
    TypeGen(String),
}

/// Generates a code-safe docstring for the given string in Typescript
pub fn ts_generate_docstring(content: &str) -> String {
    let mut lines = vec!["/**".to_string()];

    let replace_pat = regex::Regex::new(r"\*\/").expect("invalid docstring replace_pat");
    for line in content.split('\n') {
        // in the unlikely event that the description has a `*/`
        // ending the typescript docstring, we add escapes
        lines.push(format!("* {}", replace_pat.replace_all(line, "*-/")))
    }

    lines.push("*/".into());

    lines.join("\n")
}