pitwall_tauri/lib.rs
1//! # Pitwall-Tauri
2//!
3//! Tauri integration layer for the Pitwall telemetry library.
4//!
5//! This crate provides minimal, stateless bridges between Pitwall's streaming API
6//! and Tauri's IPC channels, plus TypeScript type generation for compile-time
7//! type safety between Rust and TypeScript.
8//!
9//! ## Features
10//!
11//! - **Stream Mirroring**: Simple `to_channel()` function for any Pitwall stream
12//! - **Type Generation**: Export TypeScript bindings from Rust types
13//! - **Zero State**: Completely stateless - user manages connection lifecycle
14//! - **High Performance**: <1% CPU overhead for 60Hz telemetry streaming
15//!
16//! ## Quick Start
17//!
18//! ### 1. Define Your Frame Type
19//!
20//! ```rust,ignore
21//! use pitwall::PitwallFrame;
22//! use serde::{Serialize, Deserialize};
23//! use specta::Type;
24//!
25//! #[derive(Debug, Clone, Serialize, Deserialize, Type, PitwallFrame)]
26//! struct MyTelemetry {
27//! #[pitwall(name = "Speed")]
28//! speed: f32,
29//!
30//! #[pitwall(name = "Gear")]
31//! gear: i32,
32//!
33//! #[pitwall(name = "RPM")]
34//! rpm: f32,
35//! }
36//! ```
37//!
38//! ### 2. Create Tauri Command
39//!
40//! ```rust,ignore
41//! use tauri::ipc::Channel;
42//! use pitwall_tauri::to_channel;
43//!
44//! #[tauri::command]
45//! async fn start_telemetry(
46//! telemetry: Channel<MyTelemetry>,
47//! session: Channel<SessionInfo>,
48//! ) -> Result<(), String> {
49//! let conn = Pitwall::connect().await
50//! .map_err(|e| e.to_string())?;
51//!
52//! // Spawn telemetry stream
53//! tokio::spawn({
54//! let stream = conn.subscribe::<MyTelemetry>(UpdateRate::Native);
55//! async move {
56//! to_channel(stream, telemetry).await
57//! }
58//! });
59//!
60//! // Spawn session updates stream
61//! tokio::spawn({
62//! let stream = conn.session_updates();
63//! async move {
64//! to_channel(stream, session).await
65//! }
66//! });
67//!
68//! Ok(())
69//! }
70//! ```
71//!
72//! ### 3. Generate TypeScript Bindings
73//!
74//! ```rust,ignore
75//! fn main() {
76//! // Generate TypeScript types (run once during build or manually)
77//! #[cfg(debug_assertions)]
78//! {
79//! tauri_specta::ts::export(
80//! specta::collect_types![start_telemetry],
81//! "../src/bindings.ts"
82//! ).expect("Failed to export TypeScript bindings");
83//! }
84//!
85//! tauri::Builder::default()
86//! .invoke_handler(tauri::generate_handler![start_telemetry])
87//! .run(tauri::generate_context!())
88//! .expect("error while running tauri application");
89//! }
90//! ```
91//!
92//! ### 4. Use in TypeScript
93//!
94//! ```typescript
95//! import { invoke, Channel } from '@tauri-apps/api/core';
96//! import type { MyTelemetry, SessionInfo } from './bindings';
97//!
98//! const telemetryChannel = new Channel<MyTelemetry>();
99//! const sessionChannel = new Channel<SessionInfo>();
100//!
101//! telemetryChannel.onmessage = (data) => {
102//! console.log(`Speed: ${data.speed}, Gear: ${data.gear}`);
103//! };
104//!
105//! sessionChannel.onmessage = (session) => {
106//! console.log(`Track: ${session.weekend_info.track_name}`);
107//! };
108//!
109//! await invoke('start_telemetry', {
110//! telemetry: telemetryChannel,
111//! session: sessionChannel,
112//! });
113//! ```
114
115pub mod bridge;
116
117// Re-export Pitwall types for convenience
118pub use pitwall::{Pitwall, SessionInfo, UpdateRate, VariableInfo, VariableSchema, VariableType};
119
120// Re-export bridge function as primary API
121pub use bridge::to_channel;