convex_typegen/
lib.rs

1mod codegen;
2pub mod convex;
3pub mod errors;
4
5use std::path::PathBuf;
6
7use codegen::generate_code;
8use convex::{create_functions_ast, create_schema_ast, parse_function_ast, parse_schema_ast};
9use errors::ConvexTypeGeneratorError;
10
11/// Configuration options for the type generator.
12#[derive(Debug, Clone)]
13pub struct Configuration
14{
15    /// Path to the Convex schema file (default: "convex/schema.ts")
16    pub schema_path: PathBuf,
17
18    /// Output file path for generated Rust types (default: "src/convex_types.rs")
19    pub out_file: String,
20
21    /// Paths to Convex function files for generating function argument types
22    pub function_paths: Vec<PathBuf>,
23}
24
25impl Default for Configuration
26{
27    fn default() -> Self
28    {
29        Self {
30            schema_path: PathBuf::from("convex/schema.ts"),
31            out_file: "src/convex_types.rs".to_string(),
32            function_paths: Vec::new(),
33        }
34    }
35}
36
37/// Generates Rust types from Convex schema and function definitions.
38///
39/// # Arguments
40/// * `config` - Configuration options for the type generation process
41///
42/// # Returns
43/// * `Ok(())` if type generation succeeds
44/// * `Err(ConvexTypeGeneratorError)` if an error occurs during generation
45///
46/// # Errors
47/// This function can fail for several reasons:
48/// * Schema file not found
49/// * Invalid schema structure
50/// * IO errors when reading/writing files
51/// * Parse errors in schema or function files
52pub fn generate(config: Configuration) -> Result<(), ConvexTypeGeneratorError>
53{
54    if !config.schema_path.exists() {
55        return Err(ConvexTypeGeneratorError::MissingSchemaFile);
56    }
57
58    let schema_path = config
59        .schema_path
60        .canonicalize()
61        .map_err(|e| ConvexTypeGeneratorError::IOError {
62            file: config.schema_path.to_string_lossy().to_string(),
63            error: e,
64        })?;
65
66    let schema_ast = create_schema_ast(schema_path)?;
67    let functions_ast = create_functions_ast(config.function_paths)?;
68
69    let parsed_schema = parse_schema_ast(schema_ast)?;
70    let parsed_functions = parse_function_ast(functions_ast)?;
71
72    generate_code(&config.out_file, (parsed_schema, parsed_functions))?;
73
74    Ok(())
75}