refactor(manifest): tighten ManifestPath API

- Add `parent_or_dot()` helper to replace the repeated
  `.parent().unwrap_or_else(|| Path::new("."))` idiom at three call sites.
- Add `From<ManifestPath> for PathBuf` and use it in
  `BundleFileResolver::resolve` to drop a per-resolve `PathBuf` clone.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Bryan Helmkamp 2026-04-29 12:37:46 -04:00
parent 786e01c7a7
commit 91a0bdc3f4
No known key found for this signature in database
4 changed files with 22 additions and 16 deletions

View file

@ -385,11 +385,8 @@ fn resolve_manifest_dockerfile(
return Ok(());
};
let path_owned = path.clone();
let manifest_path = ManifestPath::from_reference(
config_path.parent().unwrap_or_else(|| Path::new(".")),
&path_owned,
)
.ok_or_else(|| anyhow!("unsupported dockerfile reference: {path_owned}"))?;
let manifest_path = ManifestPath::from_reference(config_path.parent_or_dot(), &path_owned)
.ok_or_else(|| anyhow!("unsupported dockerfile reference: {path_owned}"))?;
let content = files
.get(&manifest_path)
.cloned()

View file

@ -33,9 +33,10 @@ impl BundleFileResolver {
impl FileResolver for BundleFileResolver {
fn resolve(&self, current_dir: &Path, reference: &str) -> Option<ResolvedFile> {
let path = ManifestPath::from_reference(current_dir, reference)?;
self.files.get(&path).map(|content| ResolvedFile {
path: path.as_path().to_path_buf(),
content: content.clone(),
let content = self.files.get(&path)?.clone();
Some(ResolvedFile {
path: path.into(),
content,
})
}
}

View file

@ -43,6 +43,19 @@ impl ManifestPath {
pub fn parent(&self) -> Option<&Path> {
self.0.parent()
}
/// Directory that contains this path, falling back to `.` when the path
/// has no parent component (e.g. a bare file name).
#[must_use]
pub fn parent_or_dot(&self) -> &Path {
self.0.parent().unwrap_or_else(|| Path::new("."))
}
}
impl From<ManifestPath> for PathBuf {
fn from(value: ManifestPath) -> Self {
value.0
}
}
impl TryFrom<String> for ManifestPath {

View file

@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::sync::Arc;
use serde::{Deserialize, Serialize};
@ -29,9 +29,7 @@ impl BundledWorkflow {
#[must_use]
pub fn current_dir(&self) -> PathBuf {
self.path
.parent()
.map_or_else(|| PathBuf::from("."), Path::to_path_buf)
self.path.parent_or_dot().to_path_buf()
}
}
@ -55,10 +53,7 @@ impl WorkflowBundle {
current_workflow_path: &ManifestPath,
reference: &str,
) -> Option<&BundledWorkflow> {
let current_dir = current_workflow_path
.parent()
.unwrap_or_else(|| Path::new("."));
let path = ManifestPath::from_reference(current_dir, reference)?;
let path = ManifestPath::from_reference(current_workflow_path.parent_or_dot(), reference)?;
self.workflows.get(&path)
}