3.8 KiB
3.8 KiB
P2P Chat Codebase Guide for Agents
This document outlines the development workflow, code style, and architectural patterns for the p2p-chat repository.
1. Build, Test, and Run Commands
Basic Commands
- Build:
cargo build - Run TUI (default):
cargo run - Run GUI:
cargo run -- --gui - Check:
cargo check - Format:
cargo fmt - Lint:
cargo clippy
Testing
- Run all tests:
cargo test - Run a specific test:
cargo test test_name - Run tests with output:
cargo test -- --nocapture
Debugging
- Logs:
- TUI mode logs to
p2p-chat.login the working directory. - GUI mode logs to
stdout(INFO/DEBUG) andstderr(WARN/ERROR).
- TUI mode logs to
- Environment Variables:
RUST_LOG: Control logging levels (e.g.,RUST_LOG=p2p_chat=debug,iroh=info).
2. Code Style & Architecture
General Rust Guidelines
- Edition: Rust 2021.
- Formatting: Strictly follow
rustfmt. - Error Handling: Use
anyhow::Resultfor application-level errors and main functions. Use specificthiserrorenums for libraries/modules when precise error handling is required. - Async/Await: The project is built on the
tokioruntime. Useasync/awaitfor I/O-bound tasks.
Project Structure
src/main.rs: Application entry point, runtime setup, and main event loop.src/app_logic.rs: Core business logic (AppLogicstruct). Handles network events and application commands. It is agnostic of the UI (TUI vs. GUI).src/gui.rs: Iced-based GUI implementation following the Elm architecture (Model-View-Update).src/tui/: Ratatui-based TUI implementation.src/net/: Networking layer wrappingirohandiroh-gossip.src/media/: Audio/Video capture and playback (GStreamer, cpal, FFmpeg).src/protocol/: Data structures and serialization (serde,bincode,postcard) for network messages.
Naming Conventions
- Variables/Functions:
snake_case. - Types/Traits:
CamelCase. - Constants:
SCREAMING_SNAKE_CASE. - Files:
snake_case.rs.
Architectural Patterns
-
State Management:
AppLogicholds the source of truth for application state (ChatState,MediaState,NetworkManager, etc.).FrontendStateis a simplified struct derived fromAppLogicto pass data to the UI (GUI/TUI) for rendering.- Do not put core business logic inside
gui.rsortui/.
-
Concurrency:
- Use
tokio::spawnfor background tasks. - Use
tokio::sync::mpscchannels for communicating between the UI and the backend logic. - Use
tokio::sync::broadcastfor one-to-many event distribution (e.g., video frames). - Use
Arc<Mutex<...>>orArc<Atomic...>for shared state when channels are insufficient, but prefer message passing.
- Use
-
GUI (Iced):
- Messages: Define all UI interactions in the
Messageenum insrc/gui.rs. - Update: Handle
Messages in theupdatefunction. - View: Keep the
viewfunction strictly for rendering based onself.state. - Subscriptions: Use
subscriptionto listen for external events (e.g., backend state updates, video frames).
- Messages: Define all UI interactions in the
-
Networking:
- Based on
irohQUIC. - Events are received in the main loop and processed by
AppLogic::handle_net_event.
- Based on
Dependencies
- Networking:
iroh,iroh-gossip. - Runtime:
tokio. - UI:
iced(GUI),ratatui+crossterm(TUI). - Media:
gstreamer(video capture),cpal(audio I/O),ffmpeg-next(video encoding).
Common Tasks
- Adding a new feature:
- Update
protocol/mod.rsif it involves new network messages. - Update
AppLogicto handle the logic. - Update
FrontendStateto expose data to the UI. - Update
gui.rsandtui/to render the new state.
- Update