Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Clippy configuration
avoid-breaking-exported-api = false

32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: CI

on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy

- name: Format check
run: cargo fmt --all -- --check

- name: Build
run: cargo build --verbose --all-targets

- name: Clippy
run: cargo clippy --all-targets -- -D warnings

- name: Run all tests
run: cargo test --verbose --all

15 changes: 3 additions & 12 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
/target/
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
Cargo.lock
*.pdb
.DS_Store
23 changes: 23 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "simple-chat"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "server"
path = "src/bin/server.rs"

[[bin]]
name = "client"
path = "src/bin/client.rs"

[dependencies]
tokio = { version = "1.35", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
clap = { version = "4.4", features = ["derive", "env"] }
futures = "0.3"

[dev-dependencies]
tokio-test = "0.4"

Binary file added Recording 2025-11-24 003708.mp4
Binary file not shown.
124 changes: 124 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# Usage Guide

## Building

```bash
cargo build --release
```

## Running the Server

The server can be run with default settings (127.0.0.1:8080) or with custom host/port via environment variables:

```bash
# Default (127.0.0.1:8080)
cargo run --bin server

# Custom host and port
HOST=0.0.0.0 PORT=9000 cargo run --bin server
```

## Running the Client

The client requires a username and can connect to a server using command-line arguments or environment variables:

```bash
# Using command-line arguments
cargo run --bin client -- --username alice --host 127.0.0.1 --port 8080

# Using environment variables
USERNAME=alice HOST=127.0.0.1 PORT=8080 cargo run --bin client

# Mix of both (CLI args take precedence)
USERNAME=alice cargo run --bin client -- --host 127.0.0.1
```

## Client Commands

Once connected, the client supports the following commands:

- `send <MSG>` - Send a message to the chat room
- `leave` - Disconnect from the server and exit

Example:
```
> send Hello, everyone!
> send How are you?
> leave
```

## Running Tests

```bash
# Run all tests
cargo test

# Run only unit tests
cargo test --lib

# Run only integration tests
cargo test --test integration_test
cargo test --test ci_integration_test
```

## Code Quality Checks

```bash
# Format code
cargo fmt

# Check formatting
cargo fmt --all -- --check

# Run clippy
cargo clippy --all-targets -- -D warnings

# Check compilation
cargo check --all-targets
```

## Setting Up Pre-commit Hook

To install the pre-commit hook that automatically checks formatting, compilation, and clippy:

```bash
# On Unix-like systems (Linux, macOS, WSL)
chmod +x setup-hooks.sh
./setup-hooks.sh

# Or manually
cp hooks/pre-commit .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
```

## Example Session

Terminal 1 (Server):
```bash
$ cargo run --bin server
Server listening on 127.0.0.1:8080
```

Terminal 2 (Client 1):
```bash
$ cargo run --bin client -- --username alice
Connecting to 127.0.0.1:8080...
You joined the chat
> send Hello!
alice: Hello!
>
```

Terminal 3 (Client 2):
```bash
$ cargo run --bin client -- --username bob
Connecting to 127.0.0.1:8080...
[System] alice joined the chat
You joined the chat
> alice: Hello!
> send Hi alice!
bob: Hi alice!
> leave
Leaving chat...
```

41 changes: 41 additions & 0 deletions hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

# Pre-commit hook to ensure code is formatted, compiles, and passes clippy

set -e

echo "Running pre-commit checks..."

# Check if cargo is available
if ! command -v cargo &> /dev/null; then
echo "Error: cargo is not installed or not in PATH"
exit 1
fi

# Format code
echo "Formatting code with rustfmt..."
cargo fmt --all -- --check
if [ $? -ne 0 ]; then
echo "Error: Code is not formatted. Run 'cargo fmt' to fix."
exit 1
fi

# Check compilation
echo "Checking compilation..."
cargo check --all-targets
if [ $? -ne 0 ]; then
echo "Error: Code does not compile."
exit 1
fi

# Run clippy
echo "Running clippy..."
cargo clippy --all-targets -- -D warnings
if [ $? -ne 0 ]; then
echo "Error: Clippy found issues."
exit 1
fi

echo "All pre-commit checks passed!"
exit 0

5 changes: 5 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
edition = "2021"
max_width = 100
tab_spaces = 4
newline_style = "Unix"

21 changes: 21 additions & 0 deletions setup-hooks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

# Setup script to install pre-commit hook

if [ ! -d ".git" ]; then
echo "Error: This script must be run from the repository root"
exit 1
fi

if [ ! -f "hooks/pre-commit" ]; then
echo "Error: hooks/pre-commit not found"
exit 1
fi

cp hooks/pre-commit .git/hooks/pre-commit
# Convert line endings to Unix format (LF) if needed
sed -i 's/\r$//' .git/hooks/pre-commit 2>/dev/null || sed -i '' 's/\r$//' .git/hooks/pre-commit 2>/dev/null || true
chmod +x .git/hooks/pre-commit

echo "Pre-commit hook installed successfully!"

Loading