# Kotoba2TSX
[](https://crates.io/crates/kotoba2tsx)
[](https://docs.rs/kotoba2tsx)
[](https://github.com/com-junkawasaki/kotoba)
**Complete toolchain for converting Kotoba configuration files to React TypeScript components.** Transforms Jsonnet-based UI declarations into production-ready TSX code with full type safety and modern React patterns.
## ๐ฏ Overview
Kotoba2TSX bridges the gap between declarative UI configuration and React development. It parses Kotoba files (Jsonnet format) and generates TypeScript React components with proper typing, state management, and event handling.
## ๐๏ธ Architecture
### Core Pipeline
```
Kotoba File (.kotoba) โ Parser โ AST โ Generator โ TSX Component (.tsx)
โ โ โ โ
Jsonnet/JSON Validation TypeScript React + Hooks
Evaluation & Transform Generation Component Code
```
### Key Components
#### **Parser** (`parser.rs`)
```rust
// Jsonnet-enhanced JSON parsing with validation
pub struct KotobaParser;
impl KotobaParser {
pub fn parse_file(&self, path: &str) -> Result<KotobaConfig>;
pub fn parse_content(&self, content: &str) -> Result<KotobaConfig>;
}
```
#### **Generator** (`generator.rs`)
```rust
// TypeScript + React code generation
pub struct TsxGenerator;
impl TsxGenerator {
pub fn generate_tsx(&self, config: &KotobaConfig) -> Result<String>;
pub fn generate_file(&self, config: &KotobaConfig, path: &str) -> Result<()>;
}
```
#### **SWC Integration** (`swc_integration.rs`)
```rust
// Advanced code formatting and optimization
pub struct SwcCodeGenerator;
impl SwcCodeGenerator {
pub fn format_code(&self, code: &str) -> Result<String>;
pub fn create_react_import(&self) -> String;
}
```
## ๐ Quality Metrics
| **Compilation** | โ
Clean (with warnings to fix) |
| **Tests** | โ
Comprehensive test suite (61 tests) |
| **Documentation** | โ
Complete API docs |
| **Performance** | โ
Efficient parsing and generation |
| **TSX Output** | โ
Production-ready React code |
| **Type Safety** | โ
Full TypeScript integration |
## ๐ง Usage
### Basic Conversion
```rust
use kotoba2tsx::prelude::*;
// Convert content string to TSX
let kotoba_content = r#"{
"name": "MyApp",
"version": "1.0.0",
"theme": "light",
"components": {
"Button": {
"type": "component",
"name": "Button",
"component_type": "button",
"props": {"children": "Click me"}
}
},
"handlers": {},
"states": {},
"config": {}
}"#;
let tsx_code = kotoba2tsx::convert_content(kotoba_content)?;
println!("{}", tsx_code);
```
### File-based Conversion
```rust
use kotoba2tsx::convert_file;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Convert .kotoba file to .tsx file
convert_file("app.kotoba", "App.tsx").await?;
Ok(())
}
```
### Advanced Generation
```rust
use kotoba2tsx::{KotobaParser, TsxGenerator};
// Custom configuration
let parser = KotobaParser::new();
let config = parser.parse_file("complex_app.kotoba").await?;
let generator = TsxGenerator::new();
let tsx_code = generator.generate_tsx(&config)?;
```
## ๐ Ecosystem Integration
Kotoba2TSX is part of the complete Kotoba toolchain:
| `kotoba-jsonnet` | **Required** | Jsonnet evaluation for configuration files |
| `kotoba-core` | **Required** | Base types and IR definitions |
| `kotoba-server` | Optional | REST API for configuration serving |
| `swc` | **Required** | TypeScript/JavaScript processing |
## ๐งช Testing
```bash
cargo test -p kotoba2tsx
```
**Test Coverage:**
- โ
JSON/Jsonnet parsing and validation
- โ
TSX code generation for all component types
- โ
TypeScript interface generation
- โ
React hooks and state management
- โ
Event handler integration
- โ
CSS-in-JS styled components
- โ
SWC code formatting
- โ
File I/O operations
- โ
Error handling and edge cases
## ๐ Performance
- **Fast Parsing**: Efficient Jsonnet evaluation and AST construction
- **Optimized Generation**: Template-based TSX code generation
- **SWC Integration**: Lightning-fast code formatting and optimization
- **Streaming Output**: Memory-efficient large file processing
- **Parallel Processing**: Concurrent file conversion support
## ๐ Security
- **Input Validation**: Comprehensive Jsonnet/JSON syntax validation
- **Code Injection Prevention**: Safe code generation without eval()
- **Type Safety**: Full TypeScript type checking
- **Sanitized Output**: XSS-safe React component generation
## ๐ API Reference
### Core Types
- [`KotobaConfig`] - Main configuration structure
- [`KotobaComponent`] - Individual component definition
- [`ComponentType`] - Component classification enum
- [`TsxGenerator`] - Main code generation engine
- [`KotobaParser`] - Configuration parsing engine
### Generation Options
- [`TsxGenerationOptions`] - Code generation configuration
- [`CssInJsLibrary`] - CSS-in-JS framework selection
- [`ComponentStyle`] - Styling configuration
### Utilities
- [`convert_content()`] - Convert string content to TSX
- [`convert_file()`] - Convert file to file (async)
- [`SwcCodeGenerator`] - Advanced code formatting
## ๐ค Contributing
See the [main Kotoba repository](https://github.com/com-junkawasaki/kotoba) for contribution guidelines.
## ๐ License
Licensed under MIT OR Apache-2.0. See [LICENSE](https://github.com/com-junkawasaki/kotoba/blob/main/LICENSE) for details.