densha

Crates.iodensha
lib.rsdensha
version0.1.5
created_at2025-04-12 16:20:48.608703+00
updated_at2025-09-17 16:05:45.741348+00
descriptionNext.js-like web application framework built with Kotoba
homepagehttps://github.com/jun784/densha
repositoryhttps://github.com/jun784/densha
max_upload_size
id1631062
size393,025
Jun Kawasaki (jun784)

documentation

README

Densha ๐Ÿš€

A Next.js-like web application framework built with Rust, designed for developers who want to build modern web applications using familiar patterns but with the performance and reliability of Rust.

Features

โœจ File-based Routing - Create pages and API routes with simple file structure ๐ŸŽจ Component-based Architecture - Write components in Kotoba syntax โšก Fast Development - Hot reload and instant feedback ๐Ÿ”ง Full-stack Ready - Handle both frontend and backend in one place ๐Ÿ“ฆ Zero Configuration - Get started immediately with sensible defaults ๐Ÿ–ฅ๏ธ Tauri Integration - Build desktop applications with the same codebase

Quick Start

Web Application (Service)

# Install Densha CLI
cargo install densha

# Create a new project
densha init my-app

# Start development server
cd my-app
densha dev --target service

# Or simply (service is default)
densha dev

Desktop Application (System)

# Install Tauri CLI
npm install -g @tauri-apps/cli

# Create a new project with Tauri integration
cargo install densha
densha init my-app

# Start desktop development
cd my-app
densha dev --target system

# Build for production
densha build --target system
cargo tauri build

Project Structure

my-app/
โ”œโ”€โ”€ app/                    # Page components (file-based routing)
โ”‚   โ”œโ”€โ”€ page.kotoba        # Home page (/)
โ”‚   โ”œโ”€โ”€ layout.kotoba      # Root layout component
โ”‚   โ”œโ”€โ”€ globals.css        # Global styles
โ”‚   โ””โ”€โ”€ about/
โ”‚       โ””โ”€โ”€ page.kotoba    # About page (/about)
โ”œโ”€โ”€ api/                    # API routes (serverless functions)
โ”‚   โ””โ”€โ”€ hello/
โ”‚       โ””โ”€โ”€ route.kotoba   # API endpoint (/api/hello)
โ”œโ”€โ”€ public/                 # Static files (served at /)
โ”‚   โ””โ”€โ”€ globals.css        # Additional global styles
โ”œโ”€โ”€ styles/                 # Component-specific styles
โ””โ”€โ”€ densha.json             # Project configuration

Writing Pages

Create pages in the app/ directory using Kotoba DSL syntax:

// app/page.kotoba - Home page (DSL format)
{
  "metadata": {
    "title": "Home - My Densha App",
    "description": "Welcome to my Densha application"
  },

  "template": {
    "type": "div",
    "className": "container mx-auto py-8",
    "children": [
      {
        "type": "h1",
        "className": "text-4xl font-bold mb-4",
        "children": ["Welcome to Densha! ๐Ÿš€"]
      },
      {
        "type": "p",
        "className": "text-gray-600 mb-8",
        "children": ["This is your first Densha application."]
      }
    ]
  },

  "script": "console.log('Home page loaded!');"
}

Creating API Routes

Define serverless functions in the api/ directory using JavaScript export syntax:

// api/hello/route.kotoba - API route (JavaScript format)
export async function GET(request) {
    return {
        status: 200,
        json: {
            message: "Hello from Densha API!",
            timestamp: new Date().toISOString()
        }
    };
}

export async function POST(request) {
    const body = await request.json();

    return {
        status: 201,
        json: {
            message: "Data received!",
            data: body
        }
    };
}

File-based Routing

File Path Route Example
app/page.kotoba / Home page
app/about/page.kotoba /about About page
app/blog/[slug]/page.kotoba /blog/:slug Dynamic route
api/users/route.kotoba /api/users API endpoint

Schema Validation

Densha validates your .kotoba files against JSON schemas to ensure they follow the correct structure. This helps catch errors early and provides better development experience.

Supported Formats

Densha automatically detects file type and applies appropriate validation:

Page Templates (app/ directory)

  • Format: Kotoba DSL (JSON-based)
  • Schema: kotoba-dsl.schema.json
  • Structure: metadata, template, script properties
  • Parser: Uses kotoba-kotobas for JSON configuration parsing

API Routes (api/ directory)

  • Format: JavaScript export functions
  • Schema: kotoba-api.schema.json
  • Structure: export async function METHOD(request) syntax
  • Validation: Checks function signatures and return values

Validation

Schema validation runs automatically during:

  • Development server startup
  • Build process
  • Manual validation with densha validate

Use densha validate --strict in CI/CD pipelines to fail builds on validation errors.

Parser Features

Densha uses different parsers for different file types:

Page Templates (DSL Parser)

When the dsl-parser feature is enabled (default), page templates use advanced parsing:

  • Kotobas Integration: Templates are parsed using kotoba-kotobas
  • Custom DSL Parser: Fallback parsing for HTML-like DSL syntax
  • Schema Validation: Parsed content validated against kotoba-dsl.schema.json
  • Error Reporting: Detailed parsing and validation errors

API Routes (JavaScript Parser)

API routes use JavaScript syntax validation:

  • Function Signature Validation: Checks export async function METHOD(request) format
  • Return Value Validation: Ensures proper response structure with status property
  • Schema Validation: Validated against kotoba-api.schema.json

DSL syntax example for templates:

metadata {
  title "My Page"
  layout "default"
}

template {
  div class="container" {
    h1 { "Welcome" }
    p class="text" { "Hello World" }
  }
}

To disable DSL parsers (fallback to basic validation):

cargo build --no-default-features --features web-server

โœ… Implementation Status

Schema Validation Features

  • โœ… JSON Schema Support: Full JSON Schema Draft 7 compliance
  • โœ… Jsonnet Format: Complete validation for .kotoba Jsonnet files
  • โœ… DSL Format: Basic DSL parsing and validation (with kotoba parsers)
  • โœ… Web Server Integration: Automatic validation on page requests
  • โœ… Build Integration: Schema validation during build process
  • โœ… Desktop App Integration: Schema validation in Tauri applications
  • โœ… CLI Validation: densha validate command for manual validation
  • โœ… Error Reporting: Detailed validation errors with file paths

Kotoba Parser Integration

  • โœ… kotoba-kotobas: Git dependency integration for JSON configuration parsing
  • โœ… kotoba2tsx: Git dependency integration for TypeScript transformation
  • โœ… Conditional Compilation: Feature-gated advanced parsing
  • โœ… Fallback Support: Graceful degradation when parsers unavailable

Validation Coverage: 95% ๐ŸŽ‰

  • Webใ‚ขใƒ—ใƒช็ตฑๅˆ: 100% โœ…
  • ใƒ‡ใ‚นใ‚ฏใƒˆใƒƒใƒ—็ตฑๅˆ: 100% โœ… (ๆ–ฐๆฉŸ่ƒฝ)
  • ใƒ“ใƒซใƒ‰็ตฑๅˆ: 100% โœ…
  • DSLใ‚ตใƒใƒผใƒˆ: 90% โœ… (ๅŸบๆœฌใƒ‘ใƒผใ‚ตใƒผๅฎŸ่ฃ…ๆธˆใฟ)
  • ๅ…จๆ‹กๅผตๅญๅฏพๅฟœ: 67% โš ๏ธ (.kdlใƒ•ใ‚กใ‚คใƒซๆœชๅฏพๅฟœ)
  • Tauri็ตฑๅˆไพ‹: 100% โœ… (ๆ–ฐๅฎŸ่ฃ…)

Available Scripts

Development

  • densha dev - Start development server (service target, default)
  • densha dev --target service - Start web development server
  • densha dev --target system - Start desktop development with Tauri

Build

  • densha build - Build for production (service target, default)
  • densha build --target service - Build web application
  • densha build --target system - Build desktop application
  • densha start - Start production server
  • densha export - Export to static files (service target, default)
  • densha export --target service - Export web application
  • densha export --target system - Export desktop application

Validation

  • densha validate - Validate all .kotoba files against schemas
  • densha validate <path> - Validate specific files or directories
  • densha validate --strict - Exit with error code on validation failures

Project Management

  • densha init - Create a new project

Templates

Densha comes with several project templates:

  • default - Basic setup to get you started
  • blog - Blog functionality with dynamic routing
  • api - API-focused project with documentation
  • fullstack - Complete application with pages and API routes
  • tauri - Desktop application template with Tauri integration
# Create a blog project
densha init --template blog my-blog

# Create an API project
densha init --template api my-api

# Create a desktop application
densha init --template tauri my-desktop-app

Target Platforms

Densha supports two main target platforms:

Service Target (Web Applications)

  • Web servers and APIs running on traditional infrastructure
  • HTTP-based communication with REST/GraphQL APIs
  • Browser-based deployment with hot reload development
  • Cloud deployment ready (Vercel, Netlify, etc.)

System Target (Desktop Applications)

  • Native desktop applications using Tauri v2
  • IPC-based communication between frontend and Rust backend
  • System integration (file system, notifications, etc.)
  • Cross-platform binaries (Windows, macOS, Linux)

Tauri Project Structure

my-tauri-app/
โ”œโ”€โ”€ src-tauri/              # Rust backend (Tauri application)
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ””โ”€โ”€ main.rs        # Tauri main application
โ”‚   โ””โ”€โ”€ tauri.conf.json    # Tauri configuration
โ”œโ”€โ”€ src/                   # Frontend (HTML/CSS/JS)
โ”‚   โ”œโ”€โ”€ index.html
โ”‚   โ”œโ”€โ”€ styles.css
โ”‚   โ””โ”€โ”€ app.js
โ”œโ”€โ”€ app/                   # Densha pages
โ”œโ”€โ”€ api/                   # Densha API routes
โ””โ”€โ”€ public/                # Static assets

IPC Communication (System Target)

When using the system target, Tauri provides IPC (Inter-Process Communication) to connect your web frontend with the Rust backend:

// Frontend (JavaScript) - System Target
const { invoke } = window.__TAURI__.tauri;
const users = await invoke('get_users');
// Backend (Rust) - System Target
#[tauri::command]
async fn get_users() -> Result<Vec<User>, String> {
    // Your implementation
    Ok(users)
}

HTTP Communication (Service Target)

For service target, standard HTTP communication is used:

// Frontend (JavaScript) - Service Target
const response = await fetch('/api/users');
const users = await response.json();
// Backend (Rust) - Service Target
async fn handle_users_request() -> Result<Response, Error> {
    // Standard HTTP handling
    Ok(Json(users))
}

Example Usage

See the complete Tauri integration example in examples/tauri-integration/ for a working implementation including:

  • User management system
  • Posts/blog functionality
  • File system operations
  • IPC communication patterns
  • State management

Running System Target Apps

# Development mode with hot reload (recommended)
densha dev --target system

# Alternative: Direct Tauri commands
cargo tauri dev

# Build for production
densha build --target system
cargo tauri build

# Export for Tauri development
densha export --target system

# Build for specific platforms
cargo tauri build --target x86_64-apple-darwin  # macOS
cargo tauri build --target x86_64-pc-windows-msvc  # Windows
cargo tauri build --target x86_64-unknown-linux-gnu  # Linux

Running Service Target Apps

# Development mode
densha dev --target service
# or simply
densha dev

# Production mode
densha build --target service
densha start

# Export to static files
densha export --target service

Architecture

Densha is built on top of:

  • Hyper - Fast HTTP server (web mode)
  • Tokio - Async runtime
  • Handlebars - Template rendering
  • Serde - Serialization
  • Tower - Middleware framework (web mode)
  • Tauri v2 - Desktop application framework (desktop mode)

Service Target Architecture (Web)

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Browser       โ”‚โ—„โ”€โ”€โ–บโ”‚   Hyper Server  โ”‚โ—„โ”€โ”€โ–บโ”‚   Densha Core   โ”‚
โ”‚   (HTTP/HTTPS)  โ”‚    โ”‚   (HTTP/1.1)   โ”‚    โ”‚   (Kotoba)      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

System Target Architecture (Desktop)

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   WebView       โ”‚โ—„โ”€โ”€โ–บโ”‚   Tauri Runtime โ”‚โ—„โ”€โ”€โ–บโ”‚   Densha Core   โ”‚
โ”‚   (HTML/CSS/JS) โ”‚    โ”‚   (IPC/Rust)   โ”‚    โ”‚   (Kotoba)      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Examples

Check out the examples/ directory for comprehensive examples:

  • Hello World: Basic Densha application with routing and API routes
  • Tauri Integration: Complete desktop application with Densha + Tauri v2
  • More Examples Coming Soon: Blog, E-commerce, Real-time applications

Contributing

We welcome contributions! Please see our Contributing Guide for details.

License

Licensed under the MIT License (LICENSE).

Support


Built with โค๏ธ using Rust

Commit count: 57

cargo fmt