Skip to content

Commit e892b5b

Browse files
committed
refactor: improve code organization and documentation quality
- Enhance error handling patterns across core modules - Update examples with better documentation and clarity - Refactor command modules for improved maintainability - Standardize code formatting and structure - Update project documentation and guidelines
1 parent f5694ac commit e892b5b

File tree

15 files changed

+590
-354
lines changed

15 files changed

+590
-354
lines changed

CLAUDE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
- Utility functions: git(args, working_dir) -> Result<String>, git_raw(args, working_dir) -> Result<Output>
2222
- Command modules: status.rs, add.rs, commit.rs (in src/commands/)
2323
- Core types: Hash (in src/types.rs)
24-
- Run `cargo build && cargo test && cargo clippy --all-targets --all-features -- -D warnings` after code changes
24+
- Run `cargo fmt && cargo build && cargo test && cargo clippy --all-targets --all-features -- -D warnings` after code changes
2525
- Make sure all examples are running
2626

2727
## Examples

examples/commit_workflows.rs

Lines changed: 87 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
//!
1010
//! Run with: cargo run --example commit_workflows
1111
12-
use rustic_git::{Repository, Hash, Result};
12+
use rustic_git::{Hash, Repository, Result};
1313
use std::fs;
1414
use std::path::Path;
1515

1616
fn main() -> Result<()> {
1717
println!("Rustic Git - Commit Workflows Example\n");
1818

1919
let repo_path = "/tmp/rustic_git_commit_example";
20-
20+
2121
// Clean up any previous run
2222
if Path::new(repo_path).exists() {
2323
fs::remove_dir_all(repo_path).expect("Failed to clean up previous example");
@@ -33,65 +33,74 @@ fn main() -> Result<()> {
3333
// Create initial files
3434
println!("Creating initial project files...");
3535
fs::create_dir_all(format!("{}/src", repo_path))?;
36-
36+
3737
fs::write(
3838
format!("{}/README.md", repo_path),
39-
"# Commit Demo Project\n\nThis project demonstrates commit workflows with rustic-git.\n"
39+
"# Commit Demo Project\n\nThis project demonstrates commit workflows with rustic-git.\n",
4040
)?;
41-
41+
4242
fs::write(
4343
format!("{}/src/main.rs", repo_path),
4444
r#"fn main() {
4545
println!("Hello, Commit Demo!");
4646
}
47-
"#
47+
"#,
4848
)?;
49-
50-
fs::write(format!("{}/Cargo.toml", repo_path), r#"[package]
49+
50+
fs::write(
51+
format!("{}/Cargo.toml", repo_path),
52+
r#"[package]
5153
name = "commit-demo"
5254
version = "0.1.0"
5355
edition = "2021"
54-
"#)?;
55-
56+
"#,
57+
)?;
58+
5659
println!("Created README.md, src/main.rs, and Cargo.toml");
5760

5861
// Stage and commit with basic commit()
5962
println!("\nStaging files for first commit...");
6063
repo.add_all()?;
61-
64+
6265
println!("Creating first commit with basic commit() method:");
6366
let first_hash = repo.commit("Initial commit: Add project structure")?;
64-
67+
6568
println!("First commit created!");
6669
display_hash_info(&first_hash, "First commit");
6770
println!();
6871

6972
println!("=== Hash Type Demonstrations ===\n");
7073

7174
println!("Hash type methods and usage:");
72-
75+
7376
// Demonstrate different ways to work with Hash
7477
let hash_as_string: String = first_hash.to_string();
7578
let hash_as_str: &str = first_hash.as_str();
7679
let short_hash: &str = first_hash.short();
77-
80+
7881
println!(" Hash conversions:");
7982
println!(" as_str(): '{}'", hash_as_str);
8083
println!(" short(): '{}'", short_hash);
8184
println!(" to_string(): '{}'", hash_as_string);
8285
println!(" Display: '{}'", first_hash);
83-
86+
8487
// Demonstrate Hash equality and cloning
8588
let cloned_hash = first_hash.clone();
8689
println!("\n Hash operations:");
8790
println!(" Original == Clone: {}", first_hash == cloned_hash);
88-
println!(" Hash length: {} characters", first_hash.as_str().len());
89-
println!(" Short hash length: {} characters", first_hash.short().len());
90-
91+
println!(
92+
" Hash length: {} characters",
93+
first_hash.as_str().len()
94+
);
95+
println!(
96+
" Short hash length: {} characters",
97+
first_hash.short().len()
98+
);
99+
91100
// Create Hash from different sources for demonstration
92101
let hash_from_string: Hash = "1234567890abcdef".to_string().into();
93102
let hash_from_str: Hash = "fedcba0987654321".into();
94-
103+
95104
println!(" Hash from String: {}", hash_from_string.short());
96105
println!(" Hash from &str: {}", hash_from_str.short());
97106
println!();
@@ -101,8 +110,10 @@ edition = "2021"
101110
// Create more files to commit with custom author
102111
println!("Adding features for custom author commit...");
103112
fs::create_dir_all(format!("{}/tests", repo_path))?;
104-
105-
fs::write(format!("{}/src/lib.rs", repo_path), r#"//! Commit demo library
113+
114+
fs::write(
115+
format!("{}/src/lib.rs", repo_path),
116+
r#"//! Commit demo library
106117
107118
pub fn greet(name: &str) -> String {
108119
format!("Hello, {}! This is a commit demo.", name)
@@ -117,17 +128,21 @@ mod tests {
117128
assert_eq!(greet("Alice"), "Hello, Alice! This is a commit demo.");
118129
}
119130
}
120-
"#)?;
131+
"#,
132+
)?;
121133

122-
fs::write(format!("{}/tests/integration_test.rs", repo_path), r#"use commit_demo::greet;
134+
fs::write(
135+
format!("{}/tests/integration_test.rs", repo_path),
136+
r#"use commit_demo::greet;
123137
124138
#[test]
125139
fn test_integration() {
126140
let result = greet("Integration");
127141
assert!(result.contains("Integration"));
128142
assert!(result.contains("commit demo"));
129143
}
130-
"#)?;
144+
"#,
145+
)?;
131146

132147
println!("Created src/lib.rs and tests/integration_test.rs");
133148

@@ -138,7 +153,7 @@ fn test_integration() {
138153
"Add library code and tests\n\n- Implement greet function with proper documentation\n- Add unit tests and integration tests\n- Prepare for version 0.2.0 release",
139154
"Jane Developer <[email protected]>"
140155
)?;
141-
156+
142157
println!("Commit with custom author created!");
143158
display_hash_info(&second_hash, "Second commit (custom author)");
144159
println!();
@@ -147,23 +162,28 @@ fn test_integration() {
147162

148163
// Demonstrate a series of commits
149164
let mut commit_hashes = vec![first_hash, second_hash];
150-
165+
151166
// Commit 3: Update version
152167
println!("Step 1: Update version information...");
153-
fs::write(format!("{}/Cargo.toml", repo_path), r#"[package]
168+
fs::write(
169+
format!("{}/Cargo.toml", repo_path),
170+
r#"[package]
154171
name = "commit-demo"
155172
version = "0.2.0"
156173
edition = "2021"
157174
description = "A demo project for commit workflows"
158-
"#)?;
175+
"#,
176+
)?;
159177

160178
repo.add(&["Cargo.toml"])?;
161179
let third_hash = repo.commit("Bump version to 0.2.0 and add description")?;
162180
commit_hashes.push(third_hash);
163-
181+
164182
// Commit 4: Add documentation
165183
println!("Step 2: Add documentation...");
166-
fs::write(format!("{}/CHANGELOG.md", repo_path), r#"# Changelog
184+
fs::write(
185+
format!("{}/CHANGELOG.md", repo_path),
186+
r#"# Changelog
167187
168188
## [0.2.0] - 2024-01-01
169189
@@ -177,18 +197,21 @@ description = "A demo project for commit workflows"
177197
### Added
178198
- Initial project structure
179199
- Basic Cargo configuration
180-
"#)?;
200+
"#,
201+
)?;
181202

182203
repo.add(&["CHANGELOG.md"])?;
183204
let fourth_hash = repo.commit_with_author(
184205
"docs: Add CHANGELOG with version history",
185-
"Doc Writer <[email protected]>"
206+
"Doc Writer <[email protected]>",
186207
)?;
187208
commit_hashes.push(fourth_hash);
188-
209+
189210
// Commit 5: Final polish
190211
println!("Step 3: Final polish...");
191-
fs::write(format!("{}/README.md", repo_path), r#"# Commit Demo Project
212+
fs::write(
213+
format!("{}/README.md", repo_path),
214+
r#"# Commit Demo Project
192215
193216
This project demonstrates commit workflows with rustic-git.
194217
@@ -214,17 +237,18 @@ fn main() {
214237
Current version: 0.2.0
215238
216239
See CHANGELOG.md for version history.
217-
"#)?;
240+
"#,
241+
)?;
218242

219243
repo.add(&["README.md"])?;
220244
let fifth_hash = repo.commit("docs: Enhance README with usage examples and features")?;
221245
commit_hashes.push(fifth_hash);
222246

223247
println!("\nComplete commit history created!");
224-
248+
225249
// Display all commits
226250
println!("\n=== Commit History Summary ===\n");
227-
251+
228252
for (i, hash) in commit_hashes.iter().enumerate() {
229253
println!("{}. Commit {}", i + 1, i + 1);
230254
display_hash_info(hash, &format!("Commit {}", i + 1));
@@ -233,9 +257,12 @@ See CHANGELOG.md for version history.
233257

234258
// Compare hashes
235259
println!("Hash comparisons:");
236-
println!(" First commit == Last commit: {}", commit_hashes[0] == commit_hashes[4]);
260+
println!(
261+
" First commit == Last commit: {}",
262+
commit_hashes[0] == commit_hashes[4]
263+
);
237264
println!(" All hashes unique: {}", all_unique(&commit_hashes));
238-
265+
239266
// Show short hashes for all commits
240267
println!("\nAll commit short hashes:");
241268
for (i, hash) in commit_hashes.iter().enumerate() {
@@ -260,17 +287,23 @@ See CHANGELOG.md for version history.
260287
match repo.commit_with_author("This should also fail", "Test Author <[email protected]>") {
261288
Ok(_hash) => println!(" Unexpectedly succeeded with empty custom author commit"),
262289
Err(e) => {
263-
println!(" Expected error for empty commit with custom author: {:?}", e);
290+
println!(
291+
" Expected error for empty commit with custom author: {:?}",
292+
e
293+
);
264294
}
265295
}
266296

267297
// Test commit with empty message (Git might handle this differently)
268298
println!("\nTesting commit with empty message:");
269-
299+
270300
// Create a change to commit
271-
fs::write(format!("{}/temp_for_empty_message.txt", repo_path), "temp content")?;
301+
fs::write(
302+
format!("{}/temp_for_empty_message.txt", repo_path),
303+
"temp content",
304+
)?;
272305
repo.add(&["temp_for_empty_message.txt"])?;
273-
306+
274307
match repo.commit("") {
275308
Ok(hash) => {
276309
println!(" Commit with empty message succeeded: {}", hash.short());
@@ -289,7 +322,10 @@ See CHANGELOG.md for version history.
289322
if final_status.is_clean() {
290323
println!("Repository is clean - all changes committed!");
291324
} else {
292-
println!("Repository has {} uncommitted changes", final_status.files.len());
325+
println!(
326+
"Repository has {} uncommitted changes",
327+
final_status.files.len()
328+
);
293329
}
294330

295331
println!("\nWorkflow summary:");
@@ -312,11 +348,15 @@ fn display_hash_info(hash: &Hash, context: &str) {
312348
println!(" Full hash: {}", hash);
313349
println!(" Short hash: {}", hash.short());
314350
println!(" Hash length: {} chars", hash.as_str().len());
315-
351+
316352
// Show first and last few characters for visual reference
317353
let full = hash.as_str();
318354
if full.len() >= 10 {
319-
println!(" Pattern: {}...{}", &full[..5], &full[full.len()-5..]);
355+
println!(
356+
" Pattern: {}...{}",
357+
&full[..5],
358+
&full[full.len() - 5..]
359+
);
320360
}
321361
}
322362

@@ -329,4 +369,4 @@ fn all_unique(hashes: &[Hash]) -> bool {
329369
}
330370
}
331371
true
332-
}
372+
}

0 commit comments

Comments
 (0)