|
| 1 | +# Git Workers - コーディングスタイルガイド |
| 2 | + |
| 3 | +このドキュメントでは、Git Workers プロジェクトにおけるコードの統一感とスタイル規則を定義します。 |
| 4 | + |
| 5 | +## 基本原則 |
| 6 | + |
| 7 | +### 1. 一貫性の優先 |
| 8 | + |
| 9 | +- 既存のコードパターンに合わせる |
| 10 | +- 新しいパターンを導入する際は全体に適用する |
| 11 | +- 部分的な変更より全体的な統一感を重視 |
| 12 | + |
| 13 | +### 2. 可読性の重視 |
| 14 | + |
| 15 | +- 意図が明確なコード |
| 16 | +- 適切な命名規則 |
| 17 | +- 冗長性の排除 |
| 18 | + |
| 19 | +## フォーマット規則 |
| 20 | + |
| 21 | +### String フォーマット |
| 22 | + |
| 23 | +**インライン変数構文の使用(必須)** |
| 24 | + |
| 25 | +```rust |
| 26 | +// ✅ 推奨 |
| 27 | +format!("Created worktree '{name}' at {path}") |
| 28 | +println!("Found {count} items") |
| 29 | +eprintln!("Error: {error}") |
| 30 | + |
| 31 | +// ❌ 非推奨(古い形式) |
| 32 | +format!("Created worktree '{}' at {}", name, path) |
| 33 | +println!("Found {} items", count) |
| 34 | +eprintln!("Error: {}", error) |
| 35 | +``` |
| 36 | + |
| 37 | +**適用範囲** |
| 38 | + |
| 39 | +- `format!` |
| 40 | +- `println!`, `eprintln!` |
| 41 | +- `log::info!`, `log::warn!`, `log::error!` |
| 42 | +- `panic!` |
| 43 | +- その他すべてのフォーマット系マクロ |
| 44 | + |
| 45 | +**例外** |
| 46 | +format! マクロで文字列リテラルが必要な場合(anyhow! エラーなど)は従来形式を使用: |
| 47 | + |
| 48 | +```rust |
| 49 | +// anyhow! では文字列リテラルが必要 |
| 50 | +anyhow!("Invalid path: {}", path) // OK |
| 51 | +``` |
| 52 | + |
| 53 | +## 定数管理 |
| 54 | + |
| 55 | +### 定数の集約化 |
| 56 | + |
| 57 | +**constants.rs での集中管理** |
| 58 | + |
| 59 | +```rust |
| 60 | +// ✅ 推奨 - constants.rs に定義 |
| 61 | +pub const ERROR_USER_CANCELLED: &str = "User cancelled operation"; |
| 62 | +pub const MSG_WORKTREE_CREATED: &str = "Worktree created successfully"; |
| 63 | + |
| 64 | +// 使用箇所 |
| 65 | +return Err(anyhow!(ERROR_USER_CANCELLED)); |
| 66 | +``` |
| 67 | + |
| 68 | +**ハードコーディングの禁止** |
| 69 | + |
| 70 | +```rust |
| 71 | +// ❌ 避ける - 直接文字列 |
| 72 | +return Err(anyhow!("User cancelled operation")); |
| 73 | + |
| 74 | +// ✅ 推奨 - 定数使用 |
| 75 | +return Err(anyhow!(ERROR_USER_CANCELLED)); |
| 76 | +``` |
| 77 | + |
| 78 | +### 定数の命名規則 |
| 79 | + |
| 80 | +**プレフィックス規則** |
| 81 | + |
| 82 | +- `ERROR_*`: エラーメッセージ |
| 83 | +- `MSG_*`: 一般的なメッセージ |
| 84 | +- `ICON_*`: アイコン文字 |
| 85 | +- `FORMAT_*`: フォーマット文字列 |
| 86 | +- `DEFAULT_*`: デフォルト値 |
| 87 | + |
| 88 | +**例** |
| 89 | + |
| 90 | +```rust |
| 91 | +pub const ERROR_WORKTREE_NOT_FOUND: &str = "Worktree not found"; |
| 92 | +pub const MSG_WORKTREE_DELETED: &str = "Worktree deleted successfully"; |
| 93 | +pub const ICON_CURRENT_BRANCH: &str = " "; |
| 94 | +pub const DEFAULT_BRANCH_NAME: &str = "main"; |
| 95 | +``` |
| 96 | + |
| 97 | +### テスト定数 |
| 98 | + |
| 99 | +**テスト専用定数の管理** |
| 100 | + |
| 101 | +```rust |
| 102 | +// ✅ 推奨 - cfg(test) アノテーション使用 |
| 103 | +#[cfg(test)] |
| 104 | +const TEST_WORKTREE_NAME: &str = "test-worktree"; |
| 105 | +#[cfg(test)] |
| 106 | +const TEST_BRANCH_NAME: &str = "test-branch"; |
| 107 | + |
| 108 | +// テスト内で使用 |
| 109 | +#[test] |
| 110 | +fn test_create_worktree() { |
| 111 | + let name = TEST_WORKTREE_NAME; |
| 112 | + // ... |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +## エラーハンドリング |
| 117 | + |
| 118 | +### 統一的なエラーメッセージ |
| 119 | + |
| 120 | +**メッセージ構造** |
| 121 | + |
| 122 | +```rust |
| 123 | +// 基本パターン: "動作が失敗した理由" |
| 124 | +ERROR_WORKTREE_CREATE_FAILED: "Failed to create worktree" |
| 125 | +ERROR_BRANCH_NOT_FOUND: "Branch not found" |
| 126 | +ERROR_PERMISSION_DENIED: "Permission denied" |
| 127 | + |
| 128 | +// 詳細パターン: "動作が失敗した理由: {詳細}" |
| 129 | +ERROR_CONFIG_READ_FAILED: "Failed to read configuration: {}" |
| 130 | +ERROR_WORKTREE_PATH_INVALID: "Invalid worktree path: {}" |
| 131 | +``` |
| 132 | + |
| 133 | +**anyhow! エラーの統一** |
| 134 | + |
| 135 | +```rust |
| 136 | +// ✅ 推奨パターン |
| 137 | +return Err(anyhow!("Failed to create worktree: {}", error)); |
| 138 | + |
| 139 | +// 避けるパターン |
| 140 | +return Err(anyhow!("Worktree creation error: {}", error)); |
| 141 | +return Err(anyhow!("Error creating worktree: {}", error)); |
| 142 | +``` |
| 143 | + |
| 144 | +## コード構造 |
| 145 | + |
| 146 | +### ファイル構成 |
| 147 | + |
| 148 | +**モジュール構成の原則** |
| 149 | + |
| 150 | +``` |
| 151 | +src/ |
| 152 | +├── commands/ # コマンド実装(機能別) |
| 153 | +├── core/ # コアロジック |
| 154 | +├── infrastructure/ # 外部依存(Git, ファイルシステム) |
| 155 | +├── constants.rs # 全定数の集約 |
| 156 | +├── ui.rs # UI 抽象化 |
| 157 | +└── utils.rs # ユーティリティ |
| 158 | +``` |
| 159 | + |
| 160 | +**import の順序** |
| 161 | + |
| 162 | +```rust |
| 163 | +// 1. 標準ライブラリ |
| 164 | +use std::path::PathBuf; |
| 165 | +use std::collections::HashMap; |
| 166 | + |
| 167 | +// 2. 外部クレート(アルファベット順) |
| 168 | +use anyhow::Result; |
| 169 | +use colored::Colorize; |
| 170 | + |
| 171 | +// 3. 内部モジュール(階層順) |
| 172 | +use crate::constants::*; |
| 173 | +use crate::core::validation; |
| 174 | +use crate::ui::UserInterface; |
| 175 | +``` |
| 176 | + |
| 177 | +### 関数設計 |
| 178 | + |
| 179 | +**命名規則** |
| 180 | + |
| 181 | +```rust |
| 182 | +// ✅ 動詞 + 目的語パターン |
| 183 | +fn create_worktree() -> Result<()> |
| 184 | +fn delete_branch() -> Result<()> |
| 185 | +fn validate_path() -> Result<()> |
| 186 | + |
| 187 | +// ✅ is/has などの述語パターン |
| 188 | +fn is_current_worktree() -> bool |
| 189 | +fn has_uncommitted_changes() -> bool |
| 190 | +``` |
| 191 | + |
| 192 | +**引数の順序** |
| 193 | + |
| 194 | +```rust |
| 195 | +// 1. 主要オブジェクト |
| 196 | +// 2. 設定・オプション |
| 197 | +// 3. UI インターフェース |
| 198 | +fn create_worktree( |
| 199 | + manager: &WorktreeManager, |
| 200 | + name: &str, |
| 201 | + options: &CreateOptions, |
| 202 | + ui: &dyn UserInterface |
| 203 | +) -> Result<bool> |
| 204 | +``` |
| 205 | + |
| 206 | +## テストコード |
| 207 | + |
| 208 | +### テスト構成 |
| 209 | + |
| 210 | +**テストファイル命名** |
| 211 | + |
| 212 | +- 単体テスト: `tests/unit/module_name.rs` |
| 213 | +- 統合テスト: `tests/integration/feature_name.rs` |
| 214 | +- E2E テスト: `tests/e2e/workflow_name.rs` |
| 215 | + |
| 216 | +**テスト関数命名** |
| 217 | + |
| 218 | +```rust |
| 219 | +// パターン: test_[対象]_[条件]_[期待結果] |
| 220 | +#[test] |
| 221 | +fn test_create_worktree_with_valid_name_succeeds() -> Result<()> |
| 222 | + |
| 223 | +#[test] |
| 224 | +fn test_delete_worktree_when_not_exists_fails() -> Result<()> |
| 225 | +``` |
| 226 | + |
| 227 | +### Mock の使用 |
| 228 | + |
| 229 | +**UI Mock パターン** |
| 230 | + |
| 231 | +```rust |
| 232 | +let ui = TestUI::new() |
| 233 | + .with_input(TEST_WORKTREE_NAME) |
| 234 | + .with_selection(0) |
| 235 | + .with_confirmation(true); |
| 236 | +``` |
| 237 | + |
| 238 | +## リファクタリング指針 |
| 239 | + |
| 240 | +### 段階的改善 |
| 241 | + |
| 242 | +1. **動作変更なし**のリファクタリングを先行 |
| 243 | +2. **機能追加**は別コミット |
| 244 | +3. **テスト追加**で安全性確保 |
| 245 | + |
| 246 | +### 品質チェック |
| 247 | + |
| 248 | +**必須チェック項目** |
| 249 | + |
| 250 | +```bash |
| 251 | +# フォーマット確認 |
| 252 | +cargo fmt --check |
| 253 | + |
| 254 | +# Clippy 警告ゼロ |
| 255 | +cargo clippy --all-features -- -D warnings |
| 256 | + |
| 257 | +# テスト通過 |
| 258 | +cargo test --all-features |
| 259 | + |
| 260 | +# 型チェック |
| 261 | +cargo check --all-features |
| 262 | +``` |
| 263 | + |
| 264 | +## コメント規則 |
| 265 | + |
| 266 | +### コメントの言語統一 |
| 267 | + |
| 268 | +**英語コメントの徹底使用(必須)** |
| 269 | + |
| 270 | +```rust |
| 271 | +// ✅ 推奨 - 英語コメント |
| 272 | +// Check if the worktree exists before deletion |
| 273 | +fn delete_worktree(name: &str) -> Result<()> { |
| 274 | + // Validate worktree name format |
| 275 | + if name.is_empty() { |
| 276 | + return Err(anyhow!("Worktree name cannot be empty")); |
| 277 | + } |
| 278 | + |
| 279 | + // Execute deletion command |
| 280 | + // ... |
| 281 | +} |
| 282 | + |
| 283 | +// ❌ 非推奨 - 日本語コメント |
| 284 | +// ワークツリーが存在するかチェック |
| 285 | +fn delete_worktree(name: &str) -> Result<()> { |
| 286 | + // ワークツリー名のフォーマットを検証 |
| 287 | + // ... |
| 288 | +} |
| 289 | +``` |
| 290 | + |
| 291 | +**コメント品質基準** |
| 292 | + |
| 293 | +```rust |
| 294 | +// ✅ Good - Clear and concise |
| 295 | +// Calculate relative path from project root |
| 296 | +let relative_path = calculate_relative_path(&base_path, &target_path)?; |
| 297 | + |
| 298 | +// ✅ Good - Explains complex logic |
| 299 | +// Use fuzzy matching for branch selection to improve UX |
| 300 | +// when dealing with large numbers of branches |
| 301 | +let selection = fuzzy_select_branch(&branches)?; |
| 302 | + |
| 303 | +// ❌ Avoid - Stating the obvious |
| 304 | +// Set variable to true |
| 305 | +let is_valid = true; |
| 306 | + |
| 307 | +// ❌ Avoid - Outdated or incorrect comments |
| 308 | +// TODO: This will be removed in v2.0 (but never removed) |
| 309 | +``` |
| 310 | + |
| 311 | +**ドキュメントコメント(///)の使用** |
| 312 | + |
| 313 | +````rust |
| 314 | +/// Creates a new worktree with the specified configuration |
| 315 | +/// |
| 316 | +/// # Arguments |
| 317 | +/// * `manager` - The worktree manager instance |
| 318 | +/// * `name` - Name for the new worktree |
| 319 | +/// * `config` - Creation configuration options |
| 320 | +/// |
| 321 | +/// # Returns |
| 322 | +/// * `Ok(true)` - Worktree created and switched to |
| 323 | +/// * `Ok(false)` - Worktree created but not switched |
| 324 | +/// * `Err(_)` - Creation failed |
| 325 | +/// |
| 326 | +/// # Examples |
| 327 | +/// ``` |
| 328 | +/// let result = create_worktree(&manager, "feature-branch", &config)?; |
| 329 | +/// ``` |
| 330 | +pub fn create_worktree( |
| 331 | + manager: &WorktreeManager, |
| 332 | + name: &str, |
| 333 | + config: &CreateConfig |
| 334 | +) -> Result<bool> { |
| 335 | + // Implementation... |
| 336 | +} |
| 337 | +```` |
| 338 | + |
| 339 | +**TODO/FIXME/NOTE コメント** |
| 340 | + |
| 341 | +```rust |
| 342 | +// TODO: Add support for custom hooks in v0.7.0 |
| 343 | +// FIXME: Handle edge case when git directory is corrupted |
| 344 | +// NOTE: This behavior matches Git's native worktree command |
| 345 | +// HACK: Temporary workaround for upstream bug #1234 |
| 346 | +``` |
| 347 | + |
| 348 | +### 既存コメントの英語化 |
| 349 | + |
| 350 | +**段階的移行方針** |
| 351 | + |
| 352 | +1. 新規コード:英語コメント必須 |
| 353 | +2. 既存コード:修正時に英語化 |
| 354 | +3. 重要なコメント:優先的に英語化 |
| 355 | + |
| 356 | +**英語化対象の優先順位** |
| 357 | + |
| 358 | +1. 公開 API(`pub`)のドキュメントコメント |
| 359 | +2. 複雑なロジックの説明コメント |
| 360 | +3. TODO/FIXME コメント |
| 361 | +4. テストコメント |
| 362 | +5. 一般的なインラインコメント |
| 363 | + |
| 364 | +## 今後の拡張 |
| 365 | + |
| 366 | +### 新機能追加時の指針 |
| 367 | + |
| 368 | +1. **constants.rs** への文字列集約 |
| 369 | +2. **インライン変数構文** の徹底使用 |
| 370 | +3. **統一的なエラーメッセージ** フォーマット |
| 371 | +4. **適切なテスト** カバレッジ |
| 372 | +5. **英語コメント** の使用 |
| 373 | + |
| 374 | +### 既存コードの改善 |
| 375 | + |
| 376 | +定期的に以下を実行: |
| 377 | + |
| 378 | +- format! マクロの統一性チェック |
| 379 | +- ハードコード文字列の constants.rs 移行 |
| 380 | +- エラーメッセージの統一性確認 |
| 381 | +- テスト定数の整理 |
| 382 | +- **日本語コメントの英語化** |
| 383 | + |
| 384 | +--- |
| 385 | + |
| 386 | +このガイドラインに従うことで、Git Workers プロジェクトの品質と保守性を継続的に向上させることができます。 |
0 commit comments