Skip to content

Commit 14ebec2

Browse files
authored
Merge pull request #4 from raiderrobert/fix/cargo-fmt
fix: resolve cargo fmt formatting issues
2 parents 35f4a1f + 5f32b82 commit 14ebec2

4 files changed

Lines changed: 41 additions & 46 deletions

File tree

crates/diecut-core/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ pub fn generate(options: GenerateOptions) -> Result<GeneratedProject> {
3838
let source = resolve_source(&options.template)?;
3939
let template_dir = match &source {
4040
TemplateSource::Local(path) => path.clone(),
41-
TemplateSource::Git { url, git_ref } => {
42-
get_or_clone(url, git_ref.as_deref())?
43-
}
41+
TemplateSource::Git { url, git_ref } => get_or_clone(url, git_ref.as_deref())?,
4442
};
4543

4644
// 2. Resolve template (auto-detect format, parse config)

crates/diecut-core/src/template/cache.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::path::{Path, PathBuf};
22

3-
use sha2::{Sha256, Digest};
3+
use sha2::{Digest, Sha256};
44

55
use serde::{Deserialize, Serialize};
66

@@ -71,17 +71,10 @@ pub(crate) fn cache_key(url: &str, git_ref: Option<&str>) -> String {
7171
let hash: String = digest.iter().take(8).map(|b| format!("{b:02x}")).collect();
7272

7373
// Sanitize components to prevent path traversal
74-
let sanitize = |s: &str| -> String {
75-
s.replace(['/', '\\'], "_").replace("..", "_")
76-
};
74+
let sanitize = |s: &str| -> String { s.replace(['/', '\\'], "_").replace("..", "_") };
7775

7876
// Build a human-readable prefix from the URL
79-
let prefix = sanitize(
80-
normalized
81-
.rsplit('/')
82-
.next()
83-
.unwrap_or("template"),
84-
);
77+
let prefix = sanitize(normalized.rsplit('/').next().unwrap_or("template"));
8578

8679
match git_ref {
8780
Some(r) => format!("{prefix}-{}-{hash}", sanitize(r)),
@@ -120,9 +113,10 @@ pub fn get_or_clone(url: &str, git_ref: Option<&str>) -> Result<PathBuf> {
120113
git_ref: git_ref.map(String::from),
121114
cached_at: unix_timestamp_secs(),
122115
};
123-
let metadata_toml = toml::to_string_pretty(&metadata).map_err(|e| DicecutError::CacheMetadata {
124-
context: format!("serializing cache metadata: {e}"),
125-
})?;
116+
let metadata_toml =
117+
toml::to_string_pretty(&metadata).map_err(|e| DicecutError::CacheMetadata {
118+
context: format!("serializing cache metadata: {e}"),
119+
})?;
126120
std::fs::write(tmp_dir.path().join(CACHE_METADATA_FILE), metadata_toml).map_err(|e| {
127121
DicecutError::Io {
128122
context: "writing cache metadata".into(),
@@ -143,9 +137,7 @@ pub fn get_or_clone(url: &str, git_ref: Option<&str>) -> Result<PathBuf> {
143137
std::fs::rename(tmp_dir.path(), &cached_path).or_else(|rename_err| {
144138
// rename can fail across filesystems; fall back to copy + delete
145139
copy_dir_all(tmp_dir.path(), &cached_path).map_err(|e| DicecutError::Io {
146-
context: format!(
147-
"copying cloned template to cache (rename failed: {rename_err}): {e}"
148-
),
140+
context: format!("copying cloned template to cache (rename failed: {rename_err}): {e}"),
149141
source: std::io::Error::other(e.to_string()),
150142
})?;
151143
Ok(())
@@ -187,20 +179,18 @@ pub fn list_cached() -> Result<Vec<CachedTemplate>> {
187179
continue;
188180
}
189181

190-
let metadata_str = std::fs::read_to_string(&metadata_path).map_err(|e| DicecutError::Io {
191-
context: format!("reading cache metadata {}", metadata_path.display()),
192-
source: e,
193-
})?;
182+
let metadata_str =
183+
std::fs::read_to_string(&metadata_path).map_err(|e| DicecutError::Io {
184+
context: format!("reading cache metadata {}", metadata_path.display()),
185+
source: e,
186+
})?;
194187

195188
let metadata: CacheMetadata =
196189
toml::from_str(&metadata_str).map_err(|e| DicecutError::CacheMetadata {
197190
context: format!("parsing cache metadata: {e}"),
198191
})?;
199192

200-
let key = entry
201-
.file_name()
202-
.to_string_lossy()
203-
.into_owned();
193+
let key = entry.file_name().to_string_lossy().into_owned();
204194

205195
entries.push(CachedTemplate {
206196
key,
@@ -364,9 +354,18 @@ mod tests {
364354

365355
#[test]
366356
fn normalize_url_strips_trailing_git_and_slash() {
367-
assert_eq!(normalize_url("https://github.com/user/repo.git"), "https://github.com/user/repo");
368-
assert_eq!(normalize_url("https://github.com/user/repo/"), "https://github.com/user/repo");
369-
assert_eq!(normalize_url("https://github.com/user/repo"), "https://github.com/user/repo");
357+
assert_eq!(
358+
normalize_url("https://github.com/user/repo.git"),
359+
"https://github.com/user/repo"
360+
);
361+
assert_eq!(
362+
normalize_url("https://github.com/user/repo/"),
363+
"https://github.com/user/repo"
364+
);
365+
assert_eq!(
366+
normalize_url("https://github.com/user/repo"),
367+
"https://github.com/user/repo"
368+
);
370369
}
371370

372371
#[test]

crates/diecut-core/src/template/clone.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,20 @@ pub fn clone_template(url: &str, git_ref: Option<&str>) -> Result<tempfile::Temp
2828
})?;
2929

3030
// Use prepare_clone (with worktree) so we get a checked-out working copy
31-
let mut prepare = gix::prepare_clone(url, tmp_dir.path()).map_err(|e| {
32-
DicecutError::GitClone {
31+
let mut prepare =
32+
gix::prepare_clone(url, tmp_dir.path()).map_err(|e| DicecutError::GitClone {
3333
url: url.to_string(),
3434
reason: e.to_string(),
35-
}
36-
})?;
35+
})?;
3736

3837
// If a specific ref is requested, configure it before fetching
3938
if let Some(ref_name) = git_ref {
40-
prepare = prepare.with_ref_name(Some(ref_name)).map_err(|e| {
41-
DicecutError::GitCheckout {
39+
prepare = prepare
40+
.with_ref_name(Some(ref_name))
41+
.map_err(|e| DicecutError::GitCheckout {
4242
git_ref: ref_name.to_string(),
4343
reason: e.to_string(),
44-
}
45-
})?;
44+
})?;
4645
}
4746

4847
// Fetch and prepare for checkout
@@ -54,13 +53,12 @@ pub fn clone_template(url: &str, git_ref: Option<&str>) -> Result<tempfile::Temp
5453
})?;
5554

5655
// Checkout the main worktree
57-
let (_repo, _outcome) =
58-
checkout
59-
.main_worktree(gix::progress::Discard, &gix::interrupt::IS_INTERRUPTED)
60-
.map_err(|e| DicecutError::GitClone {
61-
url: url.to_string(),
62-
reason: format!("worktree checkout failed: {e}"),
63-
})?;
56+
let (_repo, _outcome) = checkout
57+
.main_worktree(gix::progress::Discard, &gix::interrupt::IS_INTERRUPTED)
58+
.map_err(|e| DicecutError::GitClone {
59+
url: url.to_string(),
60+
reason: format!("worktree checkout failed: {e}"),
61+
})?;
6462

6563
Ok(tmp_dir)
6664
}

crates/diecut-core/src/template/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ pub mod cache;
22
pub mod clone;
33
pub mod source;
44

5-
pub use cache::{get_or_clone, list_cached, clear_cache, CachedTemplate};
5+
pub use cache::{clear_cache, get_or_clone, list_cached, CachedTemplate};
66
pub use clone::clone_template;
77
pub use source::{resolve_source, resolve_source_with_ref, TemplateSource};

0 commit comments

Comments
 (0)