tauri_typegen/
lib.rs

1//! # Tauri TypeGen
2//!
3//! Automatically generate TypeScript bindings from Tauri commands.
4//!
5//! This library scans Rust source code for `#[tauri::command]` functions and generates
6//! strongly-typed TypeScript interfaces, command functions, and optional Zod schemas
7//! with runtime validation.
8//!
9//! ## Features
10//!
11//! - 🔍 **Automatic Discovery**: Scans Rust source for `#[tauri::command]` functions
12//! - 📝 **TypeScript Generation**: Creates TypeScript interfaces for command parameters and return types
13//! - ✅ **Validation Support**: Optional Zod schema generation with runtime validation
14//! - 🚀 **Command Bindings**: Strongly-typed frontend functions
15//! - 📡 **Event Support**: Discovers and types `app.emit()` events
16//! - 📞 **Channel Support**: Types for streaming `Channel<T>` parameters
17//! - 🏷️ **Serde Support**: Respects `#[serde(rename)]` and `#[serde(rename_all)]` attributes
18//!
19//! ## Quick Start
20//!
21//! ### As a CLI Tool
22//!
23//! ```bash
24//! # Install globally
25//! cargo install tauri-typegen
26//!
27//! # Generate TypeScript bindings
28//! cargo tauri-typegen generate
29//! ```
30//!
31//! ### As a Build Dependency
32//!
33//! Add to your `src-tauri/build.rs`:
34//!
35//! ```rust,ignore
36//! fn main() {
37//!     // Generate TypeScript bindings before build
38//!     tauri_typegen::BuildSystem::generate_at_build_time()
39//!         .expect("Failed to generate TypeScript bindings");
40//!
41//!     tauri_build::build()
42//! }
43//! ```
44//!
45//! ### Programmatic Usage
46//!
47//! ```rust,no_run
48//! use tauri_typegen::{GenerateConfig, generate_from_config};
49//!
50//! let config = GenerateConfig {
51//!     project_path: "./src-tauri".to_string(),
52//!     output_path: "./src/generated".to_string(),
53//!     validation_library: "zod".to_string(),
54//!     verbose: Some(true),
55//!     ..Default::default()
56//! };
57//!
58//! let files = generate_from_config(&config)?;
59//! # Ok::<(), Box<dyn std::error::Error>>(())
60//! ```
61//!
62//! ## Example
63//!
64//! Given this Rust code:
65//!
66//! ```rust,ignore
67//! use serde::{Deserialize, Serialize};
68//!
69//! #[derive(Serialize, Deserialize)]
70//! pub struct User {
71//!     pub id: i32,
72//!     pub name: String,
73//! }
74//!
75//! #[tauri::command]
76//! pub async fn get_user(id: i32) -> Result<User, String> {
77//!     // Implementation
78//! }
79//! ```
80//!
81//! Generates this TypeScript:
82//!
83//! ```typescript
84//! export interface User {
85//!   id: number;
86//!   name: string;
87//! }
88//!
89//! export async function getUser(params: { id: number }): Promise<User> {
90//!   return invoke('get_user', params);
91//! }
92//! ```
93//!
94//! ## Configuration
95//!
96//! Configure via `tauri.conf.json`:
97//!
98//! ```json
99//! {
100//!   "plugins": {
101//!     "tauri-typegen": {
102//!       "project_path": ".",
103//!       "output_path": "../src/generated",
104//!       "validation_library": "zod",
105//!       "type_mappings": {
106//!         "DateTime<Utc>": "string",
107//!         "PathBuf": "string"
108//!       }
109//!     }
110//!   }
111//! }
112//! ```
113
114// Core library modules for the CLI tool
115pub mod analysis;
116pub mod build;
117// pub mod commands; // Removed: plugin commands are not used
118mod error;
119pub mod generators;
120pub mod interface;
121pub mod models;
122
123pub use error::{Error, Result};
124pub use models::*;
125
126// Convenience re-exports for common use cases
127pub use interface::config::GenerateConfig;
128pub use interface::generate_from_config;
129pub use interface::output::{Logger, ProgressReporter};
130
131// Build system integration
132pub use build::BuildSystem;