Skip to content

Commit 77bc3a7

Browse files
authored
Merge pull request #17 from icedoutskay/formating
Formatting, Linting & Pre-commit Hooks
2 parents dd99902 + 7cea8ae commit 77bc3a7

7 files changed

Lines changed: 516 additions & 19 deletions

File tree

.pre-commit-config.yaml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Pre-commit hooks configuration for StellarAid project
2+
# Optional: Enable pre-commit hooks for automatic code quality checks
3+
#
4+
# Installation:
5+
# 1. Install pre-commit: pip install pre-commit
6+
# 2. Install the git hooks: pre-commit install
7+
# 3. (Optional) Run against all files: pre-commit run --all-files
8+
#
9+
# To disable: pre-commit uninstall
10+
11+
repos:
12+
- repo: local
13+
hooks:
14+
- id: rustfmt
15+
name: rustfmt
16+
entry: bash -c 'cargo fmt --all -- --check'
17+
language: system
18+
types: [rust]
19+
pass_filenames: false
20+
stages: [commit]
21+
22+
- id: clippy
23+
name: clippy
24+
entry: bash -c 'cargo clippy --workspace -- -D warnings'
25+
language: system
26+
types: [rust]
27+
pass_filenames: false
28+
stages: [commit]
29+
30+
- id: cargo-test
31+
name: cargo test
32+
entry: bash -c 'cargo test --workspace'
33+
language: system
34+
types: [rust]
35+
pass_filenames: false
36+
stages: [push]
37+
38+
# Alternative: Using rust-toolchain hooks (if preferred)
39+
# - repo: https://github.com/astral-sh/ruff-pre-commit
40+
# rev: v0.1.0
41+
# hooks:
42+
# - id: ruff
43+
# args: [ --fix ]

CONTRIBUTING.md

Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
# Contributing to StellarAid
2+
3+
We're excited that you want to contribute to StellarAid! This document provides guidelines and instructions for contributing to the project.
4+
5+
## Getting Started
6+
7+
### Prerequisites
8+
9+
- Rust stable (automatically managed via `rust-toolchain.toml`)
10+
- Cargo (comes with Rust)
11+
- Git
12+
13+
### Setup Development Environment
14+
15+
```bash
16+
# Clone the repository
17+
git clone https://github.com/YOUR_USERNAME/stellaraid-contract.git
18+
cd stellaraid-contract
19+
20+
# Quick setup (installs dependencies, builds everything)
21+
make setup
22+
23+
# Or manually:
24+
rustup update # Update Rust
25+
make install-tools # Install Soroban CLI and required targets
26+
make build # Build the project
27+
```
28+
29+
## Code Quality Standards
30+
31+
We enforce code quality through **rustfmt**, **clippy**, and optional **pre-commit hooks**. These standards ensure consistency and catch common mistakes.
32+
33+
### 1. Code Formatting with rustfmt
34+
35+
We use [rustfmt](https://rust-lang.github.io/rustfmt/) to maintain consistent code style. The project includes a `rustfmt.toml` with formatting rules.
36+
37+
**Check formatting (fails if code is not formatted):**
38+
```bash
39+
cargo fmt --all -- --check
40+
```
41+
42+
**Auto-format your code:**
43+
```bash
44+
cargo fmt --all
45+
```
46+
47+
Or use the Makefile:
48+
```bash
49+
make fmt
50+
```
51+
52+
**IDE Integration:**
53+
- **VS Code**: Install the "Rust Analyzer" extension and enable `[editor.formatOnSave]`
54+
- **IntelliJ IDEA**: Enable "Reformat code on Save" in Settings
55+
- **Vim/Neovim**: Configure with `rustfmt` as the default formatter
56+
57+
### 2. Linting with Clippy
58+
59+
We use [Clippy](https://github.com/rust-lang/rust-clippy) as a strict linter to catch common pitfalls and improve code quality.
60+
61+
**Run clippy with strict settings:**
62+
```bash
63+
cargo clippy --workspace -- -D warnings
64+
```
65+
66+
Or use the Makefile:
67+
```bash
68+
make lint
69+
```
70+
71+
**Clippy will deny:**
72+
- All standard clippy warnings (`-D warnings`)
73+
- Common correctness issues
74+
- Performance anti-patterns
75+
- Code complexity problems
76+
- Readability issues
77+
78+
If clippy reports warnings, fix them before submitting your PR. In rare cases where clippy gives false positives, you can suppress specific warnings with:
79+
80+
```rust
81+
#[allow(clippy::warning_name)]
82+
```
83+
84+
Include a comment explaining why the warning is suppressed.
85+
86+
### 3. Running Tests
87+
88+
```bash
89+
# Run all tests
90+
cargo test --workspace
91+
92+
# Run tests for a specific crate
93+
cargo test -p stellaraid-core
94+
95+
# Run with output
96+
cargo test --workspace -- --nocapture
97+
```
98+
99+
## Recommended Workflow
100+
101+
### Option A: Manual Quality Checks (Minimum)
102+
103+
Before committing code:
104+
105+
```bash
106+
# 1. Format code
107+
cargo fmt --all
108+
109+
# 2. Run linter
110+
cargo clippy --workspace -- -D warnings
111+
112+
# 3. Run tests
113+
cargo test --workspace
114+
```
115+
116+
Or use the combined Makefile command:
117+
```bash
118+
make fmt lint test
119+
```
120+
121+
### Option B: Automated Checks with Pre-commit Hooks (Optional but Recommended)
122+
123+
For automatic code quality checks on every commit:
124+
125+
#### Installation
126+
127+
1. **Install pre-commit:**
128+
```bash
129+
# macOS/Linux
130+
pip install pre-commit
131+
132+
# Windows (using pip or pip3)
133+
pip install pre-commit
134+
```
135+
136+
2. **Enable pre-commit hooks in your repo:**
137+
```bash
138+
pre-commit install
139+
```
140+
141+
3. **Verify installation:**
142+
```bash
143+
pre-commit run --all-files
144+
```
145+
146+
#### How It Works
147+
148+
Once enabled, pre-commit hooks will automatically run on `git commit`:
149+
150+
- **On commit**: Runs rustfmt check and clippy
151+
- **On push**: Runs the full test suite (optional, can be slower)
152+
153+
If any check fails, commit is aborted. Fix the issues and try again:
154+
155+
```bash
156+
# Fix formatting
157+
cargo fmt --all
158+
159+
# Fix clippy warnings
160+
# (Edit code manually to resolve linter warnings)
161+
162+
# Retry commit
163+
git add .
164+
git commit -m "Your message"
165+
```
166+
167+
#### Temporarily Skip Checks (Last Resort)
168+
169+
```bash
170+
# Skip pre-commit hooks for a single commit
171+
git commit --no-verify
172+
173+
# Disable all pre-commit hooks
174+
pre-commit uninstall
175+
176+
# Re-enable pre-commit hooks
177+
pre-commit install
178+
```
179+
180+
## Submission Guidelines
181+
182+
### Before Submitting a Pull Request
183+
184+
1. **Code Quality**: Ensure all checks pass
185+
```bash
186+
cargo fmt --all -- --check # Format check
187+
cargo clippy --workspace -- -D warnings # Linter
188+
cargo test --workspace # Tests
189+
```
190+
191+
2. **Commit Messages**: Use clear, descriptive messages
192+
```
193+
[feature] Add new crowdfunding tier system
194+
[fix] Resolve panic in donation calculation
195+
[docs] Update API documentation
196+
[test] Add tests for refund logic
197+
```
198+
199+
3. **Tests**: Add tests for new features and bug fixes
200+
201+
4. **Documentation**: Update README.md, rustdoc comments, and this file if needed
202+
203+
### Pull Request Checklist
204+
205+
- [ ] Code is formatted (`cargo fmt --all`)
206+
- [ ] Clippy passes with `-D warnings`
207+
- [ ] All tests pass (`cargo test --workspace`)
208+
- [ ] Tests added for new features
209+
- [ ] Documentation updated
210+
- [ ] Commit messages are clear and descriptive
211+
- [ ] No hardcoded values or debug prints
212+
213+
## Project Structure
214+
215+
```
216+
stellarAid-contract/
217+
├── Cargo.toml # Workspace configuration
218+
├── Makefile # Development commands
219+
├── rustfmt.toml # Code formatting rules
220+
├── rust-toolchain.toml # Rust version & components
221+
├── .pre-commit-config.yaml # Optional pre-commit hooks
222+
├── CONTRIBUTING.md # This file
223+
├── README.md # Project overview
224+
├── crates/
225+
│ ├── contracts/
226+
│ │ └── core/ # Smart contract implementation
227+
│ │ ├── src/lib.rs # Contract code
228+
│ │ └── tests/ # Contract tests
229+
│ └── tools/ # CLI tools and utilities
230+
│ └── src/main.rs # CLI entry point
231+
└── target/ # Build artifacts (ignored)
232+
```
233+
234+
## Key Files
235+
236+
- **rustfmt.toml**: Formatting configuration (max width 100 chars, etc.)
237+
- **rust-toolchain.toml**: Enforces stable Rust with rustfmt and clippy
238+
- **.pre-commit-config.yaml**: Optional git hooks for automatic checks
239+
- **Makefile**: Quick commands for build, test, fmt, lint
240+
241+
## Development Tips
242+
243+
### Useful Commands
244+
245+
```bash
246+
# Check everything (format without modifying)
247+
cargo fmt --all -- --check
248+
249+
# Check clippy with details
250+
cargo clippy --workspace --verbose -- -D warnings
251+
252+
# Test a specific file
253+
cargo test --lib contract::tests
254+
255+
# Build with verbose output
256+
cargo build --verbose
257+
258+
# Generate and open documentation
259+
cargo doc --open
260+
261+
# Check for security issues
262+
cargo install cargo-audit
263+
cargo audit
264+
```
265+
266+
### Common Issues
267+
268+
**Q: Clippy warns about a pattern I want to use**
269+
- Run clippy with `--explain` to understand the issue
270+
- In rare cases, suppress with `#[allow(clippy::lint_name)]` with a comment
271+
272+
**Q: Why do some imports get reordered?**
273+
- rustfmt automatically organizes imports for consistency
274+
275+
**Q: Can I use different formatting rules?**
276+
- No, all contributors use the same `rustfmt.toml` for consistency
277+
278+
**Q: Pre-commit hooks are too slow**
279+
- You can modify `.pre-commit-config.yaml` to run clippy only on push
280+
- Or disable and use `make fmt lint` manually
281+
282+
## Reporting Issues
283+
284+
Found a bug or have a suggestion? Please open an issue with:
285+
- Clear description of the problem
286+
- Steps to reproduce (if applicable)
287+
- Expected vs actual behavior
288+
- Your Rust version (`rustc --version`)
289+
290+
## Code Review Process
291+
292+
1. Automated checks run (GitHub Actions)
293+
2. Manual code review by maintainers
294+
3. Address feedback and iterate
295+
4. Merge when approved
296+
297+
## Questions?
298+
299+
- Check the [README.md](README.md) for project overview
300+
- Open an issue for clarification
301+
- Contact maintainers in discussions
302+
303+
---
304+
305+
**Thank you for contributing to StellarAid!** 🌟
306+
307+
Happy coding!

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ resolver = "2"
44

55
[workspace.dependencies]
66
soroban-sdk = "21.0.0"
7+

0 commit comments

Comments
 (0)